import { useState } from 'react' import FormField, { fieldDescribedBy } from '@/components/ui/FormField' import Button from '@/components/ui/Button' import Input from '@/components/ui/Input' import { changedFieldNames, fieldsOf, parseFields, sameAsStored, withVertexAfter, withoutVertex, type EditableGeometry, type GroupFields, } from '@/editor/annotations/geometryEdit' import { usePlural } from '@/editor/plural' import { useT } from '@/i18n/useT' /** What applying the group answers: kept, or the words that say why not. */ export type ApplyResult = { ok: true } | { ok: false; messages: string[] } export interface GeometryGroupProps { idPrefix: string /** The stored (or pending) geometry the fields start from. */ geometry: EditableGeometry /** Proposes the WHOLE group; the core's verdict comes back as the result. */ onApply: (geometry: EditableGeometry) => ApplyResult /** Fields shown as text: read-only mode or an unmeasured view. */ locked: boolean } type Fields = GroupFields type PolygonFields = Extract interface GroupState { key: string draft: Fields messages: string[] } /** The typed fields, reset whenever the stored value changes (undo, restore, a move). */ function useGroupState(geometry: EditableGeometry) { const key = JSON.stringify(fieldsOf(geometry)) const fresh = (): GroupState => ({ key, draft: fieldsOf(geometry), messages: [], }) const [state, setState] = useState(fresh) if (state.key !== key) setState(fresh()) return { state, set: (patch: Partial) => setState({ ...state, ...patch }), } } /** * The geometry fields as ONE group held in component state (plan « Repair through a * temporary form »): Enter or leaving a field proposes the whole group as one update; * a refusal keeps the typed values marked « non appliqué » with the core's message and * the document unchanged. Selecting another element unmounts it: an unapplied group was * never in the document and is dropped (no commit on unmount). */ export default function GeometryGroup(props: GeometryGroupProps) { const { state, set } = useGroupState(props.geometry) const propose = (next: Fields) => { if (sameAsStored(next, props.geometry)) return set({ draft: next, messages: [] }) const parsed = parseFields(next) if (!parsed.ok) return set({ draft: next, messages: ['number'] }) const result = props.onApply(parsed.geometry) set({ draft: next, messages: result.ok ? [] : result.messages }) } return ( set({ draft })} onPropose={propose} onRevert={() => set({ draft: fieldsOf(props.geometry), messages: [] })} /> ) } interface BodyProps extends Omit { draft: Fields pending: Set messages: string[] onChange: (fields: Fields) => void onPropose: (fields: Fields) => void onRevert: () => void } function GroupBody(props: BodyProps) { const t = useT() const { draft, pending, messages } = props const unapplied = pending.size > 0 && messages.length > 0 return (
{t( draft.type === 'circle' ? 'editor.annotations.geometry.circle' : 'editor.annotations.geometry.polygon', )} {draft.type === 'circle' ? ( ) : ( )} {unapplied && }
) } function Unapplied({ messages, onRevert }: BodyProps) { const t = useT() const words = messages.map((m) => m === 'number' ? t('editor.annotations.geometry.number') : m, ) return (

{t('editor.annotations.geometry.unapplied')}

{words.map((word) => (

{word}

))}
) } function NumberInput({ id, label, value, unapplied, locked, onType, onApply, }: { id: string label: string value: string unapplied: boolean locked: boolean onType: (value: string) => void onApply: () => void }) { const t = useT() if (locked) { return (

{label} {value}

) } const hint = unapplied ? t('editor.annotations.geometry.unapplied_tag') : undefined return ( onType(e.target.value)} onBlur={onApply} onKeyDown={(e) => { if (e.key === 'Enter' && !e.nativeEvent.isComposing) onApply() }} /> ) } type RowsProps = BodyProps & { fields: F } function CircleRows(props: RowsProps>) { const t = useT() const { fields, pending, locked, idPrefix } = props return (
{(['cx', 'cy', 'r'] as const).map((key) => ( props.onChange({ ...fields, [key]: value })} onApply={() => props.onPropose(fields)} /> ))}
) } function PolygonRows(props: RowsProps) { const [selected, setSelected] = useState(0) const { fields } = props const change = (index: number, axis: 'x' | 'y', value: string) => props.onChange({ type: 'polygon', rows: fields.rows.map((row, i) => i === index ? { ...row, [axis]: value } : row, ), }) const plural = usePlural() return (

{plural('editor.annotations.geometry.vertices', fields.rows.length)}

{fields.rows.map((row, index) => ( change(index, axis, value)} onFocus={() => setSelected(index)} /> ))} {!props.locked && ( )}
) } function VertexRow({ index, row, group, onType, onFocus, }: { index: number row: { x: string; y: string } group: RowsProps onType: (axis: 'x' | 'y', value: string) => void onFocus: () => void }) { const t = useT() return (
{index + 1} {(['x', 'y'] as const).map((axis) => ( onType(axis, value)} onApply={() => group.onPropose(group.fields)} /> ))}
) } function VertexButtons({ fields, selected, onApply, }: { fields: PolygonFields selected: number onApply: (fields: Fields) => void }) { const t = useT() return (
) }