diff --git a/apps/app/src/components/editor/findings/FindingActions.tsx b/apps/app/src/components/editor/findings/FindingActions.tsx
index 1ceadc8c..3e51a0ad 100644
--- a/apps/app/src/components/editor/findings/FindingActions.tsx
+++ b/apps/app/src/components/editor/findings/FindingActions.tsx
@@ -63,7 +63,7 @@ interface ActionsProps {
/** The view mode's actions (edit tier only): « Modifier », review, order, « Supprimer ». */
export default function FindingActions(props: ActionsProps) {
- const { item, readOnly, edit, onEdit, onDelete } = props
+ const { item, readOnly, edit } = props
const t = useT()
return (
<>
@@ -73,7 +73,7 @@ export default function FindingActions(props: ActionsProps) {
readOnly={readOnly}
variant="secondary"
size="sm"
- onClick={onEdit}
+ onClick={props.onEdit}
>
{t('editor.findings.edit')}
@@ -90,7 +90,7 @@ export default function FindingActions(props: ActionsProps) {
readOnly={readOnly}
variant="danger"
size="sm"
- onClick={onDelete}
+ onClick={props.onDelete}
>
{t('editor.action.delete')}
diff --git a/apps/app/src/components/editor/findings/FindingCitations.tsx b/apps/app/src/components/editor/findings/FindingCitations.tsx
index 97d8f3bd..897dd395 100644
--- a/apps/app/src/components/editor/findings/FindingCitations.tsx
+++ b/apps/app/src/components/editor/findings/FindingCitations.tsx
@@ -17,6 +17,9 @@ interface CitationsProps {
onShowReferences: (opener: HTMLElement) => void
}
+const PILL =
+ 'inline-flex min-h-11 items-center gap-1 rounded-full border px-3 text-sm ' +
+ 'border-[var(--color-border)]'
const RING =
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]'
@@ -37,7 +40,7 @@ function SheetTrigger({
aria-expanded={sheetOpen}
aria-controls="editor-citations-panel"
onClick={(event) => onShowReferences(event.currentTarget)}
- className={`inline-flex min-h-11 items-center gap-1 rounded-full border border-[var(--color-border)] px-3 text-sm ${RING}`}
+ className={`${PILL} ${RING}`}
>
{plural('editor.findings.references_open', count)}
@@ -79,6 +82,28 @@ function CitationList({
)
}
+/** The finding's cited ids, the update that changes them and the references still citable. */
+function useCitationEdit(item: FindingItem, edit: EditCommandApi) {
+ const t = useT()
+ const doc = useSheetDocument()
+ const ids =
+ item.element.kind === 'zone' ? [] : item.element.value.reference_ids
+ const patch = (reference_ids: string[]) =>
+ edit.run('references', {
+ op: 'update',
+ key: item.key,
+ element: patchElement(item, { reference_ids }),
+ })
+ const options = (doc?.references ?? [])
+ .filter((r) => !ids.includes(r.id))
+ .map((r) => ({ value: r.id, label: r.citation }))
+ return {
+ ids,
+ patch,
+ options: [{ value: '', label: t('editor.field.choose') }, ...options],
+ }
+}
+
/**
* « Références citées » of a category or a node. A finding node shows « X références »
* (the sheet), a family its list. « Citer une référence » acts at once, one entry in the
@@ -87,40 +112,28 @@ function CitationList({
export default function FindingCitations(props: CitationsProps) {
const { item, readOnly, edit } = props
const t = useT()
- const doc = useSheetDocument()
- const { element } = item
- if (element.kind === 'zone') return null
- const ids = element.value.reference_ids
- const patch = (reference_ids: string[]) =>
- edit.run('references', {
- op: 'update',
- key: item.key,
- element: patchElement(item, { reference_ids }),
- })
- const options = [
- { value: '', label: t('editor.field.choose') },
- ...(doc?.references ?? [])
- .filter((r) => !ids.includes(r.id))
- .map((r) => ({ value: r.id, label: r.citation })),
- ]
+ const cited = useCitationEdit(item, edit)
+ if (item.element.kind === 'zone') return null
return (
{t('editor.findings.references')}
- {ids.length > 0 && element.kind === 'finding_node' && (
-
+ {cited.ids.length > 0 && item.element.kind === 'finding_node' && (
+
)}
- {ids.length === 0 && (
+ {cited.ids.length === 0 && (
{t('editor.findings.references_none')}
)}
- {element.kind === 'finding_category' && (
+ {item.element.kind === 'finding_category' && (
patch(ids.filter((other) => other !== id))}
+ remove={(id) =>
+ cited.patch(cited.ids.filter((other) => other !== id))
+ }
/>
)}
{!readOnly && (
@@ -128,8 +141,8 @@ export default function FindingCitations(props: CitationsProps) {
id="finding-add-reference"
label={t('editor.findings.reference_add')}
value=""
- options={options}
- onCommit={(id) => id !== '' && patch([...ids, id])}
+ options={cited.options}
+ onCommit={(id) => id !== '' && cited.patch([...cited.ids, id])}
error={edit.fieldError('references')}
/>
)}
diff --git a/apps/app/src/components/editor/findings/FindingCreateForm.tsx b/apps/app/src/components/editor/findings/FindingCreateForm.tsx
index b827b3e0..a2b73a35 100644
--- a/apps/app/src/components/editor/findings/FindingCreateForm.tsx
+++ b/apps/app/src/components/editor/findings/FindingCreateForm.tsx
@@ -16,7 +16,7 @@ import {
} from '@/editor/findings/tree'
import type { IdPrefix } from '@/editor/ids'
import { isBlank, useSheetDocument } from '@/editor/sheetDocument'
-import { useEditCommand } from '@/editor/useEditCommand'
+import { useEditCommand, type EditCommandApi } from '@/editor/useEditCommand'
import type { Guards } from '@/editor/useEditorNavigation'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
@@ -60,26 +60,36 @@ function isComplete(draft: Draft, pending: PendingFinding, parent: unknown) {
return pending.kind !== 'category' || (parent !== null && draft.kind !== '')
}
+/** The one `insert`, under a fresh id; the new id, or null when the core refused it. */
+function insertNew(
+ edit: EditCommandApi,
+ pending: PendingFinding,
+ parent: FindingItem | null,
+ draft: Draft,
+): string | null {
+ let createdId = ''
+ const name = { fr: draft.fr.trim(), en: draft.en.trim() }
+ const added = edit.runNew('form', PREFIX[pending.kind], (id) => {
+ createdId = id
+ return insertCommand({ ...pending, parent }, id, name, draft.kind)
+ })
+ return added ? createdId : null
+}
+
/** The form's state and its one `insert` (« Créer »), refused on a missing field. */
function useCreateForm(props: FindingCreateFormProps) {
- const { pending, guards, onCreated } = props
const edit = useEditCommand()
const zones = useZoneChoice()
- const form = useFormDraft(EMPTY, guards, sameDraft)
+ const form = useFormDraft(EMPTY, props.guards, sameDraft)
const create = () => {
+ const { pending } = props
const parent = pending.kind === 'category' ? zones.chosen : pending.parent
if (!isComplete(form.draft, pending, parent)) {
edit.reject('form', 'editor.findings.required')
return
}
- const { fr, en, kind } = form.draft
- let createdId = ''
- const added = edit.runNew('form', PREFIX[pending.kind], (id) => {
- createdId = id
- const name = { fr: fr.trim(), en: en.trim() }
- return insertCommand({ ...pending, parent }, id, name, kind)
- })
- if (added) onCreated(createdId, parent)
+ const id = insertNew(edit, pending, parent, form.draft)
+ if (id !== null) props.onCreated(id, parent)
}
return { ...form, zones, create, error: edit.fieldError('form') }
}
@@ -129,11 +139,9 @@ function CreateFields(props: {
* is written before « Créer », a close or a move never creates (R2F-3).
*/
export default function FindingCreateForm(props: FindingCreateFormProps) {
- const { pending, readOnly, onClose } = props
const t = useT()
const form = useCreateForm(props)
- const { guard } = form
- const category = pending.kind === 'category'
+ const category = props.pending.kind === 'category'
return (
)
diff --git a/apps/app/src/components/editor/findings/FindingDialog.tsx b/apps/app/src/components/editor/findings/FindingDialog.tsx
index 8a8499bd..ca70274f 100644
--- a/apps/app/src/components/editor/findings/FindingDialog.tsx
+++ b/apps/app/src/components/editor/findings/FindingDialog.tsx
@@ -46,43 +46,50 @@ function useFocusEditButton(editing: boolean) {
}
}
+/** 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({ item, layer, readOnly, onDelete }: ItemBodyProps) {
+function ItemBody(props: ItemBodyProps) {
+ const { layer } = props
const t = useT()
- const [editing, setEditing] = useState(false)
- const [saved, setSaved] = useState(false)
- const returnToEdit = useFocusEditButton(editing)
- const { view } = layer
- const sheetOpen = view !== null && 'key' in view && view.citations
+ const mode = useItemMode()
+ const sheetOpen = !!layer.view && 'key' in layer.view && layer.view.citations
return (
- {editing ? (
+ {mode.editing ? (
{
- returnToEdit()
- setSaved(wrote)
- setEditing(false)
- }}
+ onDone={mode.done}
onDiscardClose={() => layer.close(true)}
/>
) : (
{
- setSaved(false)
- setEditing(true)
- }}
- onDelete={() => onDelete(item)}
- onShowReferences={(opener) => layer.openCitations(item.key, opener)}
+ onEdit={mode.startEdit}
+ onDelete={() => props.onDelete(props.item)}
+ onShowReferences={(opener) =>
+ layer.openCitations(props.item.key, opener)
+ }
/>
)}
- {saved ? t('editor.findings.saved') : ''}
+ {mode.saved ? t('editor.findings.saved') : ''}
)
@@ -94,37 +101,37 @@ function ItemBody({ item, layer, readOnly, onDelete }: ItemBodyProps) {
* place is shown (no native modal inside a hidden part).
*/
export default function FindingDialog(props: FindingDialogProps) {
- const { layer, item, pending, readOnly } = props
+ const { pending } = props
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const visible = usePlaceVisible()
const title = pending
? t(`editor.findings.new.${pending.kind}`)
- : (item?.name[locale] ?? '')
+ : (props.item?.name[locale] ?? '')
return (