MCPcopy Create free account
hub / github.com/0010aor/FlashNotes / usePracticeSession

Function usePracticeSession

frontend/src/hooks/usePracticeSession.ts:21–139  ·  view source on GitHub ↗
(collectionId: string)

Source from the content-addressed store, hash-verified

19}
20
21export function usePracticeSession(collectionId: string) {
22 const [state, setState] = useState<PracticeSessionState>({
23 sessionId: null,
24 currentCard: null,
25 isFlipped: false,
26 progress: {
27 correct: 0,
28 incorrect: 0,
29 total: 0,
30 },
31 isComplete: false,
32 })
33 const [isLoading, setIsLoading] = useState(false)
34 const [error, setError] = useState<string | null>(null)
35
36 const startingRef = useRef(false)
37
38 const start = useCallback(async () => {
39 if (startingRef.current || state.sessionId) return
40 startingRef.current = true
41 setIsLoading(true)
42 setError(null)
43 try {
44 const session = await startPracticeSession(collectionId)
45 setState((prev) => ({
46 ...prev,
47 sessionId: session.id,
48 isComplete: session.is_completed,
49 progress: {
50 correct: session.correct_answers,
51 incorrect: session.cards_practiced - session.correct_answers,
52 total: session.total_cards,
53 },
54 }))
55 if (!session.is_completed) {
56 const nextCardData = await getNextPracticeCard(session.id)
57 setState((prev) => ({
58 ...prev,
59 currentCard: nextCardData ? nextCardData.card : null,
60 isFlipped: false,
61 }))
62 }
63 } catch (err: unknown) {
64 console.error(err)
65 setError('Failed to start session')
66 } finally {
67 setIsLoading(false)
68 startingRef.current = false
69 }
70 }, [collectionId, state.sessionId])
71
72 const handleFlip = useCallback(() => {
73 setState((prev) => ({ ...prev, isFlipped: !prev.isFlipped }))
74 }, [])
75
76 const handleAnswer = useCallback(
77 async (isCorrect: boolean) => {
78 if (!state.sessionId || !state.currentCard) return

Callers 1

PracticeComponentFunction · 0.90

Calls 3

startPracticeSessionFunction · 0.90
getNextPracticeCardFunction · 0.90
submitPracticeCardResultFunction · 0.90

Tested by

no test coverage detected