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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
"use client";
import { useState, useEffect } from "react";
import { DeckResponse, CardResponse } from "@/lib/types/cards";
import { startStudySession, getUserStudyStats } from "@/actions/srs";
import StudyCard from "./StudyCard";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Progress } from "@/components/ui/progress";
import { Skeleton } from "@/components/ui/skeleton";
import { cn } from "@/lib/utils";
interface StudySessionProps {
userId: number;
lessonId: number;
initialData?: DeckResponse;
}
export default function StudySession({ userId, lessonId, initialData }: StudySessionProps) {
const [deckData, setDeckData] = useState<DeckResponse | null>(initialData || null);
const [currentCardIndex, setCurrentCardIndex] = useState(0);
const [reviewedCards, setReviewedCards] = useState<CardResponse[]>([]);
const [isLoading, setIsLoading] = useState(!initialData);
const [isCompleted, setIsCompleted] = useState(false);
const [stats, setStats] = useState<any>(null);
const [error, setError] = useState<string | null>(null);
// Load the deck data if not provided
useEffect(() => {
if (!initialData) {
loadDeck();
}
// Load user stats
loadStats();
}, []);
// Load deck data
const loadDeck = async () => {
setIsLoading(true);
setError(null);
try {
const result = await startStudySession(userId, lessonId, true);
if ('error' in result) {
setError(result.error);
setDeckData(null);
} else {
setDeckData(result);
}
} catch (error) {
console.error("Error loading deck:", error);
setError("Failed to load study session. Please try again later.");
} finally {
setIsLoading(false);
}
};
// Load user stats
const loadStats = async () => {
try {
const userStats = await getUserStudyStats(userId);
setStats(userStats);
} catch (error) {
console.error("Error loading stats:", error);
}
};
// Handle card completion
const handleCardComplete = (updatedCard: CardResponse) => {
// Add to reviewed cards
setReviewedCards(prev => [...prev, updatedCard]);
// Move to next card
if (deckData && currentCardIndex < deckData.cards.length - 1) {
setCurrentCardIndex(currentCardIndex + 1);
} else {
// End of deck
setIsCompleted(true);
}
// Refresh stats
loadStats();
};
// Skip current card
const handleSkip = () => {
if (deckData && currentCardIndex < deckData.cards.length - 1) {
setCurrentCardIndex(currentCardIndex + 1);
}
};
// Restart session
const handleRestart = () => {
setCurrentCardIndex(0);
setReviewedCards([]);
setIsCompleted(false);
loadDeck();
};
// Calculate completion percentage
const getCompletionPercentage = () => {
if (!deckData) return 0;
return (reviewedCards.length / deckData.cards.length) * 100;
};
// Get current card
const getCurrentCard = (): CardResponse | null => {
if (!deckData || !deckData.cards || deckData.cards.length === 0) return null;
return deckData.cards[currentCardIndex];
};
// Render loading state
if (isLoading) {
return (
<div className="w-full max-w-3xl mx-auto p-4">
<Card className="p-6">
<div className="space-y-4">
<Skeleton className="h-8 w-1/2" />
<Skeleton className="h-[400px] w-full" />
<div className="flex justify-between">
<Skeleton className="h-10 w-24" />
<Skeleton className="h-10 w-24" />
</div>
</div>
</Card>
</div>
);
}
// Render error state
if (error) {
return (
<div className="w-full max-w-3xl mx-auto p-4">
<Card className="p-6 text-center">
<div className="text-red-500 mb-4">{error}</div>
<Button onClick={loadDeck}>Retry</Button>
</Card>
</div>
);
}
// Render completion state
if (isCompleted || !getCurrentCard()) {
return (
<div className="w-full max-w-3xl mx-auto p-4">
<Card className="p-6">
<div className="text-center">
<h2 className="text-2xl font-bold mb-4">Study Session Completed!</h2>
<div className="mb-6">
<p className="text-lg">You've reviewed {reviewedCards.length} cards.</p>
{stats && (
<div className="mt-4 text-sm text-gray-600">
<p>Total cards: {stats.totalCards}</p>
<p>Mastered cards: {stats.masteredCards}</p>
<p>Due cards remaining: {stats.dueCards}</p>
</div>
)}
</div>
<div className="flex justify-center gap-4">
<Button onClick={handleRestart}>Start New Session</Button>
<Button variant="outline" onClick={() => window.history.back()}>
Back to Lessons
</Button>
</div>
</div>
</Card>
</div>
);
}
// Render study session
return (
<div className="w-full max-w-3xl mx-auto p-4">
<div className="mb-6">
<div className="flex justify-between items-center mb-2">
<h2 className="text-xl font-bold">
{deckData?.lesson.name}
</h2>
<div className="text-sm text-gray-500">
{reviewedCards.length} / {deckData?.cards.length} cards
</div>
</div>
<Progress value={getCompletionPercentage()} className="h-2" />
</div>
<StudyCard
card={getCurrentCard()!}
userId={userId}
onComplete={handleCardComplete}
onSkip={handleSkip}
/>
<div className="mt-6 flex justify-between">
<Button variant="ghost" onClick={() => window.history.back()}>
Exit
</Button>
<Button variant="outline" onClick={handleSkip}>
Skip
</Button>
</div>
{stats && (
<div className="mt-8 p-4 bg-gray-50 rounded-lg">
<h3 className="font-medium mb-2">Your Progress</h3>
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
<div className="text-center">
<div className="text-2xl font-bold">{stats.totalCards}</div>
<div className="text-xs text-gray-500">Total Cards</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold">{stats.masteredCards}</div>
<div className="text-xs text-gray-500">Mastered</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold">{stats.dueCards}</div>
<div className="text-xs text-gray-500">Due Today</div>
</div>
<div className="text-center">
<div className="text-2xl font-bold">{stats.streakDays}</div>
<div className="text-xs text-gray-500">Day Streak</div>
</div>
</div>
</div>
)}
</div>
);
}
|