Debian Codegraph @

frontend/src/tree-table/IntervalTreeTableNode.svelte

raw
1 <script lang="ts">
2 import type { AccountBudget } from "../api/validators";
3 import type { AccountTreeNode } from "../charts/hierarchy";
4 import type { NonEmptyArray } from "../lib/array";
5 import { is_empty } from "../lib/objects";
6 import { currency_name } from "../stores";
7 import { ctx } from "../stores/format";
8 import AccountCell from "./AccountCell.svelte";
9 import Diff from "./Diff.svelte";
10 import { getTreeTableContext } from "./helpers";
11 import IntervalTreeTableNode from "./IntervalTreeTableNode.svelte";
12
13 interface Props {
14 /** The account nodes to show. */
15 nodes: NonEmptyArray<AccountTreeNode>;
16 /** The budgets (per account a list per date range). */
17 budgets: Record<string, AccountBudget[]>;
18 }
19
20 let { nodes, budgets }: Props = $props();
21
22 const { toggled, not_shown } = getTreeTableContext();
23
24 let [node] = $derived(nodes);
25 let { account, children } = $derived(node);
26 let account_budgets = $derived(budgets[account]);
27
28 let is_toggled = $derived($toggled.has(account));
29 </script>
30
31 <li>
32 <p>
33 <AccountCell {node} />
34 {#each nodes as n, index}
35 {@const account_budget = account_budgets?.[index]}
36 {@const has_balance =
37 !is_empty(n.balance) ||
38 (account_budget != null && !is_empty(account_budget.budget))}
39 {@const show_balance = !is_toggled && has_balance}
40 {@const shown_balance = show_balance ? n.balance : n.balance_children}
41 {@const shown_budget = show_balance
42 ? account_budget?.budget
43 : account_budget?.budget_children}
44 <span class="num other" class:dimmed={!is_toggled && !has_balance}>
45 {#each Object.entries(shown_balance) as [currency, number]}
46 {@const budget = shown_budget?.[currency]}
47 <span title={$currency_name(currency)}>
48 {$ctx.amount(number, currency)}
49 </span>
50 {#if budget}
51 <Diff diff={budget - number} num={budget} {currency} />
52 {/if}
53 <br />
54 {/each}
55 {#if shown_budget}
56 {#each Object.entries(shown_budget).filter(([c]) => !(shown_balance[c] ?? 0)) as [currency, budget]}
57 <span title={$currency_name(currency)}>
58 {$ctx.amount(0, currency)}
59 </span>
60 <Diff diff={budget} num={budget} {currency} />
61 <br />
62 {/each}
63 {/if}
64 </span>
65 {/each}
66 </p>
67 {#if !is_toggled && children.some((n) => !$not_shown.has(n.account))}
68 <ol>
69 {#each children as child, index (child.account)}
70 {#if !$not_shown.has(child.account)}
71 <IntervalTreeTableNode
72 nodes={nodes.map(
73 (n) => n.children[index],
74 ) as unknown as NonEmptyArray<AccountTreeNode>}
75 {budgets}
76 />
77 {/if}
78 {/each}
79 </ol>
80 {/if}
81 </li>