aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/atoms/decks.ts
blob: 5a4d44e6fa3bf8eb66ea8efab0f6f1d80cb9d635 (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
44
import { atomFamily } from "jotai-family";
import { atomWithSuspenseQuery } from "jotai-tanstack-query";
import { apiClient } from "../api/client";

export interface Deck {
	id: string;
	name: string;
	description: string | null;
	dueCardCount: number;
	createdAt: string;
	updatedAt: string;
}

// =====================
// Decks List - Suspense-compatible
// =====================

export const decksAtom = atomWithSuspenseQuery(() => ({
	queryKey: ["decks"],
	queryFn: async () => {
		const res = await apiClient.rpc.api.decks.$get(undefined, {
			headers: apiClient.getAuthHeader(),
		});
		const data = await apiClient.handleResponse<{ decks: Deck[] }>(res);
		return data.decks;
	},
}));

// =====================
// Single Deck by ID - Suspense-compatible
// =====================

export const deckByIdAtomFamily = atomFamily((deckId: string) =>
	atomWithSuspenseQuery(() => ({
		queryKey: ["decks", deckId],
		queryFn: async () => {
			const res = await apiClient.rpc.api.decks[":id"].$get({
				param: { id: deckId },
			});
			const data = await apiClient.handleResponse<{ deck: Deck }>(res);
			return data.deck;
		},
	})),
);