aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/atoms/cards.ts
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-05-02 11:11:53 +0900
committernsfisis <nsfisis@gmail.com>2026-05-02 11:12:00 +0900
commit7ca9941982a7d7a4c126d215770ce71ad2f7f427 (patch)
tree0178b48094e9b7b143fd47c4d8479d3d588bb1d7 /src/client/atoms/cards.ts
parent8f1a08fefee3a8e928baec741c830a88a4cd7200 (diff)
downloadkioku-7ca9941982a7d7a4c126d215770ce71ad2f7f427.tar.gz
kioku-7ca9941982a7d7a4c126d215770ce71ad2f7f427.tar.zst
kioku-7ca9941982a7d7a4c126d215770ce71ad2f7f427.zip
feat(client): read decks/cards/study from IndexedDB first
Switch deckAtom, cardsByDeckAtomFamily, noteTypesAtom, and studyDataAtom to a stale-while-revalidate pattern: read from IndexedDB synchronously, trigger sync in the background, and refetch on sync_complete. When local is empty, await a single bootstrap pull before deciding there's no data. Add study-builder to assemble StudyCards from LocalCard + Note + NoteType + field values, replacing the server /study endpoint dependency. The study session can now run end-to-end offline. Disable submit on all write modals when offline since writes still require the server. Add a "Showing cached data" hint to the sync status indicator. Drop cacheStudyCards (cards arrive via regular sync pull now) and update page tests to reflect that lists no longer refresh by hitting the GET API.
Diffstat (limited to 'src/client/atoms/cards.ts')
-rw-r--r--src/client/atoms/cards.ts51
1 files changed, 42 insertions, 9 deletions
diff --git a/src/client/atoms/cards.ts b/src/client/atoms/cards.ts
index 4a6e72e..7771cdb 100644
--- a/src/client/atoms/cards.ts
+++ b/src/client/atoms/cards.ts
@@ -1,7 +1,8 @@
import { atomFamily } from "jotai-family";
import { atomWithSuspenseQuery } from "jotai-tanstack-query";
-import { apiClient } from "../api/client";
-import type { CardStateType } from "../db";
+import type { CardStateType, LocalCard } from "../db";
+import { localCardRepository } from "../db/repositories";
+import { ensureBootstrap } from "./sync";
export interface Card {
id: string;
@@ -25,19 +26,51 @@ export interface Card {
syncVersion: number;
}
+function localCardToView(card: LocalCard): Card {
+ return {
+ id: card.id,
+ deckId: card.deckId,
+ noteId: card.noteId,
+ isReversed: card.isReversed,
+ front: card.front,
+ back: card.back,
+ state: card.state,
+ due: card.due.toISOString(),
+ stability: card.stability,
+ difficulty: card.difficulty,
+ elapsedDays: card.elapsedDays,
+ scheduledDays: card.scheduledDays,
+ reps: card.reps,
+ lapses: card.lapses,
+ lastReview: card.lastReview ? card.lastReview.toISOString() : null,
+ createdAt: card.createdAt.toISOString(),
+ updatedAt: card.updatedAt.toISOString(),
+ deletedAt: card.deletedAt ? card.deletedAt.toISOString() : null,
+ syncVersion: card.syncVersion,
+ };
+}
+
+async function loadCardsByDeck(deckId: string): Promise<Card[]> {
+ const cards = await localCardRepository.findByDeckId(deckId);
+ cards.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
+ return cards.map(localCardToView);
+}
+
// =====================
-// Cards by Deck - Suspense-compatible
+// Cards by Deck - Suspense-compatible, IndexedDB-first
// =====================
export const cardsByDeckAtomFamily = atomFamily((deckId: string) =>
atomWithSuspenseQuery(() => ({
queryKey: ["decks", deckId, "cards"],
- queryFn: async () => {
- const res = await apiClient.rpc.api.decks[":deckId"].cards.$get({
- param: { deckId },
- });
- const data = await apiClient.handleResponse<{ cards: Card[] }>(res);
- return data.cards;
+ queryFn: async (): Promise<Card[]> => {
+ const cards = await loadCardsByDeck(deckId);
+ if (cards.length > 0) {
+ ensureBootstrap();
+ return cards;
+ }
+ await ensureBootstrap();
+ return loadCardsByDeck(deckId);
},
})),
);