import { useState, type Dispatch } from 'react' import type { AnatomicalSheetDocument } from '@faunosteo/core-wasm/types' import Button from '@/components/ui/Button' import type { CommitContext } from '@/editor/annotations/annotationCommit' import { emptyForm, withGeometry, type CreationForm, type PendingElement, } from '@/editor/annotations/creation' import { confirmAttempt, createAttempt, type Attempt, type CreationInput, } from '@/editor/annotations/creationAttempt' import type { Frame } from '@/editor/annotations/frame' import type { UiAction } from '@/editor/annotations/uiState' import { useT } from '@/i18n/useT' import CorrectionNotice from './CorrectionNotice' import CreationFields from './CreationFields' import GeometryGroup from './GeometryGroup' export interface CreationPanelProps { pending: PendingElement viewId: string doc: AnatomicalSheetDocument /** F of the current view; null while it is not measured. */ frame: Frame | null dispatch: Dispatch context: CommitContext } /** What an attempt leaves on screen: the core's words, or the form's own. */ function messagesOf(attempt: Attempt, words: (problem: string) => string) { if (attempt.status === 'refused') return attempt.messages return attempt.status === 'invalid' ? [words(attempt.problem)] : [] } /** * The typed fields and the outcome of « Créer » / « Utiliser cette position ». The form * survives a change of the pending geometry (a second click): the panel is not remounted. */ function useCreation(props: CreationPanelProps) { const t = useT() const [form, setForm] = useState(() => emptyForm(props.doc)) const [messages, setMessages] = useState([]) const settle = (attempt: Attempt) => { const { dispatch, pending } = props setMessages( messagesOf(attempt, (p) => t(`editor.annotations.required.${p}`)), ) if (attempt.status === 'created') { dispatch({ type: 'select', id: attempt.id ?? null }) } if (attempt.status === 'correction') { const next = { ...pending, correction: attempt.correction } dispatch({ type: 'pending_update', pending: next }) } } const input: CreationInput = props return { form, messages, patch: (change: Partial) => setForm({ ...form, ...change }), create: () => settle(createAttempt(input, form)), confirm: () => { const { correction } = props.pending if (correction) settle(confirmAttempt(input, form, correction)) }, } } /** * « Nouveau point / Nouvelle zone / Nouvel accès »: the fields the core requires are * collected first, then « Créer » inserts once through `accept` (no default labels). * The geometry stays here, outside the document, until then. */ export default function CreationPanel(props: CreationPanelProps) { const t = useT() const creation = useCreation(props) const { pending, dispatch } = props const title = t(`editor.annotations.new.${pending.kind}`) return (

{title}

{ const next = withGeometry(pending, geometry) dispatch({ type: 'pending_update', pending: next }) return { ok: true } }} /> {pending.correction && ( dispatch({ type: 'pending_update', pending: { ...pending, correction: null }, }) } /> )} dispatch({ type: 'pending_drop' })} />
) } function CreationActions({ onCreate, onCancel, }: { onCreate: () => void onCancel: () => void }) { const t = useT() return (
) } function Messages({ messages }: { messages: string[] }) { if (messages.length === 0) return null return (
{messages.map((message) => (

{message}

))}
) }