import { useMemo, useState } from 'react' import { motion } from 'motion/react' import type { FindingNode, ZoneFindings, } from '@/domain/schema/findings' import type { Finding } from '@/domain/schema/observation' import Button from '@/components/ui/Button' import { useT } from '@/i18n/useT' import { useLocaleStore } from '@/state/localeStore' const levelVariants = { enter: (dir: 1 | -1) => ({ opacity: 0, x: dir * 32 }), center: { opacity: 1, x: 0 }, } export interface FindingsTreePickerProps { zoneId: string tree: ZoneFindings selected: Finding[] onChange: (next: Finding[]) => void } function isLeaf(node: FindingNode): boolean { return !node.children || 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 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 findNode( categories: FindingNode[], path: string[], ): FindingNode | undefined { let nodes: FindingNode[] = categories let node: FindingNode | undefined for (const segment of path) { node = nodes.find((n) => n.id === segment) if (!node) return undefined nodes = node.children ?? [] } return node } export default function FindingsTreePicker({ zoneId, tree, 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([]) 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 tree.categories const node = findNode(tree.categories, path) return node?.children ?? [] }, [tree, path]) const focusedNode = useMemo( () => (path.length === 0 ? undefined : findNode(tree.categories, path)), [tree, 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 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 ?? [] } }) return crumbs }, [tree, path, locale]) function isLeafSelected(leafPath: string[]): boolean { return selected.some( (f) => f.zoneId === zoneId && samePath(f.path, leafPath), ) } function countSelectedUnder(nodePath: string[]): number { let n = 0 for (const f of selected) { if (f.zoneId !== zoneId) continue if (!pathStartsWith(f.path, nodePath)) continue n += 1 } return n } function toggleLeaf(leafPath: string[], leaf: FindingNode) { if (isLeafSelected(leafPath)) { onChange( selected.filter( (f) => !(f.zoneId === zoneId && samePath(f.path, leafPath)), ), ) return } const next: Finding = { zoneId, path: leafPath, labelFrSnapshot: leaf.name_fr, labelEnSnapshot: leaf.name_en, } if (leaf.requires_vet_referral) next.requiresVetReferral = true onChange([...selected, next]) } return (
{breadcrumb.map((c, i) => { const isLast = i === breadcrumb.length - 1 return ( {!isLast && } ) })}
{focusedNode && descOf(focusedNode) && (

{descOf(focusedNode)}

)} {path.length > 0 && ( )} {currentNodes.length === 0 ? (
  • {t('findings.no_options')}
  • ) : ( currentNodes.map((node) => { const nodePath = [...path, node.id] if (isLeaf(node)) { const checked = isLeafSelected(nodePath) const nodeLabel = nameOf(node) const nodeDesc = descOf(node) return (
  • ) } const selectedCount = countSelectedUnder(nodePath) const nodeLabel = nameOf(node) const selectedAria = `${selectedCount} ${ selectedCount > 1 ? t('findings.selected_plural') : t('findings.selected_singular') }` return (
  • ) }) )}
    ) }