import { useEffect, useRef, useState } from 'react'; import { useI18n } from '../i18n/I18nProvider'; // One field, one rule, shared by the two save flows that have to ask for a name: // the photo being filed and the look being filed as a recipe. `check` is where a // caller says a name is already taken — the reply is shown under the field and // nothing is saved until it comes back clean. export function NameModal({ title, label, initial, check, onSubmit, onClose, }: { title: string; label: string; initial: string; check?: (name: string) => string | null; onSubmit: (name: string) => void; onClose: () => void; }) { const { t } = useI18n(); const [name, setName] = useState(initial); const [problem, setProblem] = useState(null); const field = useRef(null); useEffect(() => field.current?.select(), []); const submit = (e: React.FormEvent) => { e.preventDefault(); const trimmed = name.trim(); if (!trimmed) { setProblem(t('name.required')); return; } const clash = check?.(trimmed) ?? null; if (clash) { setProblem(clash); return; } onSubmit(trimmed); }; return (
e.target === e.currentTarget && onClose()}>

{title}

{ setName(e.target.value); setProblem(null); }} /> {problem ? (

{problem}

) : null}
); }