A modal Dialog replaces the citations side sheet; families open it too (isCitable). The finding dialog shows only « Références citées » and « X références », also at zero; its edit form no longer carries reference_ids. A dirty form refusing « Aller à » or « Tout afficher » closes the clean references dialog first.
144 lines
4.6 KiB
TypeScript
144 lines
4.6 KiB
TypeScript
import { useEffect, useRef, useState } from 'react'
|
|
import FindingCreateForm, {
|
|
type PendingFinding,
|
|
} from '@/components/editor/findings/FindingCreateForm'
|
|
import FindingEditForm from '@/components/editor/findings/FindingEditForm'
|
|
import FindingView from '@/components/editor/findings/FindingView'
|
|
import Dialog from '@/components/ui/Dialog'
|
|
import { usePlaceVisible } from '@/lib/placeVisibility'
|
|
import type { FindingItem, FindingKind } from '@/editor/findings/tree'
|
|
import type { FindingLayer } from '@/editor/tabs'
|
|
import { useT } from '@/i18n/useT'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
|
|
export interface FindingDialogProps {
|
|
layer: FindingLayer
|
|
/** The element the dialog shows; null while it creates one or is closed. */
|
|
item: FindingItem | null
|
|
/** The creation the dialog holds, null for the view of an element. */
|
|
pending: PendingFinding | null
|
|
readOnly: boolean
|
|
/** Where the focus goes once the dialog is closed (the tree row, the add button). */
|
|
returnFocus: () => HTMLElement | null
|
|
onDelete: (item: FindingItem) => void
|
|
onCreated: (id: string, parent: FindingItem | null, kind: FindingKind) => void
|
|
}
|
|
|
|
interface ItemBodyProps {
|
|
item: FindingItem
|
|
layer: FindingLayer
|
|
readOnly: boolean
|
|
onDelete: (item: FindingItem) => void
|
|
}
|
|
|
|
const EDIT_BUTTON_ID = 'finding-edit-button'
|
|
|
|
/** After the form closes its button is gone: « Modifier » takes the focus back. */
|
|
function useFocusEditButton(editing: boolean) {
|
|
const wanted = useRef(false)
|
|
useEffect(() => {
|
|
if (editing || !wanted.current) return
|
|
wanted.current = false
|
|
document.getElementById(EDIT_BUTTON_ID)?.focus()
|
|
}, [editing])
|
|
return () => {
|
|
wanted.current = true
|
|
}
|
|
}
|
|
|
|
/** The local mode of the one dialog: view or edit, and whether a save was just made. */
|
|
function useItemMode() {
|
|
const [mode, setMode] = useState({ editing: false, saved: false })
|
|
const returnToEdit = useFocusEditButton(mode.editing)
|
|
return {
|
|
...mode,
|
|
startEdit: () => setMode({ editing: true, saved: false }),
|
|
done: (saved: boolean) => {
|
|
returnToEdit()
|
|
setMode({ editing: false, saved })
|
|
},
|
|
}
|
|
}
|
|
|
|
/** View mode, or the edit form: a local mode of the one dialog, no history entry. */
|
|
function ItemBody(props: ItemBodyProps) {
|
|
const { layer } = props
|
|
const t = useT()
|
|
const mode = useItemMode()
|
|
const citationsOpen =
|
|
!!layer.view && 'key' in layer.view && layer.view.citations
|
|
const showReferences = (opener: HTMLElement) =>
|
|
layer.openCitations(props.item.key, opener)
|
|
return (
|
|
<div className="space-y-2">
|
|
{mode.editing ? (
|
|
<FindingEditForm
|
|
item={props.item}
|
|
readOnly={props.readOnly}
|
|
guards={layer.guards}
|
|
onDone={mode.done}
|
|
onDiscardClose={() => layer.close(true)}
|
|
citationsOpen={citationsOpen}
|
|
onShowReferences={showReferences}
|
|
/>
|
|
) : (
|
|
<FindingView
|
|
item={props.item}
|
|
readOnly={props.readOnly}
|
|
citationsOpen={citationsOpen}
|
|
onEdit={mode.startEdit}
|
|
onDelete={() => props.onDelete(props.item)}
|
|
onShowReferences={showReferences}
|
|
/>
|
|
)}
|
|
<p role="status" className="text-sm text-[var(--color-ink-muted)]">
|
|
{mode.saved ? t('editor.findings.saved') : ''}
|
|
</p>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
/**
|
|
* The finding dialog (R2F-3): one `ModalDialog` kept across its modes (view, edit,
|
|
* creation). The view layer of the browser history drives it; it opens only while its
|
|
* place is shown (no native modal inside a hidden part).
|
|
*/
|
|
export default function FindingDialog(props: FindingDialogProps) {
|
|
const { pending } = props
|
|
const t = useT()
|
|
const locale = useLocaleStore((s) => s.locale)
|
|
const visible = usePlaceVisible()
|
|
const title = pending
|
|
? t(`editor.findings.new.${pending.kind}`)
|
|
: (props.item?.name[locale] ?? '')
|
|
return (
|
|
<Dialog
|
|
open={visible && (props.item !== null || pending !== null)}
|
|
layer
|
|
size="lg"
|
|
title={title}
|
|
returnFocus={props.returnFocus}
|
|
onClose={() => (props.layer.close() ? undefined : 'kept')}
|
|
>
|
|
{pending && (
|
|
<FindingCreateForm
|
|
pending={pending}
|
|
readOnly={props.readOnly}
|
|
guards={props.layer.guards}
|
|
onCreated={(id, parent) => props.onCreated(id, parent, pending.kind)}
|
|
onClose={(forced) => props.layer.close(forced)}
|
|
/>
|
|
)}
|
|
{props.item && (
|
|
<ItemBody
|
|
key={props.item.id}
|
|
item={props.item}
|
|
layer={props.layer}
|
|
readOnly={props.readOnly}
|
|
onDelete={props.onDelete}
|
|
/>
|
|
)}
|
|
</Dialog>
|
|
)
|
|
}
|