generated from muhtadeetaron/nextjs-template
122 lines
3.8 KiB
TypeScript
122 lines
3.8 KiB
TypeScript
"use client";
|
|
|
|
import React from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { ChevronLeft, Layers } from "lucide-react";
|
|
import styles from "@/css/Header.module.css";
|
|
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
|
|
import { useModal } from "@/context/ModalContext";
|
|
import { useAuthStore } from "@/stores/authStore";
|
|
import { useTimerStore } from "@/stores/timerStore";
|
|
import { useExamStore } from "@/stores/examStore";
|
|
|
|
interface HeaderProps {
|
|
displayUser?: boolean;
|
|
displaySubject?: string;
|
|
displayTabTitle?: string;
|
|
}
|
|
|
|
const Header = ({
|
|
displayUser,
|
|
displaySubject,
|
|
displayTabTitle,
|
|
}: HeaderProps) => {
|
|
const router = useRouter();
|
|
const { open } = useModal();
|
|
const { cancelExam } = useExamStore();
|
|
const { stopTimer, timeRemaining } = useTimerStore();
|
|
const { user } = useAuthStore();
|
|
|
|
const showExitDialog = () => {
|
|
const confirmed = window.confirm("Are you sure you want to quit the exam?");
|
|
if (confirmed) {
|
|
stopTimer();
|
|
cancelExam();
|
|
router.push("/categories");
|
|
}
|
|
};
|
|
|
|
const handleBackClick = () => {
|
|
router.back();
|
|
};
|
|
|
|
// format time from context
|
|
const hours = Math.floor(timeRemaining / 3600);
|
|
const minutes = Math.floor((timeRemaining % 3600) / 60);
|
|
const seconds = timeRemaining % 60;
|
|
|
|
return (
|
|
<header className="bg-[#113768] h-[100px] w-full pt-7 px-6 flex items-center justify-between sticky top-0 z-50">
|
|
{displayUser && (
|
|
<div className="flex items-center gap-3">
|
|
<Avatar className="bg-gray-200 w-10 h-10">
|
|
<AvatarFallback className="flex items-center justify-center h-10 text-lg">
|
|
{user?.username ? (
|
|
user.username.charAt(0).toUpperCase()
|
|
) : (
|
|
<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-blue-500"></div>
|
|
)}
|
|
</AvatarFallback>
|
|
</Avatar>
|
|
<span className="text-md font-bold text-white">
|
|
Hello, {user?.username ? user.username.split(" ")[0] : "User"}
|
|
</span>
|
|
</div>
|
|
)}
|
|
|
|
{displayTabTitle && (
|
|
<div className="flex items-center gap-3">
|
|
<button onClick={handleBackClick} className="bg-none border-none p-1">
|
|
<ChevronLeft size={24} color="white" />
|
|
</button>
|
|
<span className="text-md font-bold text-white">
|
|
{displayTabTitle}
|
|
</span>
|
|
</div>
|
|
)}
|
|
|
|
{displaySubject && (
|
|
<div className="flex items-center gap-3">
|
|
<span className="text-md font-bold text-white">{displaySubject}</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* Exam timer header */}
|
|
{timeRemaining > 0 && (
|
|
<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}`}>
|
|
<Layers size={30} color="white" />
|
|
</button>
|
|
</div>
|
|
)}
|
|
</header>
|
|
);
|
|
};
|
|
|
|
export default Header;
|