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([]) 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(() => { 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 (
{breadcrumb.map((c, i) => { const isLast = i === breadcrumb.length - 1 return ( {!isLast && } ) })}
{focusedDescription && (

{focusedDescription}

)} {path.length > 0 && ( )} {currentNodes.length === 0 ? (
  • {t('findings.no_options')}
  • ) : ( 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 (
  • ) } const selectedCount = countSelectedUnder(nodePath) const selectedAria = `${selectedCount} ${ selectedCount > 1 ? t('findings.selected_plural') : t('findings.selected_singular') }` return (
  • ) }) )}
    ) }