success pt. 2
This commit is contained in:
@@ -1,70 +1,26 @@
|
||||
<script lang="ts">
|
||||
import { desc } from "drizzle-orm";
|
||||
import { onMount } from "svelte";
|
||||
import { db, overwriteDatabaseFile } from "$lib/db/client-db";
|
||||
import { animeTable } from "$lib/db/schema";
|
||||
import { getAnimeList } from "$lib/db/client-amq";
|
||||
|
||||
type AnimeListItem = {
|
||||
annId: number;
|
||||
mainName: string;
|
||||
year: number;
|
||||
seasonId: number;
|
||||
malId: number;
|
||||
};
|
||||
|
||||
const SEED_URL = "/data/amq.sqlite";
|
||||
const SEED_VERSION = "amq.sqlite.v1"; // bump when static/data/amq.sqlite changes
|
||||
const SEED_KEY = "amq.seed.version";
|
||||
|
||||
let status = $state<"idle" | "loading" | "ready" | "error">("idle");
|
||||
let error = $state<string | null>(null);
|
||||
let anime = $state<AnimeListItem[]>([]);
|
||||
|
||||
async function ensureSeeded() {
|
||||
const current = localStorage.getItem(SEED_KEY);
|
||||
if (current === SEED_VERSION) return;
|
||||
|
||||
const res = await fetch(SEED_URL, { cache: "no-cache" });
|
||||
if (!res.ok) {
|
||||
throw new Error(
|
||||
`Failed to fetch seed DB: ${res.status} ${res.statusText}`,
|
||||
);
|
||||
}
|
||||
|
||||
const stream = res.body;
|
||||
if (stream) {
|
||||
await overwriteDatabaseFile(stream);
|
||||
} else {
|
||||
const blob = await res.blob();
|
||||
await overwriteDatabaseFile(blob);
|
||||
}
|
||||
|
||||
localStorage.setItem(SEED_KEY, SEED_VERSION);
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
(async () => {
|
||||
status = "loading";
|
||||
error = null;
|
||||
|
||||
try {
|
||||
await ensureSeeded();
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
annId: animeTable.annId,
|
||||
mainName: animeTable.mainName,
|
||||
year: animeTable.year,
|
||||
seasonId: animeTable.seasonId,
|
||||
})
|
||||
.from(animeTable)
|
||||
.orderBy(
|
||||
desc(animeTable.year),
|
||||
desc(animeTable.seasonId),
|
||||
desc(animeTable.annId),
|
||||
)
|
||||
.limit(20);
|
||||
|
||||
anime = rows;
|
||||
anime = await getAnimeList(20);
|
||||
status = "ready";
|
||||
} catch (e) {
|
||||
error = e instanceof Error ? e.message : String(e);
|
||||
@@ -109,7 +65,7 @@
|
||||
<div class="font-medium">{a.mainName}</div>
|
||||
<div class="text-sm text-muted-foreground">
|
||||
{a.year}
|
||||
{seasonName(a.seasonId)} • ANN {a.annId}
|
||||
{seasonName(a.seasonId)} • ANN {a.annId} • MAL {a.malId}
|
||||
</div>
|
||||
</li>
|
||||
{/each}
|
||||
|
||||
Reference in New Issue
Block a user