feat(app): editor Constats tree with review states and reorder
This commit is contained in:
1 parent
66663dc0ca
commit
92c595caa1
11 files changed
+1219
-4
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import type { PendingFinding } from '@/components/editor/findings/FindingCreateForm'
|
||||
import { canAddNode, type FindingItem } from '@/editor/findings/tree'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
/** « Ajouter une zone / une catégorie / un constat »: each starts a creation form. */
|
||||
export default function AddBar({
|
||||
selected,
|
||||
readOnly,
|
||||
onStart,
|
||||
}: {
|
||||
selected: FindingItem | undefined
|
||||
readOnly: boolean
|
||||
onStart: (pending: PendingFinding) => void
|
||||
}) {
|
||||
const t = useT()
|
||||
return (
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => onStart({ kind: 'zone', parent: null })}
|
||||
>
|
||||
{t('editor.findings.add_zone')}
|
||||
</ActionButton>
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
disabled={!selected}
|
||||
onClick={() => onStart({ kind: 'category', parent: selected ?? null })}
|
||||
>
|
||||
{t('editor.findings.add_category')}
|
||||
</ActionButton>
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
disabled={!canAddNode(selected)}
|
||||
onClick={() => onStart({ kind: 'node', parent: selected ?? null })}
|
||||
>
|
||||
{t('editor.findings.add_node')}
|
||||
</ActionButton>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -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 },
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
import type { EditCommand } from '@faunosteo/core-wasm/types'
|
||||
import ActionButton from '@/components/editor/fields/ActionButton'
|
||||
import MoveButtons from '@/components/editor/fields/MoveButtons'
|
||||
import TextField from '@/components/editor/fields/TextField'
|
||||
import StatusBadge from '@/components/editor/findings/StatusBadge'
|
||||
import { withName, type FindingItem } from '@/editor/findings/tree'
|
||||
import { isBlank } from '@/editor/sheetDocument'
|
||||
import { useEditCommand, type EditCommandApi } from '@/editor/useEditCommand'
|
||||
import type { Locale } from '@/i18n/types'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
export interface FindingDetailProps {
|
||||
item: FindingItem
|
||||
readOnly: boolean
|
||||
onDelete: () => void
|
||||
}
|
||||
|
||||
/** The selected element: its names, its review state and the actions on it. */
|
||||
export default function FindingDetail({
|
||||
item,
|
||||
readOnly,
|
||||
onDelete,
|
||||
}: FindingDetailProps) {
|
||||
const t = useT()
|
||||
const edit = useEditCommand()
|
||||
return (
|
||||
<div className="space-y-3 rounded-lg border border-[var(--color-border)] p-3">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<p className="text-sm font-medium">
|
||||
{t(`editor.findings.type.${item.kind}`)}
|
||||
</p>
|
||||
{item.status && <StatusBadge status={item.status} />}
|
||||
</div>
|
||||
<Names item={item} readOnly={readOnly} edit={edit} />
|
||||
<div className="flex flex-wrap items-center justify-end gap-1">
|
||||
<ReviewActions item={item} readOnly={readOnly} edit={edit} />
|
||||
<MoveButtons
|
||||
index={item.index}
|
||||
count={item.siblings}
|
||||
readOnly={readOnly}
|
||||
onMove={(position) =>
|
||||
edit.run('actions', { op: 'move', key: item.key, position })
|
||||
}
|
||||
/>
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="danger"
|
||||
size="sm"
|
||||
onClick={onDelete}
|
||||
>
|
||||
{t('editor.action.delete')}
|
||||
</ActionButton>
|
||||
</div>
|
||||
{edit.fieldError('actions') && (
|
||||
<p role="alert" className="text-sm text-[var(--color-danger-strong)]">
|
||||
{edit.fieldError('actions')}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function Names({
|
||||
item,
|
||||
readOnly,
|
||||
edit,
|
||||
}: {
|
||||
item: FindingItem
|
||||
readOnly: boolean
|
||||
edit: EditCommandApi
|
||||
}) {
|
||||
const t = useT()
|
||||
const rename = (locale: Locale) => (value: string) =>
|
||||
isBlank(value)
|
||||
? edit.reject(locale, 'editor.field.required')
|
||||
: edit.run(locale, {
|
||||
op: 'update',
|
||||
key: item.key,
|
||||
element: withName(item, { ...item.name, [locale]: value }),
|
||||
})
|
||||
return (
|
||||
<div className="grid gap-3 md:grid-cols-2">
|
||||
{(['fr', 'en'] as const).map((locale) => (
|
||||
<TextField
|
||||
key={`${item.id}-${locale}`}
|
||||
id={`finding-name-${locale}`}
|
||||
label={t(`editor.general.name_${locale}`)}
|
||||
value={item.name[locale]}
|
||||
onCommit={rename(locale)}
|
||||
readOnly={readOnly}
|
||||
error={edit.fieldError(locale)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function statusCommand(
|
||||
item: FindingItem,
|
||||
status: 'reviewed' | 'to_confirm',
|
||||
): EditCommand {
|
||||
const wholeBranch = status === 'reviewed' && item.kind !== 'node'
|
||||
return wholeBranch
|
||||
? { op: 'mark_reviewed', key: item.key }
|
||||
: { op: 'set_status', key: item.key, status }
|
||||
}
|
||||
|
||||
function ReviewActions({
|
||||
item,
|
||||
readOnly,
|
||||
edit,
|
||||
}: {
|
||||
item: FindingItem
|
||||
readOnly: boolean
|
||||
edit: EditCommandApi
|
||||
}) {
|
||||
const t = useT()
|
||||
const canMark = item.kind === 'zone' || item.status !== 'reviewed'
|
||||
const canReset = item.kind !== 'zone' && item.status === 'reviewed'
|
||||
return (
|
||||
<>
|
||||
{canMark && (
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => edit.run('actions', statusCommand(item, 'reviewed'))}
|
||||
>
|
||||
{t('editor.findings.mark_reviewed')}
|
||||
</ActionButton>
|
||||
)}
|
||||
{canReset && (
|
||||
<ActionButton
|
||||
readOnly={readOnly}
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => edit.run('actions', statusCommand(item, 'to_confirm'))}
|
||||
>
|
||||
{t('editor.findings.reset_to_confirm')}
|
||||
</ActionButton>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
import { useEffect, useRef, type KeyboardEvent } from 'react'
|
||||
import StatusBadge from '@/components/editor/findings/StatusBadge'
|
||||
import { keyMove, type FindingItem } from '@/editor/findings/tree'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
export interface FindingTreeProps {
|
||||
visible: readonly FindingItem[]
|
||||
selectedId: string | null
|
||||
expanded: ReadonlySet<string>
|
||||
onSelect: (id: string) => void
|
||||
onToggle: (id: string, open: boolean) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* The findings tree (WAI-ARIA tree pattern): one tab stop, arrows move and open, the
|
||||
* selected item is the one the actions below act on. Items are a flat list with
|
||||
* `aria-level`, so the markup stays valid whatever the depth.
|
||||
*/
|
||||
export default function FindingTree(props: FindingTreeProps) {
|
||||
const t = useT()
|
||||
const refs = useRef(new Map<string, HTMLElement>())
|
||||
const focusAfter = useRef<string | null>(null)
|
||||
const { visible, selectedId, expanded, onSelect, onToggle } = props
|
||||
useEffect(() => {
|
||||
if (focusAfter.current) refs.current.get(focusAfter.current)?.focus()
|
||||
focusAfter.current = null
|
||||
})
|
||||
const current = visible.find((item) => item.id === selectedId) ?? visible[0]
|
||||
const onKeyDown = (event: KeyboardEvent, item: FindingItem) => {
|
||||
const move = keyMove(visible, item, expanded, event.key)
|
||||
if (!move) return
|
||||
event.preventDefault()
|
||||
if (move.expand) onToggle(move.expand, true)
|
||||
if (move.collapse) onToggle(move.collapse, false)
|
||||
if (move.select) {
|
||||
focusAfter.current = move.select
|
||||
onSelect(move.select)
|
||||
}
|
||||
}
|
||||
return (
|
||||
<ul
|
||||
role="tree"
|
||||
aria-label={t('editor.findings.tree')}
|
||||
className="space-y-0.5"
|
||||
>
|
||||
{visible.map((item) => (
|
||||
<TreeRow
|
||||
key={item.id}
|
||||
item={item}
|
||||
tabbable={item.id === current?.id}
|
||||
selected={item.id === selectedId}
|
||||
open={expanded.has(item.id)}
|
||||
register={(el) => {
|
||||
if (el) refs.current.set(item.id, el)
|
||||
else refs.current.delete(item.id)
|
||||
}}
|
||||
onKeyDown={onKeyDown}
|
||||
onSelect={onSelect}
|
||||
onToggle={onToggle}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
|
||||
interface TreeRowProps {
|
||||
item: FindingItem
|
||||
tabbable: boolean
|
||||
selected: boolean
|
||||
open: boolean
|
||||
register: (el: HTMLElement | null) => void
|
||||
onKeyDown: (event: KeyboardEvent, item: FindingItem) => void
|
||||
onSelect: (id: string) => void
|
||||
onToggle: (id: string, open: boolean) => void
|
||||
}
|
||||
|
||||
function TreeRow({
|
||||
item,
|
||||
tabbable,
|
||||
selected,
|
||||
open,
|
||||
register,
|
||||
onKeyDown,
|
||||
onSelect,
|
||||
onToggle,
|
||||
}: TreeRowProps) {
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
return (
|
||||
<li
|
||||
ref={register}
|
||||
role="treeitem"
|
||||
aria-level={item.level}
|
||||
aria-setsize={item.siblings}
|
||||
aria-posinset={item.index + 1}
|
||||
aria-selected={selected}
|
||||
aria-expanded={item.hasChildren ? open : undefined}
|
||||
tabIndex={tabbable ? 0 : -1}
|
||||
data-testid="finding-item"
|
||||
onKeyDown={(event) => {
|
||||
if (event.target === event.currentTarget) onKeyDown(event, item)
|
||||
}}
|
||||
onClick={() => onSelect(item.id)}
|
||||
style={{ paddingLeft: `${(item.level - 1) * 1.25 + 0.5}rem` }}
|
||||
className={`flex cursor-pointer items-center gap-2 rounded-md py-1.5 pr-2 text-sm ${
|
||||
selected
|
||||
? 'bg-[var(--color-surface-strong)] font-medium'
|
||||
: 'hover:bg-[var(--color-surface-strong)]'
|
||||
}`}
|
||||
>
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className="w-4 shrink-0 text-center"
|
||||
onClick={(event) => {
|
||||
if (!item.hasChildren) return
|
||||
event.stopPropagation()
|
||||
onToggle(item.id, !open)
|
||||
}}
|
||||
>
|
||||
{item.hasChildren ? (open ? '▾' : '▸') : ''}
|
||||
</span>
|
||||
<span className="min-w-0 flex-1 truncate">{item.name[locale]}</span>
|
||||
{item.status && <StatusBadge status={item.status} />}
|
||||
</li>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import type { ReviewStatus } from '@faunosteo/core-wasm/types'
|
||||
import Badge from '@/components/ui/Badge'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
const TONES = {
|
||||
to_confirm: 'warn',
|
||||
reviewed: 'accent',
|
||||
inherited: 'neutral',
|
||||
} as const
|
||||
|
||||
/** The review badge of a category or a node; a zone has none. */
|
||||
export default function StatusBadge({ status }: { status: ReviewStatus }) {
|
||||
const t = useT()
|
||||
return (
|
||||
<Badge tone={TONES[status]}>{t(`editor.findings.status.${status}`)}</Badge>
|
||||
)
|
||||
}
|
||||
@@ -1,14 +1,93 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import DeleteDialog, {
|
||||
type DeleteTarget,
|
||||
} from '@/components/editor/DeleteDialog'
|
||||
import AddBar from '@/components/editor/findings/AddBar'
|
||||
import FindingCreateForm, {
|
||||
type PendingFinding,
|
||||
} from '@/components/editor/findings/FindingCreateForm'
|
||||
import FindingDetail from '@/components/editor/findings/FindingDetail'
|
||||
import FindingTree from '@/components/editor/findings/FindingTree'
|
||||
import {
|
||||
buildItems,
|
||||
createdItemId,
|
||||
visibleItems,
|
||||
type FindingItem,
|
||||
} from '@/editor/findings/tree'
|
||||
import { useSheetDocument } from '@/editor/sheetDocument'
|
||||
import type { EditorTabProps } from '@/editor/tabs'
|
||||
import { useT } from '@/i18n/useT'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
|
||||
/** Placeholder: Lot I replaces the body and keeps the props contract (`EditorTabProps`). */
|
||||
export default function FindingsTab(_props: EditorTabProps) {
|
||||
/** Constats: the tree zones → categories → nodes, edited one command at a time. */
|
||||
export default function FindingsTab({ readOnly }: EditorTabProps) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const doc = useSheetDocument()
|
||||
const [selectedId, setSelectedId] = useState<string | null>(null)
|
||||
const [expanded, setExpanded] = useState<ReadonlySet<string>>(new Set())
|
||||
const [seeded, setSeeded] = useState(false)
|
||||
const [pending, setPending] = useState<PendingFinding | null>(null)
|
||||
const [deleting, setDeleting] = useState<DeleteTarget | null>(null)
|
||||
const items = useMemo(() => (doc ? buildItems(doc) : []), [doc])
|
||||
if (doc && !seeded) {
|
||||
setSeeded(true)
|
||||
setExpanded(
|
||||
new Set(items.filter((i) => i.kind === 'zone').map((i) => i.id)),
|
||||
)
|
||||
}
|
||||
const selected = items.find((item) => item.id === selectedId)
|
||||
const toggle = (id: string, open: boolean) =>
|
||||
setExpanded((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (open) next.add(id)
|
||||
else next.delete(id)
|
||||
return next
|
||||
})
|
||||
const created = (id: string) => {
|
||||
if (!pending) return
|
||||
if (pending.parent) toggle(pending.parent.id, true)
|
||||
setSelectedId(createdItemId(pending.kind, pending.parent, id))
|
||||
setPending(null)
|
||||
}
|
||||
const askDelete = (item: FindingItem) =>
|
||||
setDeleting({ key: item.key, label: item.name[locale] })
|
||||
return (
|
||||
<section
|
||||
data-testid="editor-tab-findings"
|
||||
aria-label={t('editor.tab.findings')}
|
||||
className="min-h-24"
|
||||
/>
|
||||
className="min-h-24 space-y-3"
|
||||
>
|
||||
<AddBar selected={selected} readOnly={readOnly} onStart={setPending} />
|
||||
{pending && !readOnly && (
|
||||
<FindingCreateForm
|
||||
key={`${pending.kind}-${pending.parent?.id ?? ''}`}
|
||||
pending={pending}
|
||||
onCreated={created}
|
||||
onCancel={() => setPending(null)}
|
||||
/>
|
||||
)}
|
||||
{doc && items.length === 0 && (
|
||||
<p className="text-sm text-[var(--color-ink-muted)]">
|
||||
{t('editor.findings.empty')}
|
||||
</p>
|
||||
)}
|
||||
<FindingTree
|
||||
visible={visibleItems(items, expanded)}
|
||||
selectedId={selected?.id ?? null}
|
||||
expanded={expanded}
|
||||
onSelect={setSelectedId}
|
||||
onToggle={toggle}
|
||||
/>
|
||||
{selected && (
|
||||
<FindingDetail
|
||||
key={selected.id}
|
||||
item={selected}
|
||||
readOnly={readOnly}
|
||||
onDelete={() => askDelete(selected)}
|
||||
/>
|
||||
)}
|
||||
<DeleteDialog target={deleting} onClose={() => setDeleting(null)} />
|
||||
</section>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user