Repository Public
1import { useId, useState, type FormEvent } from 'react';2import { api, ApiError, CodeBlock, CopyButton, ErrorBlock, Icon, Loading, StateBlock, safeHref, useDocumentTitle } from '@spweb/shared-ui';3import { useT } from '@spweb/shared-ui/i18n/bot';4import { MAIL } from './types';56type Phase = { k: 'idle' } | { k: 'sending' } | { k: 'error'; error: ApiError } | { k: 'done'; id: string };7type Field = 'target' | 'email' | 'reason';89const DOMAIN = /^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9-]{2,63}$/i;10const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;1112export function Removal() {13 const { t } = useT();14 const id = useId();15 const [scope, setScope] = useState<'url' | 'domain'>('url');16 const [phase, setPhase] = useState<Phase>({ k: 'idle' });17 const [errs, setErrs] = useState<Partial<Record<Field, string>>>({});18 useDocumentTitle(t('removal.meta'));1920 const check = (field: Field, value: string): string | undefined => {21 const v = value.trim();22 if (field === 'target') {23 if (scope === 'url') return safeHref(v) ? undefined : t('removal.err.target.url');24 return DOMAIN.test(v) ? undefined : t('removal.err.target.domain');25 }26 if (field === 'email') return EMAIL.test(v) ? undefined : t('removal.err.email');27 return v ? undefined : t('removal.err.reason');28 };29 const onBlur = (field: Field) => (e: { currentTarget: { value: string } }) => {30 const v = e.currentTarget.value;31 // Validate on blur only once something was typed; an untouched empty field is judged at submit.32 if (v.trim() !== '' || errs[field]) setErrs((p) => ({ ...p, [field]: check(field, v) }));33 };3435 const submit = (e: FormEvent<HTMLFormElement>) => {36 e.preventDefault();37 const f = new FormData(e.currentTarget);38 const get = (k: string) => String(f.get(k) ?? '').trim();39 const body = { scope, target: get('target'), email: get('email'), reason: get('reason'), relationship: get('relationship') || 'other' };40 const next = { target: check('target', body.target), email: check('email', body.email), reason: check('reason', body.reason) };41 setErrs(next);42 const firstBad = (['target', 'email', 'reason'] as const).find((k) => next[k]);43 if (firstBad) {44 (e.currentTarget.elements.namedItem(firstBad) as HTMLElement | null)?.focus();45 return;46 }47 setPhase({ k: 'sending' });48 api<{ request_id: string }>('/api/public/removal', { method: 'POST', body }).then(49 (r) => setPhase({ k: 'done', id: r?.request_id ?? '' }),50 (err: unknown) => setPhase({ k: 'error', error: err instanceof ApiError ? err : new ApiError('network', String(err)) }),51 );52 };5354 const fieldProps = (field: Field) => ({55 name: field,56 'aria-invalid': Boolean(errs[field]),57 'aria-describedby': errs[field] ? `${id}-${field}-e` : undefined,58 onBlur: onBlur(field),59 });60 const err = (field: Field) => <p id={`${id}-${field}-e`} className="field-error" role="alert">{errs[field] ?? ''}</p>;6162 return (63 <div className="wrap page">64 <header className="doc-head removal-head">65 <p className="eyebrow">SPBoucherBot</p>66 <h1>{t('removal.title')}</h1>67 <p className="lead">{t('removal.lead')}</p>68 </header>69 <div className="g12 removal-grid">70 <div className="c-7">71 {phase.k === 'done' ? (72 <StateBlock icon="check" tone="ok" title={t('removal.ok')} role="status">73 <p className="label">{t('removal.ref')}</p>74 <p className="ref"><code>{phase.id}</code> <CopyButton plain text={phase.id} what={t('removal.ref')} /></p>75 <p>{t('removal.ok.p', { id: phase.id })}</p>76 <p><button type="button" className="btn" onClick={() => setPhase({ k: 'idle' })}>{t('removal.again')}</button></p>77 </StateBlock>78 ) : (79 <form className="panel panel-top removal-form" onSubmit={submit} noValidate aria-label={t('removal.form')}>80 <fieldset>81 <legend>{t('removal.scope')}</legend>82 <div className="choices">83 <label className="choice choice-card">84 <input type="radio" name="scope" value="url" checked={scope === 'url'} onChange={() => { setScope('url'); setErrs((p) => ({ ...p, target: undefined })); }} />85 {t('removal.scope.url')}86 </label>87 <label className="choice choice-card">88 <input type="radio" name="scope" value="domain" checked={scope === 'domain'} onChange={() => { setScope('domain'); setErrs((p) => ({ ...p, target: undefined })); }} />89 {t('removal.scope.domain')}90 </label>91 </div>92 </fieldset>93 <div className="field">94 <label htmlFor={`${id}-t`}>{scope === 'url' ? t('removal.target.url') : t('removal.target.domain')}</label>95 <input96 id={`${id}-t`}97 className="input-data"98 type={scope === 'url' ? 'url' : 'text'}99 inputMode="url"100 required101 autoCapitalize="off"102 autoComplete="off"103 spellCheck={false}104 maxLength={2048}105 placeholder={scope === 'url' ? 'https://exemple.org/page' : 'exemple.org'}106 {...fieldProps('target')}107 />108 {err('target')}109 </div>110 <div className="g12 removal-two">111 <div className="field c-6 m-6">112 <label htmlFor={`${id}-r`}>{t('removal.rel')}</label>113 <select id={`${id}-r`} name="relationship" required defaultValue="owner">114 <option value="owner">{t('removal.rel.owner')}</option>115 <option value="other">{t('removal.rel.other')}</option>116 </select>117 </div>118 <div className="field c-6 m-6">119 <label htmlFor={`${id}-e`}>{t('removal.email')}</label>120 <input id={`${id}-e`} type="email" required autoComplete="email" maxLength={254} {...fieldProps('email')} aria-describedby={errs.email ? `${id}-email-e` : `${id}-eh`} />121 {err('email')}122 <p id={`${id}-eh`} className="hint">{t('removal.email.h')}</p>123 </div>124 </div>125 <div className="field">126 <label htmlFor={`${id}-m`}>{t('removal.reason')}</label>127 <textarea id={`${id}-m`} required maxLength={2000} {...fieldProps('reason')} />128 {err('reason')}129 </div>130 <div className="row">131 <button type="submit" className="btn btn-primary btn-lg" disabled={phase.k === 'sending'}>{t('removal.submit')}<Icon name="arrow" size={18} /></button>132 </div>133 <div aria-live="polite">134 {phase.k === 'sending' && <Loading label={t('removal.sending')} />}135 {phase.k === 'error' && <ErrorBlock error={phase.error} title={t('removal.err')} />}136 </div>137 </form>138 )}139 </div>140 <aside className="c-5 removal-side">141 <div className="panel-ink inv">142 <h2 className="label">{t('removal.side.title')}</h2>143 <ol className="next-steps">144 <li>{t('removal.side.s1')}</li>145 <li>{t('removal.side.s2')}</li>146 <li>{t('removal.side.s3')}</li>147 </ol>148 </div>149 <div className="panel">150 <h2 className="label">{t('removal.side.robots')}</h2>151 <p>{t('removal.tip')}</p>152 <CodeBlock code={'User-agent: SPBoucherBot\nDisallow: /'} label="/robots.txt" />153 <p className="muted small">{t('id.contact')} : <a href={`mailto:${MAIL}`}>{MAIL}</a></p>154 </div>155 </aside>156 </div>157 </div>158 );159}160