Debian Codegraph @

frontend/src/stores/index.ts

raw
1 import { derived, writable } from "svelte/store";
2
3 import type { BeancountError, LedgerData } from "../api/validators";
4 import { DEFAULT_INTERVAL } from "../lib/interval";
5 import { derived_array } from "../lib/store";
6
7 /** The current conversion used for reports. */
8 export const conversion = writable("");
9 /** The current interval used for reports. */
10 export const interval = writable(DEFAULT_INTERVAL);
11
12 /** The Beancount errors. */
13 export const errors = writable<readonly BeancountError[]>([]);
14
15 export const ledgerData = writable<LedgerData>();
16
17 /** Fava's options */
18 export const fava_options = derived(ledgerData, (v) => v.fava_options);
19 /** The customized currency conversion select list */
20 export const conversion_currencies = derived_array(
21 fava_options,
22 ($fava_options) => $fava_options.conversion_currencies,
23 );
24 export const locale = derived(
25 fava_options,
26 ($fava_options) => $fava_options.locale,
27 );
28
29 /** Beancount's options */
30 export const options = derived(ledgerData, (v) => v.options);
31 /** Beancount ledger title */
32 export const ledger_title = derived(options, ($options) => $options.title);
33 /** The operating currencies (sorted). */
34 export const operating_currency = derived_array(options, ($options) =>
35 [...$options.operating_currency].sort(),
36 );
37
38 /** Commodity display precisions. */
39 export const precisions = derived(ledgerData, (v) => v.precisions);
40 /** Whether Fava supports exporting to Excel. */
41 export const HAVE_EXCEL = derived(ledgerData, (v) => v.have_excel);
42 /** Whether Fava should obscure all numbers. */
43 export const incognito = derived(ledgerData, (v) => v.incognito);
44 /** Base URL. */
45 export const base_url = derived(ledgerData, (v) => v.base_url);
46 /** The Fava extensions. */
47 export const extensions = derived(ledgerData, (v) => v.extensions);
48
49 /** The ranked array of all accounts. */
50 export const accounts = derived_array(ledgerData, (v) => v.accounts);
51 export const accounts_set = derived(
52 accounts,
53 ($accounts) => new Set($accounts),
54 );
55
56 /** Get the name (as given per metadata) of a currency. */
57 export const currency_name = derived(
58 ledgerData,
59 ({ currency_names }) =>
60 (c: string) =>
61 currency_names[c] ?? c,
62 );
63
64 /** Account information. */
65 export const account_details = derived(ledgerData, (v) => v.account_details);
66 /** The ranked array of all currencies. */
67 export const currencies = derived_array(ledgerData, (v) => v.currencies);
68 /** The ranked array of all links. */
69 export const links = derived_array(ledgerData, (v) => v.links);
70 /** The ranked array of all payees. */
71 export const payees = derived_array(ledgerData, (v) => v.payees);
72 /** The ranked array of all tags. */
73 export const tags = derived_array(ledgerData, (v) => v.tags);
74 /** The array of all years. */
75 export const years = derived_array(ledgerData, (v) => v.years);
76
77 /** The sorted array of all used currencies. */
78 export const currencies_sorted = derived_array(currencies, ($currencies) =>
79 [...$currencies].sort(),
80 );