'use client'; import Link from 'next/link'; import { usePathname, useRouter } from 'next/navigation'; import { useEffect, useState } from 'react'; import { LayoutGrid, Users, Building2, Server, Hammer, FileClock, Activity, Wand2, LogOut, ShieldAlert, } from 'lucide-react'; import { apiFetch } from '@/lib/api'; import { cn } from '@/lib/cn'; import { Logo } from '@/components/logo'; interface MeUser { userId: string; email: string; isAdmin: boolean; } const NAV: { href: string; label: string; icon: React.ComponentType<{ size?: number }> }[] = [ { href: '/admin', label: 'Overview', icon: LayoutGrid }, { href: '/admin/users', label: 'Users', icon: Users }, { href: '/admin/orgs', label: 'Organizations', icon: Building2 }, { href: '/admin/servers', label: 'MCP servers', icon: Server }, { href: '/admin/builds', label: 'Builds', icon: Hammer }, { href: '/admin/audit', label: 'Audit log', icon: FileClock }, { href: '/admin/system', label: 'System health', icon: Activity }, { href: '/admin/prompt', label: 'AI prompt', icon: Wand2 }, ]; export default function AdminLayout({ children }: { children: React.ReactNode }) { const pathname = usePathname(); const router = useRouter(); const [user, setUser] = useState(null); const [authState, setAuthState] = useState<'checking' | 'ok' | 'forbidden'>('checking'); useEffect(() => { if (pathname === '/admin/login') { setAuthState('ok'); return; } apiFetch<{ user: MeUser }>('/v1/auth/me') .then((r) => { if (r.user.isAdmin) { setUser(r.user); setAuthState('ok'); } else { setAuthState('forbidden'); } }) .catch(() => setAuthState('forbidden')); }, [pathname]); useEffect(() => { if (authState === 'forbidden' && pathname !== '/admin/login') { router.replace('/admin/login'); } }, [authState, pathname, router]); async function logout() { await apiFetch('/v1/auth/logout', { method: 'POST' }).catch(() => undefined); router.replace('/admin/login'); } if (pathname === '/admin/login') return <>{children}; if (authState === 'checking') { return (

verifying admin…

); } if (authState === 'forbidden') { return (

Admin access required.

/admin/login
); } return (
{children}
); }