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:
1 parent
0a17e390cf
commit
ce2ec26112
114 files changed
+3560
-14637
No files matched your search
@@ -1,75 +1,84 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import { motion } from 'motion/react'
|
||||
import type {
|
||||
Finding,
|
||||
FindingCategory,
|
||||
FindingNode,
|
||||
ZoneFindings,
|
||||
} from '@/domain/schema/findings'
|
||||
import type { Finding, LateralSide } from '@/domain/schema/observation'
|
||||
FindingZone,
|
||||
LateralSide,
|
||||
LocalizedOptionalText,
|
||||
} from '@faunosteo/domain'
|
||||
import Button from '@/components/ui/Button'
|
||||
import { pickLabel } from '@/services/sheetIndex'
|
||||
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 {
|
||||
zoneId: string
|
||||
tree: ZoneFindings
|
||||
zone: FindingZone
|
||||
side?: LateralSide
|
||||
selected: Finding[]
|
||||
onChange: (next: Finding[]) => void
|
||||
}
|
||||
|
||||
function isLeaf(node: FindingNode): boolean {
|
||||
return !node.children || node.children.length === 0
|
||||
function isLeaf(node: TreeNode): boolean {
|
||||
return node.children.length === 0
|
||||
}
|
||||
|
||||
function samePath(a: string[], b: string[]): boolean {
|
||||
if (a.length !== b.length) return false
|
||||
for (let i = 0; i < a.length; i += 1) {
|
||||
if (a[i] !== b[i]) return false
|
||||
}
|
||||
return true
|
||||
function samePath(a: readonly string[], b: readonly string[]): boolean {
|
||||
return a.length === b.length && a.every((seg, i) => seg === b[i])
|
||||
}
|
||||
|
||||
function pathStartsWith(full: string[], prefix: string[]): boolean {
|
||||
if (full.length < prefix.length) return false
|
||||
for (let i = 0; i < prefix.length; i += 1) {
|
||||
if (full[i] !== prefix[i]) return false
|
||||
}
|
||||
return true
|
||||
function pathStartsWith(
|
||||
full: readonly string[],
|
||||
prefix: readonly string[],
|
||||
): boolean {
|
||||
return (
|
||||
full.length >= prefix.length && prefix.every((seg, i) => full[i] === seg)
|
||||
)
|
||||
}
|
||||
|
||||
function findNode(
|
||||
categories: FindingNode[],
|
||||
path: string[],
|
||||
): FindingNode | undefined {
|
||||
let nodes: FindingNode[] = categories
|
||||
let node: FindingNode | undefined
|
||||
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 ?? []
|
||||
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)`; labels are snapshotted at selection.
|
||||
*/
|
||||
export default function FindingsTreePicker({
|
||||
zoneId,
|
||||
tree,
|
||||
zone,
|
||||
side,
|
||||
selected,
|
||||
onChange,
|
||||
}: FindingsTreePickerProps) {
|
||||
const t = useT()
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
const nameOf = (node: { name_fr: string; name_en: string }) =>
|
||||
locale === 'en' ? node.name_en : node.name_fr
|
||||
const descOf = (node: { description_fr?: string; description_en?: string }) =>
|
||||
locale === 'en' ? node.description_en : node.description_fr
|
||||
|
||||
const [path, setPath] = useState<string[]>([])
|
||||
const [direction, setDirection] = useState<1 | -1>(1)
|
||||
|
||||
@@ -86,74 +95,57 @@ export default function FindingsTreePicker({
|
||||
setPath(path.slice(0, depth))
|
||||
}
|
||||
|
||||
const currentNodes = useMemo<FindingNode[]>(() => {
|
||||
if (path.length === 0) return tree.categories
|
||||
const node = findNode(tree.categories, path)
|
||||
return node?.children ?? []
|
||||
}, [tree, path])
|
||||
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(tree.categories, path)),
|
||||
[tree, path],
|
||||
() => (path.length === 0 ? undefined : findNode(zone.categories, path)),
|
||||
[zone, path],
|
||||
)
|
||||
|
||||
const breadcrumb = useMemo<{ label: string; depth: number }[]>(() => {
|
||||
const pick = (z: { name_fr: string; name_en: string }) =>
|
||||
locale === 'en' ? z.name_en : z.name_fr
|
||||
const crumbs: { label: string; depth: number }[] = [
|
||||
{ label: pick(tree), depth: 0 },
|
||||
]
|
||||
let nodes: FindingNode[] = tree.categories
|
||||
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: pick(n), depth: i + 1 })
|
||||
nodes = n.children ?? []
|
||||
crumbs.push({ label: pickLabel(n.name, locale), depth: i + 1 })
|
||||
nodes = n.children
|
||||
}
|
||||
})
|
||||
return crumbs
|
||||
}, [tree, path, locale])
|
||||
}, [zone, path, locale])
|
||||
|
||||
function matchesSide(f: Finding): boolean {
|
||||
return f.zoneId === zoneId && f.side === side
|
||||
}
|
||||
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
|
||||
|
||||
function isLeafSelected(leafPath: string[]): boolean {
|
||||
return selected.some(
|
||||
(f) => matchesSide(f) && samePath(f.path, leafPath),
|
||||
)
|
||||
}
|
||||
|
||||
function countSelectedUnder(nodePath: string[]): number {
|
||||
let n = 0
|
||||
for (const f of selected) {
|
||||
if (!matchesSide(f)) continue
|
||||
if (!pathStartsWith(f.path, nodePath)) continue
|
||||
n += 1
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
function toggleLeaf(leafPath: string[], leaf: FindingNode) {
|
||||
function toggleLeaf(leafPath: string[], leaf: TreeNode) {
|
||||
if (isLeafSelected(leafPath)) {
|
||||
onChange(
|
||||
selected.filter(
|
||||
(f) => !(matchesSide(f) && samePath(f.path, leafPath)),
|
||||
),
|
||||
selected.filter((f) => !(matches(f) && samePath(f.path, leafPath))),
|
||||
)
|
||||
return
|
||||
}
|
||||
const next: Finding = {
|
||||
zoneId,
|
||||
findingZoneId: zone.id,
|
||||
path: leafPath,
|
||||
labelFrSnapshot: leaf.name_fr,
|
||||
labelEnSnapshot: leaf.name_en,
|
||||
labelFrSnapshot: leaf.name.fr,
|
||||
labelEnSnapshot: leaf.name.en,
|
||||
}
|
||||
if (leaf.requires_vet_referral) next.requiresVetReferral = true
|
||||
if ('requires_vet_referral' in leaf && leaf.requires_vet_referral)
|
||||
next.requiresVetReferral = true
|
||||
if (side) next.side = side
|
||||
onChange([...selected, next])
|
||||
}
|
||||
|
||||
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)]">
|
||||
@@ -179,9 +171,9 @@ export default function FindingsTreePicker({
|
||||
})}
|
||||
</div>
|
||||
|
||||
{focusedNode && descOf(focusedNode) && (
|
||||
{focusedDescription && (
|
||||
<p className="text-xs italic text-[var(--color-ink-muted)]">
|
||||
{descOf(focusedNode)}
|
||||
{focusedDescription}
|
||||
</p>
|
||||
)}
|
||||
|
||||
@@ -196,16 +188,16 @@ export default function FindingsTreePicker({
|
||||
</Button>
|
||||
)}
|
||||
|
||||
<motion.ul
|
||||
key={path.join('/') || '__root__'}
|
||||
custom={direction}
|
||||
variants={levelVariants}
|
||||
initial="enter"
|
||||
animate="center"
|
||||
transition={{ duration: 0.22, ease: 'easeOut' }}
|
||||
style={{ willChange: 'transform, opacity' }}
|
||||
className="space-y-1.5 max-h-64 overflow-auto pr-1"
|
||||
>
|
||||
<motion.ul
|
||||
key={path.join('/') || '__root__'}
|
||||
custom={direction}
|
||||
variants={levelVariants}
|
||||
initial="enter"
|
||||
animate="center"
|
||||
transition={{ duration: 0.22, ease: 'easeOut' }}
|
||||
style={{ willChange: 'transform, opacity' }}
|
||||
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')}
|
||||
@@ -213,16 +205,17 @@ export default function FindingsTreePicker({
|
||||
) : (
|
||||
currentNodes.map((node) => {
|
||||
const nodePath = [...path, node.id]
|
||||
const nodeLabel = pickLabel(node.name, locale)
|
||||
const nodeDesc = descriptionOf(node, locale)
|
||||
if (isLeaf(node)) {
|
||||
const checked = isLeafSelected(nodePath)
|
||||
const nodeLabel = nameOf(node)
|
||||
const nodeDesc = descOf(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={checked}
|
||||
checked={isLeafSelected(nodePath)}
|
||||
onChange={() => toggleLeaf(nodePath, node)}
|
||||
className="mt-1 accent-[var(--color-accent)]"
|
||||
aria-label={nodeLabel}
|
||||
@@ -232,7 +225,7 @@ export default function FindingsTreePicker({
|
||||
<span className="text-sm font-medium text-[var(--color-ink)]">
|
||||
{nodeLabel}
|
||||
</span>
|
||||
{node.requires_vet_referral && (
|
||||
{vet && (
|
||||
<span
|
||||
title={t('findings.vet_referral')}
|
||||
aria-label={t('findings.vet_referral')}
|
||||
@@ -253,7 +246,6 @@ export default function FindingsTreePicker({
|
||||
)
|
||||
}
|
||||
const selectedCount = countSelectedUnder(nodePath)
|
||||
const nodeLabel = nameOf(node)
|
||||
const selectedAria = `${selectedCount} ${
|
||||
selectedCount > 1
|
||||
? t('findings.selected_plural')
|
||||
@@ -278,7 +270,10 @@ export default function FindingsTreePicker({
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
<span aria-hidden="true" className="text-[var(--color-ink-muted)]">
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className="text-[var(--color-ink-muted)]"
|
||||
>
|
||||
›
|
||||
</span>
|
||||
</button>
|
||||
@@ -286,7 +281,7 @@ export default function FindingsTreePicker({
|
||||
)
|
||||
})
|
||||
)}
|
||||
</motion.ul>
|
||||
</motion.ul>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user