feat(app): editor Constats tree with review states and reorder

This commit is contained in:
bchanot committed 2026-11-04 13:45:34 +01:00
1 parent 66663dc0ca
commit 92c595caa1
11 files changed
+1219 -4

No files matched your search

@@ -0,0 +1,163 @@
import { useState } from 'react'
import type {
EditCommand,
FindingCategoryKind,
FindingNode,
} from '@faunosteo/core-wasm/types'
import ActionButton from '@/components/editor/fields/ActionButton'
import DraftField from '@/components/editor/fields/DraftField'
import SelectField from '@/components/editor/fields/SelectField'
import type { FindingItem, FindingKind } from '@/editor/findings/tree'
import type { IdPrefix } from '@/editor/ids'
import { isBlank } from '@/editor/sheetDocument'
import { useEditCommand } from '@/editor/useEditCommand'
import { useT } from '@/i18n/useT'
export const CATEGORY_KINDS: readonly FindingCategoryKind[] = [
'dysfunction',
'consultation_reason',
'anamnesis',
'custom',
]
export interface PendingFinding {
kind: FindingKind
/** The zone, category or node the new element goes under; null for a zone. */
parent: FindingItem | null
}
/**
* Collects what the core requires (names FR/EN, the kind of a category), then one
* `insert`: no element exists with default labels.
*/
export default function FindingCreateForm({
pending,
onCreated,
onCancel,
}: {
pending: PendingFinding
onCreated: (id: string) => void
onCancel: () => void
}) {
const t = useT()
const edit = useEditCommand()
const [fr, setFr] = useState('')
const [en, setEn] = useState('')
const [kind, setKind] = useState('')
const create = () => {
const incomplete =
isBlank(fr) || isBlank(en) || (pending.kind === 'category' && !kind)
if (incomplete) {
edit.reject('form', 'editor.findings.required')
return
}
let createdId = ''
const added = edit.runNew('form', PREFIX[pending.kind], (id) => {
createdId = id
return insertCommand(pending, id, { fr, en }, kind)
})
if (added) onCreated(createdId)
}
return (
<div className="space-y-2 rounded-lg border border-dashed border-[var(--color-border)] p-3">
<p className="text-sm font-medium">
{t(`editor.findings.new.${pending.kind}`)}
</p>
<div className="grid gap-3 md:grid-cols-2">
{pending.kind === 'category' && (
<SelectField
id="finding-new-kind"
label={t('editor.findings.kind')}
value={kind}
options={[
{ value: '', label: t('editor.field.choose') },
...CATEGORY_KINDS.map((k) => ({
value: k,
label: t(`editor.findings.kind.${k}`),
})),
]}
onCommit={setKind}
/>
)}
<DraftField
id="finding-new-name-fr"
label={t('editor.general.name_fr')}
value={fr}
onChange={setFr}
/>
<DraftField
id="finding-new-name-en"
label={t('editor.general.name_en')}
value={en}
onChange={setEn}
/>
</div>
{edit.fieldError('form') && (
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
{edit.fieldError('form')}
</p>
)}
<div className="flex gap-2">
<ActionButton readOnly={false} variant="secondary" onClick={create}>
{t('editor.findings.create')}
</ActionButton>
<ActionButton readOnly={false} variant="ghost" onClick={onCancel}>
{t('editor.findings.cancel')}
</ActionButton>
</div>
</div>
)
}
const PREFIX: Record<FindingKind, IdPrefix> = {
zone: 'zone',
category: 'fcat',
node: 'node',
}
function insertCommand(
pending: PendingFinding,
id: string,
name: { fr: string; en: string },
kind: string,
): EditCommand {
const parent = pending.parent?.key
if (pending.kind === 'zone') {
return {
op: 'insert',
element: { kind: 'zone', value: { id, name, categories: [] } },
}
}
if (pending.kind === 'category') {
return {
op: 'insert',
parent: { kind: 'zone', id: pending.parent?.zoneId ?? '' },
element: {
kind: 'finding_category',
value: {
id,
kind: kind as FindingCategoryKind,
name,
description: {},
reference_ids: [],
review_status: 'to_confirm',
children: [],
},
},
}
}
const node: FindingNode = {
id,
name,
description: {},
requires_vet_referral: false,
reference_ids: [],
review_status: 'to_confirm',
children: [],
}
return {
op: 'insert',
parent,
element: { kind: 'finding_node', value: node },
}
}