anki/ts/card-info/CardInfo.svelte

57 lines
1.6 KiB
Svelte
Raw Normal View History

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
2021-10-17 18:38:28 +02:00
import type { Stats } from "../lib/proto";
2021-10-17 20:29:32 +02:00
import { getCardStats } from "./lib";
import Container from "../components/Container.svelte";
import Row from "../components/Row.svelte";
2021-10-17 18:38:28 +02:00
import CardStats from "./CardStats.svelte";
import CardInfoPlaceholder from "./CardInfoPlaceholder.svelte";
import Revlog from "./Revlog.svelte";
2021-10-18 09:11:00 +02:00
export let cardId: number | null = null;
2021-10-18 09:04:49 +02:00
export let includeRevlog: boolean = true;
2021-10-17 20:29:32 +02:00
2021-10-18 09:29:33 +02:00
let stats: Stats.CardStatsResponse | null = null;
let revlog: Stats.CardStatsResponse.StatsRevlogEntry[] | null = null;
2021-10-17 20:29:32 +02:00
async function updateStats(cardId: number): Promise<void> {
2021-10-18 09:12:10 +02:00
const requestedCardId = cardId;
const cardStats = await getCardStats(requestedCardId);
/* Skip if another update has been triggered in the meantime. */
if (requestedCardId === cardId) {
stats = cardStats;
if (includeRevlog) {
revlog = stats.revlog as Stats.CardStatsResponse.StatsRevlogEntry[];
}
}
}
$: if (cardId) {
updateStats(cardId);
} else {
stats = null;
revlog = null;
2021-10-17 22:06:44 +02:00
}
</script>
<Container breakpoint="md" --gutter-inline="0.25rem" --gutter-block="0.5rem">
{#if stats}
<Row>
2021-10-17 20:29:32 +02:00
<CardStats {stats} />
</Row>
2021-10-17 20:29:32 +02:00
{#if revlog}
<Row>
<Revlog {revlog} />
</Row>
{/if}
{:else}
<CardInfoPlaceholder />
{/if}
</Container>