diff --git a/src/components/observation/FindingsTreePicker.tsx b/src/components/observation/FindingsTreePicker.tsx index 125cccbd..d072e882 100644 --- a/src/components/observation/FindingsTreePicker.tsx +++ b/src/components/observation/FindingsTreePicker.tsx @@ -25,6 +25,14 @@ function samePath(a: string[], b: string[]): boolean { 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[], @@ -79,6 +87,16 @@ export default function FindingsTreePicker({ ) } + 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( @@ -185,6 +203,7 @@ export default function FindingsTreePicker({ ) } + const selectedCount = countSelectedUnder(nodePath) return (