aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/atoms/cards.ts
blob: 4a6e72ec85a364296d4c05e382bcd245daa74461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { atomFamily } from "jotai-family";
import { atomWithSuspenseQuery } from "jotai-tanstack-query";
import { apiClient } from "../api/client";
import type { CardStateType } from "../db";

export interface Card {
	id: string;
	deckId: string;
	noteId: string;
	isReversed: boolean;
	front: string;
	back: string;
	state: CardStateType;
	due: string;
	stability: number;
	difficulty: number;
	elapsedDays: number;
	scheduledDays: number;
	reps: number;
	lapses: number;
	lastReview: string | null;
	createdAt: string;
	updatedAt: string;
	deletedAt: string | null;
	syncVersion: number;
}

// =====================
// Cards by Deck - Suspense-compatible
// =====================

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;
		},
	})),
);