import React, { useEffect, useState } from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; import { AppBar, Toolbar, Typography, IconButton, Box, useTheme, Menu, MenuItem, ListItemIcon, ListItemText, Divider } from '@mui/material'; import MenuIcon from '@mui/icons-material/Menu'; import Login from '@mui/icons-material/Login'; import Logout from '@mui/icons-material/Logout'; import Teacher from '@mui/icons-material/School'; import Student from '@mui/icons-material/Person'; import TLDrawDev from '@mui/icons-material/Dashboard'; import DevTools from '@mui/icons-material/Build'; import Multiplayer from '@mui/icons-material/Groups'; import Calendar from '@mui/icons-material/CalendarToday'; import TeacherPlanner from '@mui/icons-material/Assignment'; import ExamMarker from '@mui/icons-material/AssignmentTurnedIn'; import Settings from '@mui/icons-material/Settings'; import Search from '@mui/icons-material/Search'; import Admin from '@mui/icons-material/AdminPanelSettings'; import Home from '@mui/icons-material/Home'; import Schedule from '@mui/icons-material/Schedule'; import Class from '@mui/icons-material/Class'; import Book from '@mui/icons-material/Book'; import Enrollment from '@mui/icons-material/HowToReg'; import { HEADER_HEIGHT } from './Layout'; import { logger } from '../debugConfig'; import { GraphNavigator } from '../components/navigation/GraphNavigator'; const Header: React.FC = () => { const theme = useTheme(); const navigate = useNavigate(); const location = useLocation(); const { user, signOut } = useAuth(); const [anchorEl, setAnchorEl] = useState(null); const [isAuthenticated, setIsAuthenticated] = useState(!!user); const isAdmin = user?.email === import.meta.env.VITE_SUPER_ADMIN_EMAIL; const showGraphNavigation = location.pathname === '/single-player'; // Update authentication state whenever user changes useEffect(() => { const newAuthState = !!user; setIsAuthenticated(newAuthState); logger.debug('user-context', '🔄 User state changed in header', { hasUser: newAuthState, userId: user?.id, userEmail: user?.email, userState: newAuthState ? 'logged-in' : 'logged-out', isAdmin }); }, [user, isAdmin]); const handleMenuOpen = (event: React.MouseEvent) => { setAnchorEl(event.currentTarget); }; const handleMenuClose = () => { setAnchorEl(null); }; const handleNavigation = (path: string) => { navigate(path); handleMenuClose(); }; const handleSignupNavigation = (role: 'teacher' | 'student') => { navigate('/signup', { state: { role } }); handleMenuClose(); }; const handleSignOut = async () => { try { logger.debug('auth-service', '🔄 Signing out user', { userId: user?.id }); await signOut(); setIsAuthenticated(false); setAnchorEl(null); logger.debug('auth-service', '✅ User signed out'); } catch (error) { logger.error('auth-service', '❌ Error signing out:', error); console.error('Error signing out:', error); } }; return ( {showGraphNavigation && ( )} handleNavigation('/')} > Classroom Copilot {isAuthenticated && ( {user?.email} )} Navigation {isAuthenticated ? [ // Home handleNavigation('/')}> , , // Timetable Section Timetable & Classes , handleNavigation('/timetable')}> , handleNavigation('/classes')}> , handleNavigation('/my-classes')}> , handleNavigation('/enrollment-requests')}> , , // Features Section Features , handleNavigation('/calendar')}> , handleNavigation('/teacher-planner')}> , handleNavigation('/exam-marker')}> , , // Utilities Section Utilities , handleNavigation('/settings')}> , handleNavigation('/search')}> , // Admin Section ...(isAdmin ? [ , Administration , handleNavigation('/admin')}> ] : []), // Authentication Section , ] : [ // Authentication Section for Non-authenticated Users handleNavigation('/login')}> , , handleSignupNavigation('teacher')}> , handleSignupNavigation('student')}> ]} ); }; export default Header;