import { useState, useMemo } from 'react'
import { Link, Outlet, useLocation } from 'react-router-dom'
import { useTheme } from '../hooks/useTheme'
import { useAuth } from '../contexts/AuthContext'
function NavLink({
to,
active,
children,
onClick,
className = '',
}: {
to: string
active: boolean
children: React.ReactNode
onClick?: () => void
className?: string
}) {
return (
{children}
)
}
function ThemeToggle() {
const { theme, toggle } = useTheme()
return (
)
}
function UserMenu({ onAction }: { onAction?: () => void }) {
const { user, loading, signOut } = useAuth()
const [open, setOpen] = useState(false)
if (loading) {
return
}
if (!user) {
return (
Sign in
)
}
const email = user.email ?? ''
const initials = email.charAt(0).toUpperCase()
return (
{open && (
<>
setOpen(false)} />
>
)}
)
}
export function Layout() {
const [menuOpen, setMenuOpen] = useState(false)
const location = useLocation()
const { user, isAdmin } = useAuth()
const navLinks = useMemo(() => {
if (!user) {
// Logged out: Home, Runs, Genlockes, Stats
return [
{ to: '/', label: 'Home' },
{ to: '/runs', label: 'Runs' },
{ to: '/genlockes', label: 'Genlockes' },
{ to: '/stats', label: 'Stats' },
]
}
// Logged in: New Run, My Runs, Genlockes, Stats
const links = [
{ to: '/runs/new', label: 'New Run' },
{ to: '/runs', label: 'My Runs' },
{ to: '/genlockes', label: 'Genlockes' },
{ to: '/stats', label: 'Stats' },
]
// Admin gets Admin link
if (isAdmin) {
links.push({ to: '/admin', label: 'Admin' })
}
return links
}, [user, isAdmin])
function isActive(to: string) {
if (to === '/' || to === '/runs/new') return location.pathname === to
return location.pathname.startsWith(to)
}
return (
)
}