Debian Codegraph @

frontend/src/stores/chart.ts

raw
1 import { derived, writable } from "svelte/store";
2
3 import { _ } from "../i18n";
4 import iso4217currencies from "../lib/iso4217";
5 import { localStorageSyncedStore } from "../lib/store";
6 import type { ValidationT } from "../lib/validation";
7 import { array, constants, string } from "../lib/validation";
8 import {
9 conversion_currencies,
10 currencies_sorted,
11 operating_currency,
12 } from ".";
13
14 /** Whether the charts should be shown - this applies globally to all charts. */
15 export const showCharts = writable(true);
16
17 /** This store is used to switch to the same chart (as identified by name) on navigation. */
18 export const lastActiveChartName = writable<string | null>(null);
19
20 const hierarchy_chart_mode_validator = constants("treemap", "sunburst");
21 type HierarchyChartMode = ValidationT<typeof hierarchy_chart_mode_validator>;
22
23 /** The currently selected hierarchy chart mode. */
24 export const hierarchyChartMode = localStorageSyncedStore<HierarchyChartMode>(
25 "hierarchy-chart-mode",
26 hierarchy_chart_mode_validator,
27 () => "treemap",
28 () => [
29 ["treemap", _("Treemap")],
30 ["sunburst", _("Sunburst")],
31 ],
32 );
33
34 const line_chart_mode_validator = constants("line", "area");
35 type LineChartMode = ValidationT<typeof line_chart_mode_validator>;
36
37 /** The currently selected line chart mode. */
38 export const lineChartMode = localStorageSyncedStore<LineChartMode>(
39 "line-chart-mode",
40 line_chart_mode_validator,
41 () => "line",
42 () => [
43 ["line", _("Line chart")],
44 ["area", _("Area chart")],
45 ],
46 );
47
48 const bar_chart_mode_validator = constants("stacked", "single");
49 type BarChartMode = ValidationT<typeof bar_chart_mode_validator>;
50
51 /** The currently selected bar chart mode. */
52 export const barChartMode = localStorageSyncedStore<BarChartMode>(
53 "bar-chart-mode",
54 bar_chart_mode_validator,
55 () => "stacked",
56 () => [
57 ["stacked", _("Stacked Bars")],
58 ["single", _("Single Bars")],
59 ],
60 );
61
62 /** The currencies that are currently not shown in the bar and line charts. */
63 export const chartToggledCurrencies = localStorageSyncedStore(
64 "chart-toggled-currencies",
65 array(string),
66 () => [],
67 );
68
69 /** The currencies to over as conversion options. */
70 const currency_suggestions = derived(
71 [operating_currency, currencies_sorted, conversion_currencies],
72 ([$operating_currency, $currencies_sorted, $conversion_currencies]) =>
73 $conversion_currencies.length > 0
74 ? $conversion_currencies
75 : [
76 ...$operating_currency,
77 ...$currencies_sorted.filter(
78 (c) => !$operating_currency.includes(c) && iso4217currencies.has(c),
79 ),
80 ],
81 );
82
83 /** The possible conversion options and their human-readable descriptions. */
84 export const conversions = derived(
85 currency_suggestions,
86 ($currency_suggestions): readonly string[] => [
87 "at_cost",
88 "at_value",
89 "units",
90 ...$currency_suggestions,
91 ],
92 );