import { useState, useEffect, useRef } from 'react'; import { Link, useLocation, Outlet, Navigate } from 'react-router-dom'; import { useGetMe } from '../generated/anthoLumeAPIV1'; import { useAuth } from '../auth/AuthContext'; import { UserIcon, DropdownIcon } from '../icons'; import { useTheme } from '../theme/ThemeProvider'; import type { ThemeMode } from '../utils/localSettings'; import HamburgerMenu from './HamburgerMenu'; const themeModes: ThemeMode[] = ['light', 'dark', 'system']; export default function Layout() { const location = useLocation(); const { isAuthenticated, user, logout, isCheckingAuth } = useAuth(); const { themeMode, setThemeMode } = useTheme(); const { data } = useGetMe(isAuthenticated ? {} : undefined); const fetchedUser = data?.status === 200 && data.data && 'username' in data.data ? data.data : null; const userData = user ?? fetchedUser; const [isUserDropdownOpen, setIsUserDropdownOpen] = useState(false); const dropdownRef = useRef(null); const handleLogout = () => { logout(); setIsUserDropdownOpen(false); }; useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setIsUserDropdownOpen(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, []); const navItems = [ { path: '/admin/import-results', title: 'Admin - Import' }, { path: '/admin/import', title: 'Admin - Import' }, { path: '/admin/users', title: 'Admin - Users' }, { path: '/admin/logs', title: 'Admin - Logs' }, { path: '/admin', title: 'Admin - General' }, { path: '/documents', title: 'Documents' }, { path: '/progress', title: 'Progress' }, { path: '/activity', title: 'Activity' }, { path: '/search', title: 'Search' }, { path: '/settings', title: 'Settings' }, { path: '/', title: 'Home' }, ]; const currentPageTitle = navItems.find(item => item.path === '/' ? location.pathname === item.path : location.pathname.startsWith(item.path) )?.title || 'Home'; useEffect(() => { document.title = `AnthoLume - ${currentPageTitle}`; }, [currentPageTitle]); if (isCheckingAuth) { return
Loading...
; } if (!isAuthenticated) { return ; } return (

{currentPageTitle}

{isUserDropdownOpen && (

Theme

{themeModes.map(mode => ( ))}
setIsUserDropdownOpen(false)} className="block px-4 py-2 text-content-muted hover:bg-surface-muted hover:text-content" role="menuitem" > Settings
)}
); }