import { useRef, useState, type RefObject } from 'react' import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types' import DiscardConfirm from '@/components/editor/findings/DiscardConfirm' import ReadOnlyNotice from '@/components/editor/findings/ReadOnlyNotice' import Button from '@/components/ui/Button' import Dialog from '@/components/ui/Dialog' import { creationOpener } from '@/editor/annotations/creationAsk' import { confirmAttempt, createAttempt, type Attempt, } from '@/editor/annotations/creationAttempt' import { dropCreation, finishCreation, patchForm, replacePoint, resumeTrace, setCorrection, } from '@/editor/annotations/creationWork' import { answerDrawing, guardDrawing } from '@/editor/annotations/drawingGuard' import type { MissingField } from '@/editor/annotations/missingFields' import { useAnnotationWork, type Creation, } from '@/editor/annotations/workStore' import { visibleElement } from '@/editor/focusRequest' import { CANVAS_BOX } from '@/editor/useEditorNavigation' import { usePlaceVisible } from '@/lib/placeVisibility' import { useT } from '@/i18n/useT' import { useLocaleStore } from '@/state/localeStore' import { pluralKey } from '@/editor/plural' import EditorGlyph from '../EditorGlyph' import CorrectionNotice from './CorrectionNotice' import CreationFields from './CreationFields' import MissingSummary, { focusFirstMissing } from './MissingSummary' const ID_PREFIX = 'creation-' export interface CreationDialogProps { doc: AnatomicalSheetDocument readOnly: boolean /** The column the dialog sits over (A2); null: centred. */ anchor: RefObject } /** What « Créer » left on screen: the fields R-3 names, or the core's refusal. */ interface Outcome { missing: MissingField[] messages: string[] } const NONE: Outcome = { missing: [], messages: [] } /** * « Créer » and « Utiliser cette position »: one `insertAnnotation` under `accept` (one * undo); a point lets the core's verdict offer C (C26); a success ends the creation. */ function useCreate(work: Creation, created: { current: boolean }) { const context = { t: useT(), locale: useLocaleStore((s) => s.locale) } const [outcome, setOutcome] = useState(NONE) const settle = (attempt: Attempt) => { if (attempt.status === 'invalid') { setOutcome({ missing: attempt.missing, messages: [] }) return focusFirstMissing(ID_PREFIX, attempt.missing) } setOutcome({ missing: [], messages: attempt.status === 'refused' ? attempt.messages : [], }) if (attempt.status === 'correction') setCorrection(attempt.correction) if (attempt.status !== 'created') return created.current = true if (attempt.id) finishCreation(attempt.id, work.pending.kind) else dropCreation() } return { outcome, create: () => settle(createAttempt({ ...work, context }, work.form)), confirm: () => { const { correction } = work.pending if (!correction) return settle(confirmAttempt({ ...work, context }, work.form, correction)) }, } } /** « Point placé · Replacer le point », « Zone tracée, 5 sommets · Reprendre le tracé ». */ const GLYPH = { point: 'point', zone: 'zone', detail_trigger: 'link' } as const /** « Point placé », « Zone tracée, 5 sommets », « Accès tracé, 3 sommets ». */ function useShapeText(pending: Creation['pending']): string { const t = useT() const locale = useLocaleStore((s) => s.locale) const { kind, geometry } = pending if (geometry.type !== 'polygon') return t('editor.annotations.creation.placed') const count = geometry.points.length const key = `editor.annotations.creation.traced.${kind}` return t(pluralKey(locale, key, count)).replace('{count}', String(count)) } function ShapeLine({ work, readOnly }: { work: Creation; readOnly: boolean }) { const t = useT() const text = useShapeText(work.pending) const point = work.pending.kind === 'point' return (
{text}
) } /** Board 02: « Abandonner » asks first (A12), « Créer » inserts; the question in its place. */ function CreationActions({ readOnly, onDiscard, }: { readOnly: boolean onDiscard: () => void }) { const t = useT() const asking = useAnnotationWork((s) => s.question?.work === 'creation') if (asking) { return ( answerDrawing(false)} onDiscard={() => answerDrawing(true)} /> ) } return (
) } /** The guard is always armed (A12): ✕, Échap and « Abandonner » ask, even with nothing typed. */ const askDiscard = () => guardDrawing(() => undefined) function CreationBody({ work, doc, readOnly, created, }: { work: Creation doc: AnatomicalSheetDocument readOnly: boolean created: { current: boolean } }) { const creating = useCreate(work, created) const { outcome } = creating return (
{ event.preventDefault() if (!readOnly) creating.create() }} > {readOnly && } {work.pending.correction && ( !readOnly && creating.confirm()} onCancel={() => setCorrection(null)} /> )} {outcome.messages.length > 0 && (
{outcome.messages.map((text) => (

{text}

))}
)} ) } /** * Where the focus goes once the dialog closes (board 32): nowhere after « Créer » (the new * row takes it), the strip's « Revenir aux propriétés » while the shape is replaced or * retraced, else what started the creation. */ function useCreationReturn(work: Creation | null) { const last = useRef(null) const created = useRef(false) // A new creation starts with no « Créer » behind it. if (work && last.current === null) created.current = false last.current = work ?? last.current const returnFocus = () => { const ended = last.current if (useAnnotationWork.getState().creation) { return ( visibleElement('[data-back-to-properties]') ?? visibleElement(CANVAS_BOX) ) } last.current = null if (created.current || !ended) return null return creationOpener(ended) } return { created, returnFocus } } /** * « Nouveau point », « Nouvelle zone », « Nouvel accès » (board 02, 04): a modal without a * history layer over the right column, mounted outside the edit-only body so a read-only * flip keeps it with its form and ghost, « Créer » disabled (R-14). The shape and the * form are the work store's: they outlive « Replacer le point » and « Reprendre le tracé ». */ export default function CreationDialog(props: CreationDialogProps) { const t = useT() const visible = usePlaceVisible() const work = useAnnotationWork((s) => s.creation) const back = useCreationReturn(work) return ( (askDiscard() ? undefined : 'kept')} > {work && ( )} ) }