Consumers now fed by the core (crates/core through @faunosteo/core-wasm):
- the sheet: the bundled payload is parsed and indexed by the core
(SheetHandle, SheetIndexData); SheetIndex reads precomputed tables only;
- the store: new report, add / remove / move observation, draft restore
(v2 migration + rebase) through the core, actions answering { ok };
- the editor click chain (zone.click), laterality and vertebra pickers
(zone.add_sides, vertebra.initial / sync / select), the findings picker
(finding.toggle by clicked leaf position), zone removal;
- the import: engine byte scan (TypeScript until R4), then the core's
layered reading and rebase in one call; the export: the core builds the
sheet snapshot, the frozen TypeScript engine writes the PDF (R4).
Lazy core: Home and the new-report form read the sheet availability from
the bundle and load the module on import / submit only; the auth shell
never references it. Degraded states (core_unavailable, invalid_sheet)
show the validated notice with a retry and never redirect or touch the
draft; an unreadable draft or archive is set aside under
osteo:quarantine:* (only content kinds; a failed write keeps it in place
and the editor stays), a rebase that flags items keeps the raw pre-rebase
draft. Four strings, wording validated by the owner.
Deleted: services/zoneInteraction.ts and its suite (13 cases ported to
cargo), the TypeScript Finding builder and vertebra selection rules of the
pickers, store rules, persistence parsing, hydrateReport. ESLint forbids
@faunosteo/domain in apps/app/src; the report:compare CLI keeps the frozen
domain until R4 (scripts/compare/sheet.ts). App suite 313/313 on the real
core (24 new tests: quarantine, degraded editor, core failures, N-1
reader); step6 goldens and the frozen envelopes unchanged.
273 lines
8.8 KiB
TypeScript
273 lines
8.8 KiB
TypeScript
import { useMemo, useState } from 'react'
|
||
import { motion } from 'motion/react'
|
||
import type {
|
||
Finding,
|
||
FindingCategory,
|
||
FindingNode,
|
||
FindingZone,
|
||
LateralSide,
|
||
LocalizedOptionalText,
|
||
} from '@faunosteo/core-wasm/types'
|
||
import Button from '@/components/ui/Button'
|
||
import { pickLabel } from '@/i18n/pickLocaleLabel'
|
||
import { useT } from '@/i18n/useT'
|
||
import { useLocaleStore } from '@/state/localeStore'
|
||
import type { Locale } from '@/i18n/types'
|
||
|
||
const levelVariants = {
|
||
enter: (dir: 1 | -1) => ({ opacity: 0, x: dir * 32 }),
|
||
center: { opacity: 1, x: 0 },
|
||
}
|
||
|
||
type TreeNode = FindingCategory | FindingNode
|
||
|
||
export interface FindingsTreePickerProps {
|
||
zone: FindingZone
|
||
side?: LateralSide
|
||
selected: Finding[]
|
||
/**
|
||
* A leaf was ticked or unticked: its path and its position among the siblings shown.
|
||
* The parent has the core build or remove the finding (R2).
|
||
*/
|
||
onToggle: (leafPath: string[], leafPosition: number) => void
|
||
}
|
||
|
||
function isLeaf(node: TreeNode): boolean {
|
||
return node.children.length === 0
|
||
}
|
||
|
||
function samePath(a: readonly string[], b: readonly string[]): boolean {
|
||
return a.length === b.length && a.every((seg, i) => seg === b[i])
|
||
}
|
||
|
||
function pathStartsWith(
|
||
full: readonly string[],
|
||
prefix: readonly string[],
|
||
): boolean {
|
||
return (
|
||
full.length >= prefix.length && prefix.every((seg, i) => full[i] === seg)
|
||
)
|
||
}
|
||
|
||
function findNode(
|
||
categories: readonly FindingCategory[],
|
||
path: readonly string[],
|
||
): TreeNode | undefined {
|
||
let nodes: readonly TreeNode[] = categories
|
||
let node: TreeNode | undefined
|
||
for (const segment of path) {
|
||
node = nodes.find((n) => n.id === segment)
|
||
if (!node) return undefined
|
||
nodes = node.children
|
||
}
|
||
return node
|
||
}
|
||
|
||
function descriptionOf(node: TreeNode, locale: Locale): string | undefined {
|
||
const text: LocalizedOptionalText | undefined = node.description
|
||
if (!text) return undefined
|
||
return (
|
||
(locale === 'en' ? text.en || text.fr : text.fr || text.en) || undefined
|
||
)
|
||
}
|
||
|
||
/**
|
||
* Drill-down picker over one finding zone of the sheet. A finding is keyed
|
||
* by `(findingZoneId, path, side)`; the core snapshots its labels when it
|
||
* builds it. The selection predicates below only decide what is shown.
|
||
*/
|
||
export default function FindingsTreePicker({
|
||
zone,
|
||
side,
|
||
selected,
|
||
onToggle,
|
||
}: FindingsTreePickerProps) {
|
||
const t = useT()
|
||
const locale = useLocaleStore((s) => s.locale)
|
||
const [path, setPath] = useState<string[]>([])
|
||
const [direction, setDirection] = useState<1 | -1>(1)
|
||
|
||
function goInto(nodePath: string[]) {
|
||
setDirection(1)
|
||
setPath(nodePath)
|
||
}
|
||
function goBack() {
|
||
setDirection(-1)
|
||
setPath(path.slice(0, -1))
|
||
}
|
||
function goToDepth(depth: number) {
|
||
setDirection(depth >= path.length ? 1 : -1)
|
||
setPath(path.slice(0, depth))
|
||
}
|
||
|
||
const currentNodes = useMemo<readonly TreeNode[]>(() => {
|
||
if (path.length === 0) return zone.categories
|
||
return findNode(zone.categories, path)?.children ?? []
|
||
}, [zone, path])
|
||
|
||
const focusedNode = useMemo(
|
||
() => (path.length === 0 ? undefined : findNode(zone.categories, path)),
|
||
[zone, path],
|
||
)
|
||
|
||
const breadcrumb = useMemo<{ label: string; depth: number }[]>(() => {
|
||
const crumbs = [{ label: pickLabel(zone.name, locale), depth: 0 }]
|
||
let nodes: readonly TreeNode[] = zone.categories
|
||
path.forEach((seg, i) => {
|
||
const n = nodes.find((x) => x.id === seg)
|
||
if (n) {
|
||
crumbs.push({ label: pickLabel(n.name, locale), depth: i + 1 })
|
||
nodes = n.children
|
||
}
|
||
})
|
||
return crumbs
|
||
}, [zone, path, locale])
|
||
|
||
const matches = (f: Finding) => f.findingZoneId === zone.id && f.side === side
|
||
const isLeafSelected = (leafPath: string[]) =>
|
||
selected.some((f) => matches(f) && samePath(f.path, leafPath))
|
||
const countSelectedUnder = (nodePath: string[]) =>
|
||
selected.filter((f) => matches(f) && pathStartsWith(f.path, nodePath))
|
||
.length
|
||
|
||
const focusedDescription = focusedNode && descriptionOf(focusedNode, locale)
|
||
|
||
return (
|
||
<div className="space-y-2">
|
||
<div className="flex flex-wrap items-center gap-1 text-xs text-[var(--color-ink-muted)]">
|
||
{breadcrumb.map((c, i) => {
|
||
const isLast = i === breadcrumb.length - 1
|
||
return (
|
||
<span key={`${c.label}-${i}`} className="flex items-center gap-1">
|
||
<button
|
||
type="button"
|
||
onClick={() => goToDepth(c.depth)}
|
||
disabled={isLast}
|
||
className={
|
||
isLast
|
||
? 'text-[var(--color-ink)] font-medium'
|
||
: 'underline text-[var(--color-accent-strong)]'
|
||
}
|
||
>
|
||
{c.label}
|
||
</button>
|
||
{!isLast && <span aria-hidden="true">›</span>}
|
||
</span>
|
||
)
|
||
})}
|
||
</div>
|
||
|
||
{focusedDescription && (
|
||
<p className="text-xs italic text-[var(--color-ink-muted)]">
|
||
{focusedDescription}
|
||
</p>
|
||
)}
|
||
|
||
{path.length > 0 && (
|
||
<Button
|
||
size="sm"
|
||
variant="ghost"
|
||
onClick={goBack}
|
||
aria-label={t('findings.back_to_parent')}
|
||
>
|
||
{t('findings.back')}
|
||
</Button>
|
||
)}
|
||
|
||
<motion.ul
|
||
key={path.join('/') || '__root__'}
|
||
custom={direction}
|
||
variants={levelVariants}
|
||
initial="enter"
|
||
animate="center"
|
||
transition={{ duration: 0.22, ease: 'easeOut' }}
|
||
className="space-y-1.5 max-h-64 overflow-auto pr-1"
|
||
>
|
||
{currentNodes.length === 0 ? (
|
||
<li className="text-sm text-[var(--color-ink-muted)]">
|
||
{t('findings.no_options')}
|
||
</li>
|
||
) : (
|
||
currentNodes.map((node, position) => {
|
||
const nodePath = [...path, node.id]
|
||
const nodeLabel = pickLabel(node.name, locale)
|
||
const nodeDesc = descriptionOf(node, locale)
|
||
if (isLeaf(node)) {
|
||
const vet =
|
||
'requires_vet_referral' in node && node.requires_vet_referral
|
||
return (
|
||
<li key={node.id}>
|
||
<label className="flex items-start gap-2 cursor-pointer">
|
||
<input
|
||
type="checkbox"
|
||
checked={isLeafSelected(nodePath)}
|
||
onChange={() => onToggle(nodePath, position)}
|
||
className="mt-1 accent-[var(--color-accent)]"
|
||
aria-label={nodeLabel}
|
||
/>
|
||
<div className="flex-1">
|
||
<div className="flex items-center gap-2">
|
||
<span className="text-sm font-medium text-[var(--color-ink)]">
|
||
{nodeLabel}
|
||
</span>
|
||
{vet && (
|
||
<span
|
||
title={t('findings.vet_referral')}
|
||
aria-label={t('findings.vet_referral')}
|
||
className="text-[var(--color-danger)]"
|
||
>
|
||
⚠
|
||
</span>
|
||
)}
|
||
</div>
|
||
{nodeDesc && (
|
||
<div className="text-xs text-[var(--color-ink-muted)]">
|
||
{nodeDesc}
|
||
</div>
|
||
)}
|
||
</div>
|
||
</label>
|
||
</li>
|
||
)
|
||
}
|
||
const selectedCount = countSelectedUnder(nodePath)
|
||
const selectedAria = `${selectedCount} ${
|
||
selectedCount > 1
|
||
? t('findings.selected_plural')
|
||
: t('findings.selected_singular')
|
||
}`
|
||
return (
|
||
<li key={node.id}>
|
||
<button
|
||
type="button"
|
||
onClick={() => goInto(nodePath)}
|
||
className="flex items-center justify-between w-full text-left text-sm px-2 py-1.5 rounded hover:bg-[var(--color-surface-strong)] text-[var(--color-ink)]"
|
||
aria-label={`${t('findings.open')} ${nodeLabel}`}
|
||
>
|
||
<span className="flex items-center gap-2">
|
||
<span>{nodeLabel}</span>
|
||
{selectedCount > 0 && (
|
||
<span
|
||
aria-label={selectedAria}
|
||
className="inline-flex items-center justify-center min-w-[20px] h-[20px] px-1.5 rounded-full text-[11px] font-semibold bg-[var(--color-accent)] text-white"
|
||
>
|
||
{selectedCount}
|
||
</span>
|
||
)}
|
||
</span>
|
||
<span
|
||
aria-hidden="true"
|
||
className="text-[var(--color-ink-muted)]"
|
||
>
|
||
›
|
||
</span>
|
||
</button>
|
||
</li>
|
||
)
|
||
})
|
||
)}
|
||
</motion.ul>
|
||
</div>
|
||
)
|
||
}
|