"use client"; import { useRouter } from "next/navigation"; import { useExam, useExamResults } from "@/context/ExamContext"; import { useEffect, useState } from "react"; import React from "react"; import { ArrowLeft } from "lucide-react"; import { Badge } from "@/components/ui/badge"; import Image from "next/image"; import SlidingGallery from "@/components/SlidingGallery"; interface Question { correctAnswer: string; id: number; question: string; options: Record; solution?: string; } interface QuestionItemProps { question: Question; selectedAnswer: string | undefined; } const QuestionItem = ({ question, selectedAnswer }: QuestionItemProps) => (

{question.id}. {question.question}

{!selectedAnswer ? ( Skipped ) : selectedAnswer.answer === question.correctAnswer ? ( Correct ) : ( Incorrect )}
{Object.entries(question.options).map(([key, value]) => { const isCorrect = key === question.correctAnswer; const isSelected = key === selectedAnswer?.answer; let optionStyle = "px-2 py-1 flex items-center rounded-full border font-medium text-sm"; if (isCorrect) { optionStyle += " bg-green-600 text-white border-green-600"; } if (isSelected && !isCorrect) { optionStyle += " bg-red-600 text-white border-red-600"; } if (!isCorrect && !isSelected) { optionStyle += " border-gray-300 text-gray-700"; } return (
{key.toUpperCase()} {value}
); })}

Solution:

{question.solution}

); export default function ResultsPage() { const router = useRouter(); const { clearExam, isExamCompleted, getApiResponse } = useExam(); let examResults; useEffect(() => { if (!isExamCompleted()) { router.push("/unit"); return; } }, [isExamCompleted, router]); try { examResults = useExamResults(); console.log(examResults); } catch (error) { // Handle case where there's no completed exam return (

Loading...

); } // Get API response data const apiResponse = getApiResponse(); const handleBackToHome = () => { clearExam(); // Give time for state to fully reset before pushing new route setTimeout(() => { router.push("/unit"); }, 400); // 50–100ms is usually enough }; const timeTaken = examResults.endTime && examResults.startTime ? Math.round( (examResults.endTime.getTime() - examResults.startTime.getTime()) / 1000 / 60 ) : 0; const resultViews = [ { id: 1, content: (
Accuracy Rate:
accuracy

{( (examResults.score / examResults.totalQuestions) * 100 ).toFixed(1)} %

), }, { id: 2, content: (
Error Rate:
accuracy

{( ((examResults.totalQuestions - examResults.score) / examResults.totalQuestions) * 100 ).toFixed(1)} %

), }, { id: 3, content: (
Attempt Rate:
accuracy

{( (examResults.answers.length / examResults.totalQuestions) * 100 ).toFixed(1)} %

), }, ]; return (

{examResults?.score < 30 ? "Try harder!" : examResults?.score < 70 ? "Getting Better" : "You did great!"}

{/* Score Display */} {apiResponse && (

Solutions

{apiResponse.questions?.map((question) => ( ))}
)} {/* Action Buttons */}
); }