"use client"; import React, { useEffect, useCallback, useMemo } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { useTimer } from "@/context/TimerContext"; import { useExam } from "@/context/ExamContext"; import Header from "@/components/Header"; import { useModal } from "@/context/ModalContext"; import Modal from "@/components/ExamModal"; import QuestionItem from "@/components/QuestionItem"; import BackgroundWrapper from "@/components/BackgroundWrapper"; export default function ExamPage() { const router = useRouter(); const searchParams = useSearchParams(); const test_id = searchParams.get("test_id") || ""; const type = searchParams.get("type") || ""; // const { isOpen, close, open } = useModal(); const { timeRemaining, setInitialTime, stopTimer } = useTimer(); const { test, answers, startExam, setAnswer, submitExam, cancelExam } = useExam(); // Start exam + timer useEffect(() => { if (type && test_id) { startExam(type, test_id).then(() => { if (test?.metadata.time_limit_minutes) { setInitialTime(test.metadata.time_limit_minutes * 60); // convert to seconds } }); } }, [type, test_id, startExam, setInitialTime]); const showExitDialog = useCallback(() => { if (window.confirm("Are you sure you want to quit the exam?")) { stopTimer(); cancelExam(); router.push(`/categories/${type}s`); } }, [stopTimer, cancelExam, router]); if (!test) { return (

Loading exam...

); } // answered set for modal overview // const answeredSet = useMemo( // () => // new Set( // answers // .map((a, idx) => // a !== null && a !== undefined ? idx.toString() : null // ) // .filter(Boolean) as string[] // ), // [answers] // ); return (
{/* Header with live timer */}
{/* Modal: Question overview */} {/*

Questions: {test.questions.length}

Answered:{" "} {answeredSet.size}

Skipped:{" "} {test.questions.length - answeredSet.size}

{test.questions.map((q, idx) => { const answered = answeredSet.has(String(idx)); return (
{ // optional: scroll to question const el = document.getElementById(`question-${idx}`); if (el) { el.scrollIntoView({ behavior: "smooth" }); close(); } }} > {idx + 1}
); })}
*/} {/* Questions */}
{test.questions.map((q, idx) => (
setAnswer(idx, answer)} />
))} {/* Bottom submit bar */}
); }