import React, { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import { ChevronLeft, Layers } from "lucide-react"; import { useTimer } from "@/context/TimerContext"; import styles from "@/css/Header.module.css"; import { useExam } from "@/context/ExamContext"; import { Avatar, AvatarFallback } from "@/components/ui/avatar"; import { useModal } from "@/context/ModalContext"; import { useAuth } from "@/context/AuthContext"; interface HeaderProps { displayUser?: boolean; displaySubject?: string; displayTabTitle?: string; examDuration?: string | null; } const Header = ({ displayUser, displaySubject, displayTabTitle, examDuration, }: HeaderProps) => { const router = useRouter(); const { open } = useModal(); const { clearExam } = useExam(); const [totalSeconds, setTotalSeconds] = useState( examDuration ? parseInt(examDuration) * 60 : 0 ); const { stopTimer } = useTimer(); const { user, isLoading } = useAuth(); useEffect(() => { if (!examDuration) return; const timer = setInterval(() => { setTotalSeconds((prev) => { if (prev <= 0) { clearInterval(timer); return 0; } return prev - 1; }); }, 1000); return () => clearInterval(timer); }, [examDuration]); const hours = Math.floor(totalSeconds / 3600); const minutes = Math.floor((totalSeconds % 3600) / 60); const seconds = totalSeconds % 60; const showExitDialog = () => { const confirmed = window.confirm("Are you sure you want to quit the exam?"); if (confirmed) { if (stopTimer) { stopTimer(); } clearExam(); router.push("/unit"); } }; const handleBackClick = () => { router.back(); }; return (
{displayUser && (
{user?.username ? user.username.charAt(0).toUpperCase() : ""} Hello, {user?.username ? user.username.split(" ")[0] : ""}
)} {displayTabTitle && (
{displayTabTitle}
)} {displaySubject && (
{displaySubject}
)} {examDuration && (
{String(hours).padStart(2, "0")} Hrs
{String(minutes).padStart(2, "0")} Mins
{String(seconds).padStart(2, "0")} Secs
)}
); }; export default Header;