anki/ts/graphs/GraphsPage.svelte

72 lines
1.9 KiB
Svelte
Raw Normal View History

<script context="module">
</script>
<script lang="typescript">
2020-12-27 02:04:45 +01:00
import type { SvelteComponent } from "svelte/internal";
import type { I18n } from "anki/i18n";
import type { PreferenceStore } from "./preferences";
import type pb from "anki/backend_proto";
import { getGraphData, RevlogRange, daysToRevlogRange } from "./graph-helpers";
import { getPreferences } from "./preferences";
2020-06-27 11:24:49 +02:00
export let i18n: I18n;
2020-06-30 08:23:46 +02:00
export let nightMode: boolean;
export let graphs: SvelteComponent[];
2020-06-27 11:24:49 +02:00
2020-12-22 22:25:47 +01:00
export let search: string;
2020-12-25 22:29:42 +01:00
export let days: number;
export let controller: SvelteComponent | null;
2020-12-22 22:25:47 +01:00
let active = false;
2020-12-25 22:59:11 +01:00
let sourceData: pb.BackendProto.GraphsOut | null = null;
let preferences: PreferenceStore | null = null;
2020-12-25 22:29:42 +01:00
let revlogRange: RevlogRange;
const preferencesPromise = getPreferences();
2020-12-25 22:59:11 +01:00
const refreshWith = async (search: string, days: number) => {
active = true;
2020-12-25 22:59:11 +01:00
try {
[sourceData, preferences] = await Promise.all([
getGraphData(search, days),
preferencesPromise,
]);
revlogRange = daysToRevlogRange(days);
2020-12-25 22:59:11 +01:00
} catch (e) {
sourceData = null;
alert(i18n.tr(i18n.TR.STATISTICS_ERROR_FETCHING));
}
active = false;
2020-12-26 18:24:24 +01:00
};
2020-06-22 12:28:36 +02:00
2020-12-25 22:59:11 +01:00
const refresh = (event: CustomEvent) => {
2020-12-26 18:24:24 +01:00
refreshWith(event.detail.search, event.detail.days);
};
2020-12-26 18:24:24 +01:00
refreshWith(search, days);
</script>
{#if controller}
2020-12-26 18:24:24 +01:00
<svelte:component
this={controller}
{i18n}
{search}
{days}
{active}
on:update={refresh} />
{/if}
2020-12-25 22:59:11 +01:00
{#if sourceData}
<div tabindex="-1" class="no-focus-outline">
{#each graphs as graph}
2020-12-26 18:24:24 +01:00
<svelte:component
this={graph}
{sourceData}
{preferences}
2020-12-26 18:24:24 +01:00
{revlogRange}
{i18n}
{nightMode} />
2020-12-25 22:59:11 +01:00
{/each}
</div>
{/if}