import { useMemo, useState } from 'react' import type { AnatomicalSheetDocument, Dependent, ElementKey, } from '@faunosteo/core-wasm/types' import Button from '@/components/ui/Button' import Dialog from '@/components/ui/Dialog' import { dependentsOf } from '@/editor/core/editOps' import { refusalMessage } from '@/editor/refusal' import { elementLabel, useSheetDocument } from '@/editor/sheetDocument' import { useEditorStore } from '@/editor/state/editorStore' import { useT } from '@/i18n/useT' import { useLocaleStore } from '@/state/localeStore' export interface DeleteTarget { key: ElementKey /** The element's name as the list shows it. */ label: string } export interface DeleteDialogProps { /** null = closed. The dialog asks the core for the dependents each time it opens. */ target: DeleteTarget | null onClose: () => void } /** * Delete with dependents (shared by the Général, Références, Vertèbres, Vues and Constats * panels): the core lists what still names the element; if anything does, the dialog * shows the list and offers no deletion (the core would refuse it); descendants go with * the element and are listed in the confirmation. */ export default function DeleteDialog({ target, onClose }: DeleteDialogProps) { const t = useT() const doc = useSheetDocument() const heading = t('editor.delete.title').replace( '{name}', target?.label ?? '', ) return ( {target && doc && ( )} ) } function DeleteBody({ target, doc, onClose, }: { target: DeleteTarget doc: AnatomicalSheetDocument onClose: () => void }) { const t = useT() const [failure, setFailure] = useState(null) const found = useMemo( () => dependentsOf(useEditorStore.getState().documentText, target.key), [target.key], ) if (!found.ok) { return ( ) } const blocking = found.value.filter((d) => d.relation !== 'descendant') const descendants = found.value.filter((d) => d.relation === 'descendant') if (blocking.length > 0) { return ( <>

{t('editor.delete.blocked').replace('{name}', target.label)}

) } const confirm = () => { const outcome = useEditorStore .getState() .commit({ op: 'remove', key: target.key }) if (outcome.ok) onClose() else setFailure(refusalMessage(t, outcome)) } return ( <>

{t('editor.delete.ask').replace('{name}', target.label)}

{descendants.length > 0 && ( <>

{t('editor.delete.descendants')}

)} {failure && (

{failure}

)}
) } function Closing({ message, onClose, }: { message?: string onClose: () => void }) { const t = useT() return ( <> {message &&

{message}

}
) } function DependentList({ doc, dependents, withRelation = false, }: { doc: AnatomicalSheetDocument dependents: readonly Dependent[] withRelation?: boolean }) { const t = useT() const locale = useLocaleStore((s) => s.locale) return (
    {dependents.map((d) => (
  • {t(`editor.element.${d.element.kind}`)} «{' '} {elementLabel(doc, d.element, locale)} » {withRelation && <> : {t(`editor.relation.${d.relation}`)}}
  • ))}
) }