Notifications
{notifications.length} New{notif.title}
{notif.message}
{userProfile?.full_name || "User"}
{userProfile?.work_email || "user@examcraft.ai"}
import React, { useState, useEffect, useRef } from "react"; import { Outlet, NavLink, useNavigate, Link } from "react-router-dom"; import { useTheme } from "../../context/ThemeContext"; import { cn } from "../../utils/cn"; import { motion, AnimatePresence } from "framer-motion"; import { supabase } from "../../utils/supabaseClient"; import { API_BASE_URL } from "../../utils/api"; import { LayoutDashboard, UploadCloud, BrainCircuit, History as HistoryIcon, Settings, LogOut, ChevronLeft, ChevronRight, Sun, Moon, Bell, Search, Menu, X, KeyRound, AlertCircle } from "lucide-react"; export default function DashboardLayout() { const [isSidebarOpen, setSidebarOpen] = useState(true); const [isMobileOpen, setMobileOpen] = useState(false); const [isProfileOpen, setProfileOpen] = useState(false); const [isNotificationsOpen, setNotificationsOpen] = useState(false); const [userProfile, setUserProfile] = useState(null); const [notifications, setNotifications] = useState([]); // Exam Password States const [showPasswordPopup, setShowPasswordPopup] = useState(false); const [examPassword, setExamPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [passwordError, setPasswordError] = useState(""); const [isSavingPassword, setIsSavingPassword] = useState(false); const profileRef = useRef(null); const notificationsRef = useRef(null); const { theme, toggleTheme } = useTheme(); const navigate = useNavigate(); useEffect(() => { const fetchUser = async () => { const { data: { session } } = await supabase.auth.getSession(); if (session?.user) { try { const response = await fetch(`${API_BASE_URL}/api/profile`, { method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${session.access_token}` } }); if (response.ok) { const data = await response.json(); if (data && data.full_name) { setUserProfile(data); // Detect if exam_password is not set if (data.has_exam_password === false) { setNotifications([{ id: 'exam_password_setup', title: 'Set Exam Password', message: 'A 6-digit exam password is required to secure your papers.', isUnread: true }]); // Prevent sending emails repeatedly per session if (!sessionStorage.getItem('passwordReminderSent')) { sessionStorage.setItem('passwordReminderSent', 'true'); fetch(`${API_BASE_URL}/api/send-password-reminder`, { method: 'POST', headers: { 'Authorization': `Bearer ${session.access_token}` } }).catch(err => console.error("Email API Err:", err)); } } } else { setUserProfile(session.user.user_metadata); } } else { setUserProfile(session.user.user_metadata); } } catch (error) { console.error("Failed to fetch profile via API", error); setUserProfile(session.user.user_metadata); } } }; fetchUser(); }, []); // Close profile and notification dropdowns when clicking outside useEffect(() => { const handleClickOutside = (event) => { if (profileRef.current && !profileRef.current.contains(event.target)) { setProfileOpen(false); } if (notificationsRef.current && !notificationsRef.current.contains(event.target)) { setNotificationsOpen(false); } }; document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, []); const handleSavePassword = async (e) => { e.preventDefault(); setPasswordError(""); if (!/^\d{6}$/.test(examPassword)) { setPasswordError("Password must be exactly 6 digits."); return; } if (examPassword !== confirmPassword) { setPasswordError("Passwords do not match."); return; } setIsSavingPassword(true); try { const { data: { session } } = await supabase.auth.getSession(); const response = await fetch(`${API_BASE_URL}/api/profile`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${session.access_token}` }, body: JSON.stringify({ exam_password: examPassword }) }); if (!response.ok) throw new Error("Failed to save password"); setUserProfile(prev => ({ ...prev, has_exam_password: true })); setNotifications(prev => prev.filter(n => n.id !== 'exam_password_setup')); setShowPasswordPopup(false); setExamPassword(""); setConfirmPassword(""); } catch (error) { setPasswordError(error.message); } finally { setIsSavingPassword(false); } }; const navItems = [ { name: "Dashboard", path: "/app", icon: LayoutDashboard }, { name: "Upload", path: "/app/upload", icon: UploadCloud }, { name: "Generate", path: "/app/generate", icon: BrainCircuit }, { name: "History", path: "/app/history", icon: HistoryIcon }, { name: "Settings", path: "/app/settings", icon: Settings }, ]; return (
{notif.message}
{userProfile?.full_name || "User"}
{userProfile?.work_email || "user@examcraft.ai"}
Create a secure 6-digit PIN to protect your generated resources and control exam sessions.