| 1 |
<script lang="ts"> |
| 2 |
import type { AccountTreeNode } from "../charts/hierarchy"; |
| 3 |
import { is_empty } from "../lib/objects"; |
| 4 |
import { currency_name, operating_currency } from "../stores"; |
| 5 |
import { ctx } from "../stores/format"; |
| 6 |
import AccountCell from "./AccountCell.svelte"; |
| 7 |
import Diff from "./Diff.svelte"; |
| 8 |
import { getTreeTableContext } from "./helpers"; |
| 9 |
import TreeTableNode from "./TreeTableNode.svelte"; |
| 10 |
|
| 11 |
interface Props { |
| 12 |
/** The account node to show. */ |
| 13 |
node: AccountTreeNode; |
| 14 |
/** Whther to invert all numbers (either `1` or `-1`). */ |
| 15 |
invert: number; |
| 16 |
} |
| 17 |
|
| 18 |
let { node, invert }: Props = $props(); |
| 19 |
|
| 20 |
const { toggled, not_shown } = getTreeTableContext(); |
| 21 |
|
| 22 |
let { account, children } = $derived(node); |
| 23 |
|
| 24 |
let is_toggled = $derived($toggled.has(account)); |
| 25 |
|
| 26 |
let has_balance = $derived(!is_empty(node.balance)); |
| 27 |
/** Whether to show the balance (or balance_children) */ |
| 28 |
let show_balance = $derived(!is_toggled && has_balance); |
| 29 |
let shown_balance = $derived( |
| 30 |
show_balance ? node.balance : node.balance_children, |
| 31 |
); |
| 32 |
let shown_cost = $derived(show_balance ? node.cost : node.cost_children); |
| 33 |
let shown_balance_other = $derived( |
| 34 |
Object.entries(shown_balance) |
| 35 |
.sort() |
| 36 |
.filter(([c]) => !$operating_currency.includes(c)), |
| 37 |
); |
| 38 |
let dimmed = $derived(!is_toggled && !has_balance); |
| 39 |
</script> |
| 40 |
|
| 41 |
<li> |
| 42 |
<p> |
| 43 |
<AccountCell {node} /> |
| 44 |
{#each $operating_currency as currency} |
| 45 |
{@const num = shown_balance[currency]} |
| 46 |
{@const cost_num = shown_cost?.[currency] ?? 0} |
| 47 |
<span class="num" class:dimmed> |
| 48 |
{#if num} |
| 49 |
{$ctx.num(invert * num, currency)} |
| 50 |
{#if cost_num && num - cost_num} |
| 51 |
{@const diff = invert * (num - cost_num)} |
| 52 |
<Diff {diff} num={invert * cost_num} {currency} /> |
| 53 |
{/if} |
| 54 |
{/if} |
| 55 |
</span> |
| 56 |
{/each} |
| 57 |
<span class="num other" class:dimmed> |
| 58 |
{#each shown_balance_other as [currency, num]} |
| 59 |
{@const cost_num = shown_cost?.[currency] ?? 0} |
| 60 |
<span title={$currency_name(currency)}> |
| 61 |
{$ctx.amount(invert * num, currency)} |
| 62 |
</span> |
| 63 |
{#if cost_num && num - cost_num} |
| 64 |
{@const diff = invert * (num - cost_num)} |
| 65 |
<Diff {diff} num={invert * cost_num} {currency} /> |
| 66 |
{/if} |
| 67 |
<br /> |
| 68 |
{/each} |
| 69 |
</span> |
| 70 |
</p> |
| 71 |
{#if !is_toggled && children.some((n) => !$not_shown.has(n.account))} |
| 72 |
<ol> |
| 73 |
{#each children.filter((n) => !$not_shown.has(n.account)) as child (child.account)} |
| 74 |
<TreeTableNode node={child} {invert} /> |
| 75 |
{/each} |
| 76 |
</ol> |
| 77 |
{/if} |
| 78 |
</li> |