-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquiz-view.tsx
More file actions
224 lines (201 loc) · 9.72 KB
/
Copy pathquiz-view.tsx
File metadata and controls
224 lines (201 loc) · 9.72 KB
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
import { useState } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { Check, X, ArrowRight, Award, Loader2 } from 'lucide-react';
import { api, Quiz, QuizResultResponse } from '@/lib/api-client';
import confetti from 'canvas-confetti';
interface QuizViewProps {
repoId: string;
quiz: Quiz;
onClose: () => void;
onResultSubmitted?: (result: QuizResultResponse) => void;
}
export function QuizView({ repoId, quiz, onClose, onResultSubmitted }: QuizViewProps) {
const [currentQuestionIndex, setCurrentQuestionIndex] = useState(0);
const [selectedOption, setSelectedOption] = useState<number | null>(null);
const [isAnswered, setIsAnswered] = useState(false);
const [score, setScore] = useState(0);
const [showResult, setShowResult] = useState(false);
const [submittingResult, setSubmittingResult] = useState(false);
const [resultError, setResultError] = useState<string | null>(null);
const [backendResult, setBackendResult] = useState<QuizResultResponse | null>(null);
const currentQuestion = quiz.questions[currentQuestionIndex];
const isCorrect = selectedOption === currentQuestion.correct_option_index;
const handleSelectOption = (index: number) => {
if (isAnswered || submittingResult) return;
setSelectedOption(index);
setIsAnswered(true);
if (index === currentQuestion.correct_option_index) {
setScore((value) => value + 1);
confetti({
particleCount: 50,
spread: 60,
origin: { y: 0.8 },
colors: ['#6366f1', '#818cf8'],
});
}
};
const submitQuizResult = async () => {
const totalQuestions = quiz.questions.length;
const normalizedScore = totalQuestions > 0 ? score / totalQuestions : 0;
setSubmittingResult(true);
setResultError(null);
try {
const result = await api.submitQuizResult(repoId, quiz.lesson_id, normalizedScore);
setBackendResult(result);
onResultSubmitted?.(result);
setShowResult(true);
confetti({
particleCount: 150,
spread: 100,
origin: { y: 0.6 },
});
} catch (error) {
console.error('Failed to submit quiz result:', error);
setResultError('Could not submit quiz result. Try again.');
} finally {
setSubmittingResult(false);
}
};
const handleNext = async () => {
if (currentQuestionIndex < quiz.questions.length - 1) {
setCurrentQuestionIndex((value) => value + 1);
setSelectedOption(null);
setIsAnswered(false);
return;
}
await submitQuizResult();
};
if (showResult) {
const xpAwarded = backendResult
? backendResult.xp_gained.amount + (backendResult.xp_gained.bonus ?? 0)
: 0;
return (
<div className="flex flex-col items-center justify-center h-full p-8 text-center animate-in fade-in zoom-in duration-300">
<div className="w-20 h-20 bg-yellow-500/20 rounded-full flex items-center justify-center mb-6">
<Award size={40} className="text-yellow-500" />
</div>
<h2 className="text-3xl font-bold text-white mb-2">Quiz Complete!</h2>
<p className="text-zinc-400 mb-4">
You scored <span className="text-white font-bold">{score}</span> out of{' '}
<span className="text-white font-bold">{quiz.questions.length}</span>
</p>
{backendResult && (
<p className="text-sm text-indigo-300 mb-8">
{backendResult.is_perfect
? `Perfect score! +${xpAwarded} XP`
: backendResult.is_pass
? `Passed! +${xpAwarded} XP`
: 'Keep practicing to pass the quiz.'}
</p>
)}
<div className="flex gap-4">
<button
onClick={onClose}
className="bg-zinc-800 hover:bg-zinc-700 text-white px-6 py-2 rounded-lg transition-colors"
>
Close Quiz
</button>
</div>
</div>
);
}
return (
<div className="flex flex-col h-full bg-zinc-950 p-6 max-w-2xl mx-auto">
{/* Header */}
<div className="flex justify-between items-center mb-8">
<div className="text-zinc-500 text-sm font-medium">
Question {currentQuestionIndex + 1} of {quiz.questions.length}
</div>
<div className="text-indigo-400 font-bold">
Score: {score}
</div>
</div>
{/* Progress Bar */}
<div className="w-full bg-zinc-900 h-1 rounded-full mb-8 overflow-hidden">
<div
className="bg-indigo-600 h-full transition-all duration-300"
style={{ width: `${((currentQuestionIndex + 1) / quiz.questions.length) * 100}%` }}
/>
</div>
{/* Question */}
<div className="flex-1">
<h3 className="text-xl text-white font-semibold mb-6">
{currentQuestion.text}
</h3>
<div className="space-y-3">
{currentQuestion.options.map((option, idx) => {
let className = 'w-full p-4 rounded-xl border-2 text-left transition-all relative ';
if (isAnswered) {
if (idx === currentQuestion.correct_option_index) {
className += 'border-green-500 bg-green-500/10 text-green-100';
} else if (idx === selectedOption) {
className += 'border-red-500 bg-red-500/10 text-red-100 opacity-60';
} else {
className += 'border-zinc-800 bg-zinc-900/50 text-zinc-500 opacity-50';
}
} else {
className += 'border-zinc-800 bg-zinc-900 hover:bg-zinc-800 hover:border-zinc-700 text-zinc-300';
}
return (
<button
key={idx}
onClick={() => handleSelectOption(idx)}
disabled={isAnswered || submittingResult}
className={className}
>
<div className="flex items-center justify-between">
<span>{option}</span>
{isAnswered && idx === currentQuestion.correct_option_index && (
<Check size={20} className="text-green-500" />
)}
{isAnswered && idx === selectedOption && idx !== currentQuestion.correct_option_index && (
<X size={20} className="text-red-500" />
)}
</div>
</button>
);
})}
</div>
{/* Feedback / Next Button */}
<AnimatePresence>
{isAnswered && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
className="mt-8"
>
<div className={`p-4 rounded-lg mb-6 ${isCorrect ? 'bg-green-500/10 border border-green-500/20' : 'bg-red-500/10 border border-red-500/20'}`}>
<p className={`font-semibold mb-1 ${isCorrect ? 'text-green-400' : 'text-red-400'}`}>
{isCorrect ? 'Correct!' : 'Incorrect'}
</p>
<p className="text-zinc-300 text-sm">
{currentQuestion.explanation}
</p>
</div>
{resultError && (
<p className="text-sm text-red-400 mb-4">{resultError}</p>
)}
<button
onClick={handleNext}
disabled={submittingResult}
className="w-full bg-indigo-600 hover:bg-indigo-700 disabled:opacity-50 text-white font-medium py-3 rounded-xl flex items-center justify-center gap-2 transition-colors"
>
{submittingResult ? (
<>
<Loader2 size={18} className="animate-spin" />
Submitting...
</>
) : (
<>
{currentQuestionIndex < quiz.questions.length - 1 ? 'Next Question' : 'Finish Quiz'}
<ArrowRight size={18} />
</>
)}
</button>
</motion.div>
)}
</AnimatePresence>
</div>
</div>
);
}