anki/ts/graphs/GraphsPage.svelte

63 lines
1.6 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 pb from "anki/backend_proto";
import { getGraphData, RevlogRange } from "./graph-helpers";
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;
2020-12-25 22:29:42 +01:00
let revlogRange: RevlogRange;
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 = await getGraphData(search, days);
2020-12-26 18:24:24 +01:00
revlogRange = days > 365 || days === 0 ? RevlogRange.All : RevlogRange.Year;
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}
{revlogRange}
{i18n}
{nightMode} />
2020-12-25 22:59:11 +01:00
{/each}
</div>
{/if}