Files
examjam-frontend/components/Header.tsx
2025-08-17 19:59:14 +06:00

139 lines
3.9 KiB
TypeScript

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 (
<header className={styles.header}>
{displayUser && (
<div className={styles.profile}>
<Avatar className="bg-gray-200 w-10 h-10">
<AvatarFallback className=" text-lg">
{user?.username ? user.username.charAt(0).toUpperCase() : ""}
</AvatarFallback>
</Avatar>
<span className={styles.text}>
Hello, {user?.username ? user.username.split(" ")[0] : ""}
</span>
</div>
)}
{displayTabTitle && (
<div className={styles.profile}>
<button onClick={handleBackClick} className={styles.iconButton}>
<ChevronLeft size={24} color="white" />
</button>
<span className={styles.text}>{displayTabTitle}</span>
</div>
)}
{displaySubject && (
<div className={styles.profile}>
<span className={styles.text}>{displaySubject}</span>
</div>
)}
{examDuration && (
<div className={styles.examHeader}>
<button onClick={showExitDialog} className={styles.iconButton}>
<ChevronLeft size={30} color="white" />
</button>
<div className={styles.timer}>
<div className={styles.timeUnit}>
<span className={styles.timeValue}>
{String(hours).padStart(2, "0")}
</span>
<span className={styles.timeLabel}>Hrs</span>
</div>
<div className={styles.timeUnit}>
<span className={styles.timeValue}>
{String(minutes).padStart(2, "0")}
</span>
<span className={styles.timeLabel}>Mins</span>
</div>
<div className={styles.timeUnit} style={{ borderRight: "none" }}>
<span className={styles.timeValue}>
{String(seconds).padStart(2, "0")}
</span>
<span className={styles.timeLabel}>Secs</span>
</div>
</div>
<button
onClick={open}
className={`${styles.iconButton} ${styles.disabled}`}
>
<Layers size={30} color="white" />
</button>
</div>
)}
</header>
);
};
export default Header;