Debian Codegraph @

frontend/src/charts/ChartSwitcher.svelte

raw
1 <script lang="ts">
2 import { _ } from "../i18n";
3 import type { KeySpec } from "../keyboard-shortcuts";
4 import { keyboardShortcut } from "../keyboard-shortcuts";
5 import { lastActiveChartName, showCharts } from "../stores/chart";
6 import type { FavaChart } from ".";
7 import Chart from "./Chart.svelte";
8 import ConversionAndInterval from "./ConversionAndInterval.svelte";
9
10 interface Props {
11 charts: readonly FavaChart[];
12 }
13
14 let { charts }: Props = $props();
15
16 let active_chart = $derived(
17 charts.find((c) => c.name === $lastActiveChartName) ?? charts[0],
18 );
19
20 // Get the shortcut key for jumping to the previous chart.
21 let shortcutPrevious = $derived((index: number): KeySpec | undefined => {
22 const current = active_chart ? charts.indexOf(active_chart) : -1;
23 return index === (current - 1 + charts.length) % charts.length
24 ? { key: "C", note: _("Previous") }
25 : undefined;
26 });
27 // Get the shortcut key for jumping to the next chart.
28 let shortcutNext = $derived((index: number): KeySpec | undefined => {
29 const current = active_chart ? charts.indexOf(active_chart) : -1;
30 return index === (current + 1 + charts.length) % charts.length
31 ? { key: "c", note: _("Next") }
32 : undefined;
33 });
34 </script>
35
36 {#if active_chart}
37 <Chart chart={active_chart}>
38 <ConversionAndInterval />
39 </Chart>
40 <div hidden={!$showCharts}>
41 {#each charts as chart, index}
42 <button
43 type="button"
44 class="unset"
45 class:selected={chart === active_chart}
46 onclick={() => {
47 $lastActiveChartName = chart.name;
48 }}
49 use:keyboardShortcut={shortcutPrevious(index)}
50 use:keyboardShortcut={shortcutNext(index)}
51 >
52 {chart.name}
53 </button>
54 {/each}
55 </div>
56 {/if}
57
58 <style>
59 div {
60 margin-bottom: 1.5em;
61 font-size: 1em;
62 color: var(--text-color-lightest);
63 text-align: center;
64 }
65
66 button {
67 padding: 0 0.5em;
68 }
69
70 button + button {
71 border-left: 1px solid var(--text-color-lighter);
72 }
73
74 button.selected,
75 button:hover {
76 color: var(--text-color-lighter);
77 }
78
79 @media print {
80 button {
81 display: none;
82 border-left: none;
83 }
84
85 button + button {
86 border-left: none;
87 }
88
89 button.selected {
90 display: inline;
91 }
92 }
93 </style>