Files
faunosteo/apps/app/src/components/editor/DeleteDialog.tsx
T

168 lines
4.5 KiB
TypeScript

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 (
<Dialog
open={target !== null && doc !== null}
onClose={onClose}
title={heading}
>
{target && doc && (
<DeleteBody target={target} doc={doc} onClose={onClose} />
)}
</Dialog>
)
}
function DeleteBody({
target,
doc,
onClose,
}: {
target: DeleteTarget
doc: AnatomicalSheetDocument
onClose: () => void
}) {
const t = useT()
const [failure, setFailure] = useState<string | null>(null)
const found = useMemo(
() => dependentsOf(useEditorStore.getState().documentText, target.key),
[target.key],
)
if (!found.ok) {
return (
<Closing message={t('editor.delete.lookup_failed')} onClose={onClose} />
)
}
const blocking = found.value.filter((d) => d.relation !== 'descendant')
const descendants = found.value.filter((d) => d.relation === 'descendant')
if (blocking.length > 0) {
return (
<>
<p className="text-sm">
{t('editor.delete.blocked').replace('{name}', target.label)}
</p>
<DependentList doc={doc} dependents={blocking} withRelation />
<Closing onClose={onClose} />
</>
)
}
const confirm = () => {
const outcome = useEditorStore
.getState()
.commit({ op: 'remove', key: target.key })
if (outcome.ok) onClose()
else setFailure(refusalMessage(t, outcome))
}
return (
<>
<p className="text-sm">
{t('editor.delete.ask').replace('{name}', target.label)}
</p>
{descendants.length > 0 && (
<>
<p className="mt-2 text-sm">{t('editor.delete.descendants')}</p>
<DependentList doc={doc} dependents={descendants} />
</>
)}
{failure && (
<p
role="alert"
className="mt-2 text-sm text-[var(--color-danger-strong)]"
>
{failure}
</p>
)}
<div className="mt-4 flex flex-wrap justify-end gap-2">
<Button type="button" variant="ghost" onClick={onClose}>
{t('editor.delete.cancel')}
</Button>
<Button type="button" variant="danger" onClick={confirm}>
{t('editor.delete.confirm')}
</Button>
</div>
</>
)
}
function Closing({
message,
onClose,
}: {
message?: string
onClose: () => void
}) {
const t = useT()
return (
<>
{message && <p className="text-sm">{message}</p>}
<div className="mt-4 flex justify-end">
<Button type="button" variant="secondary" onClick={onClose}>
{t('editor.delete.close')}
</Button>
</div>
</>
)
}
function DependentList({
doc,
dependents,
withRelation = false,
}: {
doc: AnatomicalSheetDocument
dependents: readonly Dependent[]
withRelation?: boolean
}) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
return (
<ul className="mt-2 list-disc space-y-1 pl-5 text-sm">
{dependents.map((d) => (
<li key={JSON.stringify(d.element)}>
{t(`editor.element.${d.element.kind}`)} «{' '}
{elementLabel(doc, d.element, locale)} »
{withRelation && <> : {t(`editor.relation.${d.relation}`)}</>}
</li>
))}
</ul>
)
}