feat(app): application on the canonical sheet — report v3 store with v2 draft migration, bundled SheetProvider (SHEET_BUNDLE), annotation layer rendered from the document, zone interaction service, pickers and cards on the sheet index, PDF v3 export with v2 archive import; legacy registry, matchers, editors and assets removed

This commit is contained in:
bchanot committed 2026-10-10 11:36:14 +02:00
1 parent 0a17e390cf
commit ce2ec26112
114 files changed
+3560 -14637

No files matched your search

@@ -1,16 +1,11 @@
import { useMemo, useState } from 'react'
import type {
Observation,
ObservationZone,
Vertebra,
Finding,
FindingZone,
LateralSide,
} from '@/domain/schema/observation'
import type {
SpeciesFindings,
ZoneFindings,
} from '@/domain/schema/findings'
import type { Bone } from '@/anatomy/types'
import { matchZoneFindings } from '@/lib/zoneMatch'
Observation,
ReportZone,
} from '@faunosteo/domain'
import { AnimatePresence, motion } from 'motion/react'
import Card from '@/components/ui/Card'
import Badge from '@/components/ui/Badge'
@@ -20,9 +15,9 @@ import Textarea from '@/components/ui/Textarea'
import FindingsTreePicker from './FindingsTreePicker'
import FreeTextFindingInput from './FreeTextFindingInput'
import SeveritySelector from './SeveritySelector'
import { isObservable, pickLabel, type SheetIndex } from '@/services/sheetIndex'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
import { boneLabel } from '@/anatomy/labels'
import type { Locale } from '@/i18n/types'
export interface ObservationCardProps {
@@ -36,35 +31,75 @@ export interface ObservationCardProps {
canMoveUp: boolean
canMoveDown: boolean
// Editor body props (used only when expanded)
bones: Map<string, Bone>
findings: SpeciesFindings | null
sheet: SheetIndex
onChange: (next: Observation) => void
onOpenVertebraPicker: (zoneIndex: number) => void
onRemoveZone: (zoneIndex: number) => void
}
/** Zone label: the sheet's current name, else the snapshot, else the id. */
export function zoneLabel(
z: ReportZone,
sheet: SheetIndex,
locale: Locale,
): string {
const annotation = sheet.annotation(z.annotationId)
if (annotation) return pickLabel(sheet.name(annotation), locale)
const snapshot =
locale === 'en'
? z.labelEnSnapshot || z.labelFrSnapshot
: z.labelFrSnapshot || z.labelEnSnapshot
return snapshot || z.annotationId
}
function describeZone(
z: ObservationZone,
bones: Map<string, Bone>,
z: ReportZone,
sheet: SheetIndex,
locale: Locale,
t: (key: string) => string,
): string {
const label = boneLabel(bones.get(z.boneId), locale, z.boneId)
const label = zoneLabel(z, sheet, locale)
if (z.vertebrae?.length) {
const vs = z.vertebrae
.map((v: Vertebra) => `${v.region}${v.index}`)
.join(', ')
return `${label} (${vs})`
return `${label} (${z.vertebrae.map((v) => `${v.region}${v.index}`).join(', ')})`
}
if (z.side) {
const sideLabel = t(
z.side === 'left' ? 'lateral.left_short' : 'lateral.right_short',
)
return `${label} (${sideLabel})`
return `${label} (${t(z.side === 'left' ? 'lateral.left_short' : 'lateral.right_short')})`
}
return label
}
interface ZoneTree {
zone: FindingZone
side?: LateralSide
}
/** One picker per (finding zone, side) the observation's zones resolve to. */
function zoneTrees(observation: Observation, sheet: SheetIndex): ZoneTree[] {
const seen = new Set<string>()
const out: ZoneTree[] = []
for (const z of observation.zones) {
const annotation = sheet.annotation(z.annotationId)
if (!isObservable(annotation) || !annotation.finding_zone_id) continue
const zone = sheet.findingZone(annotation.finding_zone_id)
if (!zone) continue
const key = z.side ? `${zone.id}:${z.side}` : zone.id
if (seen.has(key)) continue
seen.add(key)
out.push({ zone, side: z.side })
}
return out
}
/** Findings the sheet no longer holds at their path (migrated data, kept verbatim). */
function unresolvedFindings(
observation: Observation,
sheet: SheetIndex,
): Finding[] {
return observation.findings.filter(
(f) => !sheet.nodeAt(f.findingZoneId, f.path),
)
}
export default function ObservationCard({
observation,
index,
@@ -75,34 +110,37 @@ export default function ObservationCard({
onMoveDown,
canMoveUp,
canMoveDown,
bones,
findings,
sheet,
onChange,
onOpenVertebraPicker,
onRemoveZone,
}: ObservationCardProps) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
const nameOf = (z: { name_fr: string; name_en: string }) =>
locale === 'en' ? z.name_en : z.name_fr
// Per-zone picker accordion — only one tree picker open at a time,
// matching the observation-card accordion semantics. Closed by default
// so the card stays compact when first expanded.
const [expandedZoneId, setExpandedZoneId] = useState<string | null>(null)
const zoneCount = observation.zones.length
const findingCount =
observation.findings.length + observation.freeTextFindings.length
const zoneCountLabel =
const zoneCountLabel = t(
zoneCount > 1
? t('observation.zone_label_plural')
: t('observation.zone_label_singular')
const findingCountLabel =
? 'observation.zone_label_plural'
: 'observation.zone_label_singular',
)
const findingCountLabel = t(
findingCount > 1
? t('observation.finding_label_plural')
: t('observation.finding_label_singular')
? 'observation.finding_label_plural'
: 'observation.finding_label_singular',
)
const trees = useMemo(
() => zoneTrees(observation, sheet),
[observation, sheet],
)
const unresolved = useMemo(
() => unresolvedFindings(observation, sheet),
[observation, sheet],
)
function onHeaderKey(event: React.KeyboardEvent<HTMLDivElement>) {
if (event.key === 'Enter' || event.key === ' ') {
@@ -110,39 +148,15 @@ export default function ObservationCard({
onToggle()
}
}
function stop(fn: () => void) {
return (event: React.MouseEvent) => {
event.stopPropagation()
fn()
}
const stop = (fn: () => void) => (event: React.MouseEvent) => {
event.stopPropagation()
fn()
}
const zonesArray = useMemo(
() => (findings ? Object.values(findings.zones) : []),
[findings],
)
const zonesWithTree = useMemo(() => {
if (zonesArray.length === 0)
return [] as { tree: ZoneFindings; side?: LateralSide }[]
const seen = new Set<string>()
const out: { tree: ZoneFindings; side?: LateralSide }[] = []
for (const z of observation.zones) {
const bone = bones.get(z.boneId)
if (!bone) continue
const tree = matchZoneFindings(
{ fr: bone.fr, en: bone.en },
zonesArray,
)
if (!tree) continue
const key = z.side ? `${tree.id}:${z.side}` : tree.id
if (seen.has(key)) continue
seen.add(key)
out.push({ tree, side: z.side })
}
return out
}, [observation.zones, bones, zonesArray])
const removeFinding = (target: Finding) =>
onChange({
...observation,
findings: observation.findings.filter((f) => f !== target),
})
return (
<Card
@@ -164,13 +178,16 @@ export default function ObservationCard({
>
<div className="flex-1 min-h-[60px] text-left">
<div className="flex items-center gap-2 mb-2">
<Badge tone="neutral">{t('observation.badge_n')}{index + 1}</Badge>
<Badge tone="neutral">
{t('observation.badge_n')}
{index + 1}
</Badge>
{observation.severity && (
<Badge
tone={
observation.severity === 'sévère'
observation.severity === 'severe'
? 'danger'
: observation.severity === 'modérée'
: observation.severity === 'moderate'
? 'warn'
: 'neutral'
}
@@ -248,43 +265,49 @@ export default function ObservationCard({
</p>
) : (
<ul className="flex flex-wrap gap-2">
{observation.zones.map((z, i) => {
const b = bones.get(z.boneId)
const supportsVertebrae =
b?.special === 'vertebral-precision'
return (
<li key={`${z.boneId}-${i}`}>
<Badge tone="accent" className="gap-2">
{describeZone(z, bones, locale, t)}
{supportsVertebrae && (
<button
type="button"
onClick={() => onOpenVertebraPicker(i)}
className="underline text-xs text-[var(--color-accent-strong)]"
title={t('observation.vertebrae_button_title')}
{observation.zones.map((z, i) => (
<li
key={`${z.annotationId}-${z.viewId}-${z.side ?? ''}-${i}`}
>
<Badge tone="accent" className="gap-2">
{describeZone(z, sheet, locale, t)}
{z.unresolved &&
!sheet.annotation(z.annotationId) && (
<span
className="text-xs italic"
title={t('observation.zone_unresolved')}
>
{t('observation.vertebrae_button')}
</button>
{t('observation.unresolved_short')}
</span>
)}
{sheet.isSpine(z.annotationId) && (
<button
type="button"
aria-label={`${t('observation.remove_zone')} ${boneLabel(b, locale, z.boneId)}`}
onClick={() => onRemoveZone(i)}
className="ml-1 text-[var(--color-accent-strong)]"
onClick={() => onOpenVertebraPicker(i)}
className="underline text-xs text-[var(--color-accent-strong)]"
title={t('observation.vertebrae_button_title')}
>
✕
{t('observation.vertebrae_button')}
</button>
</Badge>
</li>
)
})}
)}
<button
type="button"
aria-label={`${t('observation.remove_zone')} ${zoneLabel(z, sheet, locale)}`}
onClick={() => onRemoveZone(i)}
className="ml-1 text-[var(--color-accent-strong)]"
>
✕
</button>
</Badge>
</li>
))}
</ul>
)}
</section>
<section>
<Label>{t('observation.findings_catalog_label')}</Label>
{zonesWithTree.length === 0 ? (
{trees.length === 0 ? (
<p className="text-sm text-[var(--color-ink-muted)]">
{observation.zones.length === 0
? t('observation.findings_pick_zone_first')
@@ -292,26 +315,19 @@ export default function ObservationCard({
</p>
) : (
<ul className="space-y-2">
{zonesWithTree.map(({ tree, side }) => {
const pickerKey = side
? `${tree.id}:${side}`
: tree.id
const expanded = expandedZoneId === pickerKey
{trees.map(({ zone, side }) => {
const pickerKey = side ? `${zone.id}:${side}` : zone.id
const open = expandedZoneId === pickerKey
const selectedCount = observation.findings.filter(
(f) =>
f.zoneId === tree.id && f.side === side,
(f) => f.findingZoneId === zone.id && f.side === side,
).length
const selectedAria = `${selectedCount} ${
selectedCount > 1
? t('findings.selected_plural')
: t('findings.selected_singular')
}`
const selectedAria = `${selectedCount} ${selectedCount > 1 ? t('findings.selected_plural') : t('findings.selected_singular')}`
return (
<li
key={pickerKey}
className={[
'rounded-xl border bg-[var(--color-surface)] transition',
expanded
open
? 'border-[var(--color-accent)] ring-1 ring-[var(--color-accent)]'
: 'border-[var(--color-border)]',
].join(' ')}
@@ -319,16 +335,17 @@ export default function ObservationCard({
<button
type="button"
onClick={() =>
setExpandedZoneId(expanded ? null : pickerKey)
setExpandedZoneId(open ? null : pickerKey)
}
aria-expanded={expanded}
aria-expanded={open}
aria-controls={`findings-zone-${pickerKey}`}
className="w-full flex items-center justify-between gap-2 px-3 py-2 text-left rounded-xl focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)]/40"
>
<span className="flex items-center gap-2">
<span className="text-sm font-medium text-[var(--color-ink)]">
{nameOf(tree)}
{side && ` (${t(side === 'left' ? 'lateral.left' : 'lateral.right')})`}
{pickLabel(zone.name, locale)}
{side &&
` (${t(side === 'left' ? 'lateral.left' : 'lateral.right')})`}
</span>
{selectedCount > 0 && (
<span
@@ -343,11 +360,11 @@ export default function ObservationCard({
aria-hidden="true"
className="text-[var(--color-ink-muted)]"
>
{expanded ? '▲' : '▼'}
{open ? '▲' : '▼'}
</span>
</button>
<AnimatePresence initial={false}>
{expanded && (
{open && (
<motion.div
key="picker-body"
id={`findings-zone-${pickerKey}`}
@@ -359,12 +376,14 @@ export default function ObservationCard({
>
<div className="px-3 pb-3 pt-1 border-t border-[var(--color-border)]">
<FindingsTreePicker
zoneId={tree.id}
tree={tree}
zone={zone}
side={side}
selected={observation.findings}
onChange={(next) =>
onChange({ ...observation, findings: next })
onChange({
...observation,
findings: next,
})
}
/>
</div>
@@ -378,6 +397,40 @@ export default function ObservationCard({
)}
</section>
{unresolved.length > 0 && (
<section>
<Label>{t('observation.findings_unresolved_label')}</Label>
<p className="text-xs text-[var(--color-ink-muted)] mb-2">
{t('observation.findings_unresolved_help')}
</p>
<ul className="space-y-1">
{unresolved.map((f, i) => (
<li
key={`${f.findingZoneId}-${f.path.join('/')}-${f.side ?? ''}-${i}`}
className="flex items-center gap-2 text-sm"
>
<span className="text-[var(--color-ink)]">
{f.requiresVetReferral ? '⚠ ' : ''}
{locale === 'en'
? f.labelEnSnapshot
: f.labelFrSnapshot}
{f.side &&
` (${t(f.side === 'left' ? 'lateral.left_short' : 'lateral.right_short')})`}
</span>
<button
type="button"
aria-label={`${t('common.delete')} ${locale === 'en' ? f.labelEnSnapshot : f.labelFrSnapshot}`}
onClick={() => removeFinding(f)}
className="text-[var(--color-accent-strong)]"
>
✕
</button>
</li>
))}
</ul>
</section>
)}
<section>
<Label>{t('observation.findings_free_label')}</Label>
<FreeTextFindingInput