| 1 |
import { derived } from "svelte/store"; |
| 2 |
|
| 3 |
import { conversion, currencies, operating_currency } from "../stores"; |
| 4 |
import { currentDateFormat } from "../stores/format"; |
| 5 |
|
| 6 |
/** Context data for parsing and rendering of the charts. */ |
| 7 |
export interface ChartContext { |
| 8 |
/** The list of operating currencies, complemented by the current conversion currency. */ |
| 9 |
readonly currencies: readonly string[]; |
| 10 |
/** The current date format as determined from the interval. */ |
| 11 |
readonly dateFormat: (date: Date) => string; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* The list of operating currencies, adding in the current conversion currency. |
| 16 |
*/ |
| 17 |
const operatingCurrenciesWithConversion = derived( |
| 18 |
[operating_currency, currencies, conversion], |
| 19 |
([$operating_currency, $currencies, $conversion]) => |
| 20 |
$currencies.includes($conversion) && |
| 21 |
!$operating_currency.includes($conversion) |
| 22 |
? [...$operating_currency, $conversion] |
| 23 |
: $operating_currency, |
| 24 |
); |
| 25 |
|
| 26 |
export const chartContext = derived( |
| 27 |
[operatingCurrenciesWithConversion, currentDateFormat], |
| 28 |
([$operatingCurrenciesWithConversion, $currentDateFormat]): ChartContext => ({ |
| 29 |
currencies: $operatingCurrenciesWithConversion, |
| 30 |
dateFormat: $currentDateFormat, |
| 31 |
}), |
| 32 |
); |