diff --git a/frontend/src/components/AccountTreeGrid.tsx b/frontend/src/components/AccountTreeGrid.tsx index 3ce6b68c..908aacf2 100644 --- a/frontend/src/components/AccountTreeGrid.tsx +++ b/frontend/src/components/AccountTreeGrid.tsx @@ -1,4 +1,4 @@ -import { useState, useMemo, useCallback } from 'react' +import { useState, useMemo, useCallback, useRef, useEffect } from 'react' import { Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Paper, IconButton, Box, Typography, @@ -17,77 +17,59 @@ export interface AccountTreeGridProps { nodes: TreeNode[] columnHeaders: string[] title?: string - currencyCode?: string onDownloadCsv?: () => void - /** Show sign-reversed display for result accounts (Income/Cost) */ reverseSign?: boolean } -interface FlatRow { - node: TreeNode - depth: number - isLeaf: boolean - hasOwnValue: boolean +interface BuiltNode { + id: number + name: string + parentId: number + ownValues: number[] + children: BuiltNode[] aggregatedValues: number[] + isLeaf: boolean + depth: number } -function buildTree(nodes: TreeNode[]): TreeNode[] { - const byId = new Map() - const roots: TreeNode[] = [] +function buildTree(flatNodes: TreeNode[], colCount: number): BuiltNode[] { + const byId = new Map() - for (const n of nodes) { - byId.set(n.id, { ...n, children: [] }) + for (const n of flatNodes) { + byId.set(n.id, { + id: n.id, name: n.name, parentId: n.parentId, + ownValues: n.ownValues, + children: [], aggregatedValues: new Array(colCount).fill(0), + isLeaf: true, depth: 0, + }) } - for (const n of nodes) { + const roots: BuiltNode[] = [] + for (const n of flatNodes) { const node = byId.get(n.id)! if (n.parentId === 0 || !byId.has(n.parentId)) { roots.push(node) } else { - byId.get(n.parentId)!.children!.push(node) + const parent = byId.get(n.parentId)! + parent.children.push(node) + parent.isLeaf = false } } - return roots -} - -function computeAggregates(node: TreeNode, colCount: number): number[] { - const sums = new Array(colCount).fill(0) - for (let i = 0; i < colCount; i++) { - sums[i] = node.ownValues[i] || 0 - } - if (node.children) { + function computeAgg(node: BuiltNode, depth: number): number[] { + node.depth = depth + const sums = new Array(colCount).fill(0) + for (let i = 0; i < colCount; i++) sums[i] = node.ownValues[i] || 0 for (const child of node.children) { - const childSums = computeAggregates(child, colCount) - for (let i = 0; i < colCount; i++) { - sums[i] += childSums[i] - } - } - } - return sums -} - -function flattenVisible( - nodes: TreeNode[], - expanded: Set, - colCount: number, - depth: number = 0 -): FlatRow[] { - const rows: FlatRow[] = [] - - for (const node of nodes) { - const isLeaf = !node.children || node.children.length === 0 - const hasOwnValue = (node.ownValues || []).some(v => v !== 0) - const aggregatedValues = isLeaf ? node.ownValues : computeAggregates(node, colCount) - - rows.push({ node, depth, isLeaf, hasOwnValue, aggregatedValues }) - - if (!isLeaf && expanded.has(node.id)) { - rows.push(...flattenVisible(node.children!, expanded, colCount, depth + 1)) + const childSums = computeAgg(child, depth + 1) + for (let i = 0; i < colCount; i++) sums[i] += childSums[i] } + node.aggregatedValues = sums + return sums } - return rows + for (const root of roots) computeAgg(root, 0) + return roots } function formatValue(cents: number, reverseSign: boolean): string { @@ -97,33 +79,62 @@ function formatValue(cents: number, reverseSign: boolean): string { minimumFractionDigits: 0, maximumFractionDigits: 0, }) - return val < 0 ? `−${formatted}` : val === 0 ? '0' : formatted + if (val === 0) return '0' + return val < 0 ? `−${formatted}` : formatted +} + +function AnimatedRow({ visible, children }: { visible: boolean; children: React.ReactNode }) { + const rowRef = useRef(null) + const [height, setHeight] = useState(undefined) + const [mounted, setMounted] = useState(visible) + + useEffect(() => { + if (visible) { + setMounted(true) + requestAnimationFrame(() => { + if (rowRef.current) setHeight(rowRef.current.scrollHeight) + }) + } else { + setHeight(0) + const timer = setTimeout(() => setMounted(false), 300) + return () => clearTimeout(timer) + } + }, [visible]) + + useEffect(() => { + if (mounted && visible && rowRef.current) { + setHeight(rowRef.current.scrollHeight) + } + }, [mounted, visible]) + + if (!mounted) return null + + return ( + + {children} + + ) } export default function AccountTreeGrid({ - nodes, - columnHeaders, - title, - onDownloadCsv, - reverseSign = false, + nodes, columnHeaders, title, onDownloadCsv, reverseSign = false, }: AccountTreeGridProps) { - const [expanded, setExpanded] = useState>(() => { - const topLevel = new Set() - for (const n of nodes) { - if (n.children && n.children.length > 0 && n.parentId === 0) { - topLevel.add(n.id) - } - } - return topLevel - }) - - const tree = useMemo(() => buildTree(nodes), [nodes]) const colCount = columnHeaders.length + const tree = useMemo(() => buildTree(nodes, colCount), [nodes, colCount]) - const visibleRows = useMemo( - () => flattenVisible(tree, expanded, colCount), - [tree, expanded, colCount] - ) + const [expanded, setExpanded] = useState>(() => { + const initial = new Set() + for (const root of tree) if (!root.isLeaf) initial.add(root.id) + return initial + }) const toggleExpand = useCallback((id: number) => { setExpanded(prev => { @@ -134,26 +145,41 @@ export default function AccountTreeGrid({ }) }, []) + // Flatten the FULL tree, tracking which rows should be visible + const allRows = useMemo(() => { + const rows: { node: BuiltNode; visible: boolean }[] = [] + function walk(nodes: BuiltNode[], parentVisible: boolean) { + for (const node of nodes) { + rows.push({ node, visible: parentVisible }) + if (!node.isLeaf) { + walk(node.children, parentVisible && expanded.has(node.id)) + } + } + } + walk(tree, true) + return rows + }, [tree, expanded]) + const totals = useMemo(() => { const sums = new Array(colCount).fill(0) - for (const node of tree) { - const agg = computeAggregates(node, colCount) - for (let i = 0; i < colCount; i++) sums[i] += agg[i] + for (const root of tree) { + for (let i = 0; i < colCount; i++) sums[i] += root.aggregatedValues[i] } return sums }, [tree, colCount]) + // Fixed column widths: account name gets flex, value columns are fixed + const valColWidth = Math.max(90, Math.min(120, 700 / colCount)) + return ( {title && ( {title} {onDownloadCsv && ( - + onClick={onDownloadCsv}> Download CSV )} @@ -161,100 +187,63 @@ export default function AccountTreeGrid({ )} - +
+ + + {columnHeaders.map((_, i) => ( + + ))} + - Account + Account {columnHeaders.map((h, i) => ( - - {h} - + {h} ))} - {visibleRows.map(row => { - const isExpanded = expanded.has(row.node.id) - const isParent = !row.isLeaf - const showAggregate = isParent && !isExpanded + {allRows.map(({ node, visible }) => { + const isParent = !node.isLeaf + const isExpanded = expanded.has(node.id) + const isCollapsed = isParent && !isExpanded + if (node.depth === 0) { + return ( + + {renderAccountCell(node, isParent, isExpanded, () => toggleExpand(node.id))} + {renderValueCells(node, isCollapsed, reverseSign)} + + ) + } return ( - - - {isParent ? ( - toggleExpand(row.node.id)} sx={{ p: 0.25 }}> - {isExpanded ? : } - - ) : ( - - )} - {isParent ? ( - isExpanded ? - : - ) : ( - - )} - - {row.node.name} - - - - - {row.aggregatedValues.map((val, i) => ( - - - {showAggregate && val !== 0 && ( - - Σ - - )} - {isParent && isExpanded && row.hasOwnValue ? ( - formatValue(row.node.ownValues[i] || 0, reverseSign) - ) : ( - formatValue(val, reverseSign) - )} - - - ))} + {renderAnimatedAccountCell(node, isParent, isExpanded, () => toggleExpand(node.id), visible)} + {renderAnimatedValueCells(node, isCollapsed, reverseSign, visible)} ) })} - {/* Totals row */} - + Total {totals.map((val, i) => ( {formatValue(val, reverseSign)} @@ -266,3 +255,174 @@ export default function AccountTreeGrid({ ) } + +function DualValueCell({ aggVal, ownVal, isCollapsed, reverseSign, showOwnEmpty }: { + aggVal: number; ownVal: number; isCollapsed: boolean; reverseSign: boolean; showOwnEmpty: boolean +}) { + return ( + + + {aggVal !== 0 && ( + Σ + )} + {formatValue(aggVal, reverseSign)} + + + {showOwnEmpty ? '' : formatValue(ownVal, reverseSign)} + + + ) +} + +const ROW_HEIGHT = 28 +const CELL_PX = '3px 8px' + +function AnimatedCellContent({ visible, children, align }: { + visible: boolean; children: React.ReactNode; align?: 'right' | 'left' +}) { + return ( +
+
+ {children} +
+
+ ) +} + +function renderAnimatedAccountCell( + node: BuiltNode, isParent: boolean, isExpanded: boolean, onToggle: () => void, visible: boolean +) { + return ( + + + + {isParent ? ( + + + + + + ) : ( + + )} + {isParent + ? (isExpanded + ? + : ) + : } + + {node.name} + + + + + ) +} + +function renderAnimatedValueCells( + node: BuiltNode, isCollapsed: boolean, + reverseSign: boolean, visible: boolean +) { + return node.aggregatedValues.map((aggVal, i) => { + const ownVal = node.ownValues[i] || 0 + const isParent = !node.isLeaf + const showOwnEmpty = isParent && ownVal === 0 + + return ( + + + {isParent ? ( + + ) : ( + + {formatValue(ownVal, reverseSign)} + + )} + + + ) + }) +} + +function renderAccountCell( + node: BuiltNode, isParent: boolean, isExpanded: boolean, onToggle: () => void +) { + return ( + + + {isParent ? ( + + + + + + ) : ( + + )} + {isParent + ? (isExpanded + ? + : ) + : } + + {node.name} + + + + ) +} + +function renderValueCells( + node: BuiltNode, isCollapsed: boolean, reverseSign: boolean +) { + return node.aggregatedValues.map((aggVal, i) => { + const ownVal = node.ownValues[i] || 0 + const isParent = !node.isLeaf + const showOwnEmpty = isParent && ownVal === 0 + + return ( + + {isParent ? ( + + ) : ( + + {formatValue(ownVal, reverseSign)} + + )} + + ) + }) +}