import { useEffect, useState, type ReactNode } from 'react' import { useNavigate, useLocation, Link } from 'react-router-dom' interface User { id: number username: string name: string | null role: string avatar_url: string | null } interface LayoutProps { children: ReactNode } export function Layout({ children }: LayoutProps) { const navigate = useNavigate() const location = useLocation() const [user, setUser] = useState(null) const [loading, setLoading] = useState(true) const [theme, setTheme] = useState(() => document.documentElement.getAttribute('data-theme') || 'light' ) useEffect(() => { fetch('/api/auth/me', { credentials: 'include' }) .then(res => res.json()) .then(data => { if (!data.user) { navigate('/login') return null } return fetch(`/api/users/${data.user.id}`, { credentials: 'include' }) .then(res => res.json()) }) .then((profile) => { if (profile) { setUser(profile) } }) .catch(() => navigate('/login')) .finally(() => setLoading(false)) }, [navigate]) const handleLogout = async () => { await fetch('/api/auth/logout', { method: 'POST', credentials: 'include', }) navigate('/login') } const toggleTheme = () => { const newTheme = theme === 'dark' ? 'light' : 'dark' document.documentElement.setAttribute('data-theme', newTheme) localStorage.setItem('theme', newTheme) setTheme(newTheme) } if (loading) { return
Loading...
} if (!user) { return null } const displayName = user.name || user.username const navItems = [ { path: '/', label: 'Dashboard', icon: '/icons/healthcare/icons8-powerchart-50.png' }, { path: '/profile', label: 'Profile', icon: '/icons/general/icons8-user-50.png' }, { path: '/insights', label: 'Insights', icon: '/icons/general/icons8-idea-50.png', disabled: true }, { path: '/sources', label: 'Sources', icon: '/icons/general/icons8-document-50.png' }, ] return (
{/* Sidebar */} {/* Main Content */}
{children}
) }