From 8bb4f0a913d60eca3cbd11561add35c213504b90 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Wed, 2 Jul 2025 21:12:19 +0600 Subject: [PATCH] wip --- src/components/AIQuiz/AIQuizContent.tsx | 51 ++++++++------ src/components/AIQuiz/AIQuizResults.tsx | 89 +++++++++++++++++++++++++ src/queries/ai-quiz.ts | 5 -- 3 files changed, 119 insertions(+), 26 deletions(-) create mode 100644 src/components/AIQuiz/AIQuizResults.tsx diff --git a/src/components/AIQuiz/AIQuizContent.tsx b/src/components/AIQuiz/AIQuizContent.tsx index ba474d3ca..bb8799bb9 100644 --- a/src/components/AIQuiz/AIQuizContent.tsx +++ b/src/components/AIQuiz/AIQuizContent.tsx @@ -4,6 +4,7 @@ import { AIMCQQuestion } from './AIMCQQuestion'; import { AIOpenEndedQuestion } from './AIOpenEndedQuestion'; import { QuizTopNavigation } from './QuizTopNavigation'; import { getPercentage } from '../../lib/number'; +import { AIQuizResults } from './AIQuizResults'; export type QuestionState = { isSubmitted: boolean; @@ -25,10 +26,11 @@ type AIQuizContentProps = { quizSlug?: string; questions: QuizQuestion[]; isLoading?: boolean; + onNewQuiz?: () => void; }; export function AIQuizContent(props: AIQuizContentProps) { - const { quizSlug, questions, isLoading } = props; + const { quizSlug, questions, isLoading, onNewQuiz } = props; const [activeQuestionIndex, setActiveQuestionIndex] = useState(0); const activeQuestion = questions[activeQuestionIndex]; @@ -43,12 +45,10 @@ export function AIQuizContent(props: AIQuizContentProps) { const handleSubmit = (status: QuestionState['status']) => { setQuestionStates((prev) => { - const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE; - const newSelectedOptions = { ...prev, [activeQuestionIndex]: { - ...oldState, + ...activeQuestionState, isSubmitted: true, status, }, @@ -62,12 +62,10 @@ export function AIQuizContent(props: AIQuizContentProps) { const handleSetUserAnswer = (userAnswer: string) => { setQuestionStates((prev) => { - const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE; - const newSelectedOptions = { ...prev, [activeQuestionIndex]: { - ...oldState, + ...activeQuestionState, userAnswer, }, }; @@ -78,12 +76,10 @@ export function AIQuizContent(props: AIQuizContentProps) { const handleSetCorrectAnswer = (correctAnswer: string) => { setQuestionStates((prev) => { - const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE; - const newSelectedOptions = { ...prev, [activeQuestionIndex]: { - ...oldState, + ...activeQuestionState, correctAnswer, }, }; @@ -94,12 +90,10 @@ export function AIQuizContent(props: AIQuizContentProps) { const handleSelectOptions = (options: number[]) => { setQuestionStates((prev) => { - const oldState = activeQuestionState ?? DEFAULT_QUESTION_STATE; - const newSelectedOptions = { ...prev, [activeQuestionIndex]: { - ...oldState, + ...activeQuestionState, selectedOptions: options, }, }; @@ -112,6 +106,12 @@ export function AIQuizContent(props: AIQuizContentProps) { setActiveQuestionIndex(activeQuestionIndex + 1); }; + const handleRetry = () => { + setActiveQuestionIndex(0); + setQuestionStates({}); + setIsAllQuestionsSubmitted(false); + }; + const totalQuestions = questions?.length ?? 0; const progressPercentage = isLoading ? 0 @@ -119,15 +119,24 @@ export function AIQuizContent(props: AIQuizContentProps) { return (
- setActiveQuestionIndex(activeQuestionIndex - 1)} - onNext={() => setActiveQuestionIndex(activeQuestionIndex + 1)} - /> - {!isAllQuestionsSubmitted && ( + setActiveQuestionIndex(activeQuestionIndex - 1)} + onNext={() => setActiveQuestionIndex(activeQuestionIndex + 1)} + /> + )} + + {isAllQuestionsSubmitted ? ( + {})} + /> + ) : ( <> {activeQuestion && activeQuestion.type === 'mcq' && ( ; + totalQuestions: number; + onRetry: () => void; + onNewQuiz: () => void; +}; + +export function AIQuizResults(props: AIQuizResultsProps) { + const { questionStates, totalQuestions, onRetry, onNewQuiz } = props; + + const states = Object.values(questionStates); + const correctCount = states.filter( + (state) => state.status === 'correct', + ).length; + + const incorrectCount = states.filter( + (state) => state.status === 'incorrect', + ).length; + + const skippedCount = states.filter( + (state) => state.status === 'skipped', + ).length; + + const accuracy = getPercentage(correctCount, totalQuestions); + + return ( +
+ + +
+ {correctCount}/{totalQuestions} +
+ +

+ Great job! You answered {correctCount} out of {totalQuestions} questions + correctly — that's {accuracy}% accuracy! +

+ +
+
+ +
{correctCount}
+
Correct
+
+ +
+ +
{incorrectCount}
+
Incorrect
+
+ +
+ +
{skippedCount}
+
Skipped
+
+
+ +
+ + +
+
+ ); +} diff --git a/src/queries/ai-quiz.ts b/src/queries/ai-quiz.ts index 72b43aa41..0aadd6f6a 100644 --- a/src/queries/ai-quiz.ts +++ b/src/queries/ai-quiz.ts @@ -214,11 +214,6 @@ export function generateAiQuizQuestions(questionData: string): QuizQuestion[] { continue; } - console.log('-'.repeat(20)); - console.log('CONTEXT:', context); - console.log('LINE:', line); - console.log('-'.repeat(20)); - if (context === 'question') { currentQuestion.title += `\n${line}`; } else if (context === 'explanation') {