| 1 |
<script lang="ts"> |
| 2 |
import { intersection, min } from "d3-array"; |
| 3 |
import { writable } from "svelte/store"; |
| 4 |
|
| 5 |
import type { AccountBudget } from "../api/validators"; |
| 6 |
import type { AccountTreeNode } from "../charts/hierarchy"; |
| 7 |
import { urlForAccount } from "../helpers"; |
| 8 |
import type { NonEmptyArray } from "../lib/array"; |
| 9 |
import { collapse_account } from "../stores/accounts"; |
| 10 |
import { currentTimeFilterDateFormat } from "../stores/format"; |
| 11 |
import AccountCellHeader from "./AccountCellHeader.svelte"; |
| 12 |
import { get_collapsed, get_not_shown, setTreeTableContext } from "./helpers"; |
| 13 |
import IntervalTreeTableNode from "./IntervalTreeTableNode.svelte"; |
| 14 |
|
| 15 |
interface Props { |
| 16 |
/** The account trees to show. */ |
| 17 |
trees: NonEmptyArray<AccountTreeNode>; |
| 18 |
/** The dates. */ |
| 19 |
dates: { begin: Date; end: Date }[]; |
| 20 |
/** The budgets (per account a list per date range). */ |
| 21 |
budgets: Record<string, AccountBudget[]>; |
| 22 |
/** Whether this is cumulative. */ |
| 23 |
accumulate: boolean; |
| 24 |
} |
| 25 |
|
| 26 |
let { trees, dates, budgets, accumulate }: Props = $props(); |
| 27 |
|
| 28 |
// Initialize context. |
| 29 |
// toggled is computed once on initialisation; not_shown is kept updated. |
| 30 |
const toggled = writable(get_collapsed(trees[0], $collapse_account)); |
| 31 |
const not_shown = writable(new Set<string>()); |
| 32 |
setTreeTableContext({ toggled, not_shown }); |
| 33 |
|
| 34 |
$effect(() => { |
| 35 |
$not_shown = intersection( |
| 36 |
...trees.map((n, index) => $get_not_shown(n, dates[index]?.end ?? null)), |
| 37 |
); |
| 38 |
}); |
| 39 |
|
| 40 |
let account = $derived(trees[0].account); |
| 41 |
let start_date = $derived( |
| 42 |
accumulate ? min(dates, (d) => d.begin) : undefined, |
| 43 |
); |
| 44 |
let start_date_filter = $derived( |
| 45 |
start_date ? $currentTimeFilterDateFormat(start_date) : undefined, |
| 46 |
); |
| 47 |
let time_filters = $derived( |
| 48 |
dates.map((date_range): [string, string] => { |
| 49 |
const title = $currentTimeFilterDateFormat(date_range.begin); |
| 50 |
return start_date_filter != null |
| 51 |
? [title, `${start_date_filter}-${title}`] |
| 52 |
: [title, title]; |
| 53 |
}), |
| 54 |
); |
| 55 |
</script> |
| 56 |
|
| 57 |
<ol class="flex-table tree-table-new"> |
| 58 |
<li class="head"> |
| 59 |
<p> |
| 60 |
<AccountCellHeader /> |
| 61 |
{#each time_filters as [title, time]} |
| 62 |
<span class="num other"> |
| 63 |
<a href={$urlForAccount(account, { time })}> |
| 64 |
{title} |
| 65 |
</a> |
| 66 |
</span> |
| 67 |
{/each} |
| 68 |
</p> |
| 69 |
</li> |
| 70 |
<IntervalTreeTableNode nodes={trees} {budgets} /> |
| 71 |
</ol> |
| 72 |
|
| 73 |
<style> |
| 74 |
ol { |
| 75 |
overflow-x: auto; |
| 76 |
} |
| 77 |
</style> |