// App shell — cloned from the Dra admin console: fixed sidebar with sections // and collapse, blurred topbar with page identity + live badge + clock, and an // ambient near-black backdrop with faint grid + radial glows. // // Role-aware: the sidebar nav and identity card reflect the logged-in user's // role (admin sees everything; user sees Overview/Traffic/Configuration/Budgets // but not Providers/Settings/Users). Master-key admins keep the bearer token so // /admin/stream SSE stays live. import { useEffect, useMemo, useRef, useState } from "react"; import { Link, NavLink, Outlet, useLocation, useNavigate } from "react-router-dom"; import { Activity, BarChart3, BellRing, Boxes, ChevronLeft, CreditCard, Database, Hexagon, History, KeyRound, LayoutDashboard, Layers, Link2, LogOut, Menu, ScrollText, Server, ShieldCheck, Terminal, Users, X, } from "lucide-react"; import { useAuth } from "@/api/auth"; import { useAdminStream } from "@/api/stream"; import { getToken } from "@/api/client"; import { useClientPrefs } from "@/lib/settings"; const SIDEBAR_WIDE = 260; const SIDEBAR_COLLAPSED = 72; // Topbar height and the matching content offset must stay in lockstep — they // were two hand-written numbers before, which left a 1px seam. const TOPBAR_H = 64; const DESKTOP_QUERY = "(min-width: 1024px)"; // The sidebar is a persistent column on ≥lg and an overlay drawer below it. // `collapsed` is a desktop-only affordance: forcing it on a phone produced an // unreadable 72px icon rail inside the drawer. function useIsDesktop() { const [isDesktop, setIsDesktop] = useState( () => typeof window !== "undefined" && window.matchMedia(DESKTOP_QUERY).matches, ); useEffect(() => { const mq = window.matchMedia(DESKTOP_QUERY); const onChange = (e: MediaQueryListEvent) => setIsDesktop(e.matches); setIsDesktop(mq.matches); mq.addEventListener("change", onChange); return () => mq.removeEventListener("change", onChange); }, []); return isDesktop; } interface NavItem { to: string; label: string; icon: typeof Server; end?: boolean; } interface NavSection { title: string; items: NavItem[]; } // Sections visible to every authenticated user. Admin-only routes // (Providers, Built-in Providers, Proxy Logs, Settings, Users) are appended // only when the current user is an admin. const USER_NAV_SECTIONS: NavSection[] = [ { title: "Overview", items: [{ to: "/console", label: "Dashboard", icon: LayoutDashboard, end: true }], }, { title: "Traffic", items: [ { to: "/console/request-logs", label: "Request Logs", icon: ScrollText }, { to: "/console/usage", label: "Usage", icon: Activity }, { to: "/console/analytics", label: "Analytics", icon: BarChart3 }, ], }, { title: "Configuration", items: [ { to: "/console/models", label: "Models", icon: Database }, { to: "/console/keys", label: "Virtual Keys", icon: KeyRound }, ], }, { title: "Admin", items: [{ to: "/console/budgets", label: "Budgets & Alerts", icon: BellRing }], }, ]; const ADMIN_ONLY_SECTIONS: NavSection[] = [ { title: "Configuration", items: [ { to: "/console/providers", label: "Providers", icon: Server }, // Every OAuth-backed account type is added and managed from this one // page (Cline, WorkBuddy, Hermes). The dedicated WorkBuddy / Hermes OAuth // / Account login pages remain reachable by URL for bulk import-export // and deep links, but they no longer each take a sidebar row. { to: "/console/oauth", label: "Accounts", icon: Link2 }, { to: "/console/hermes", label: "Hermes", icon: Hexagon }, { to: "/console/builtin-providers", label: "Built-in Providers", icon: Boxes }, { to: "/console/combos", label: "Combos", icon: Layers }, ], }, { title: "Admin", items: [ { to: "/console/proxy-logs", label: "Proxy Logs", icon: Terminal }, { to: "/console/audit", label: "Audit Log", icon: History }, { to: "/console/users", label: "Users", icon: Users }, { to: "/console/settings", label: "Settings", icon: CreditCard }, ], }, ]; const OAUTH_TITLES: Record = { cline: "Cline", workbuddy: "WorkBuddy", hermes: "Hermes", }; const PAGE_META: Record = { "/console": { title: "Dashboard", section: "Overview" }, "/console/request-logs": { title: "Request Logs", section: "Traffic" }, "/console/proxy-logs": { title: "Proxy Logs", section: "Traffic" }, "/console/audit": { title: "Audit Log", section: "Admin" }, "/console/usage": { title: "Usage", section: "Traffic" }, "/console/analytics": { title: "Analytics", section: "Traffic" }, "/console/providers": { title: "Providers", section: "Configuration" }, "/console/oauth": { title: "Connected accounts", section: "Configuration" }, "/console/login": { title: "Account login", section: "Configuration" }, "/console/workbuddy": { title: "WorkBuddy", section: "Configuration" }, "/console/hermes": { title: "Hermes", section: "Configuration" }, "/console/hermes-oauth": { title: "Hermes OAuth", section: "Configuration" }, "/console/builtin-providers": { title: "Built-in Providers", section: "Configuration" }, "/console/models": { title: "Models", section: "Configuration" }, "/console/combos": { title: "Combos", section: "Configuration" }, "/console/keys": { title: "Virtual Keys", section: "Configuration" }, "/console/budgets": { title: "Budgets & Alerts", section: "Admin" }, "/console/settings": { title: "Settings", section: "Admin" }, "/console/users": { title: "Users", section: "Admin" }, }; function LiveClock() { const { prefs } = useClientPrefs(); const [time, setTime] = useState(""); useEffect(() => { const update = () => setTime( new Date().toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit", second: "2-digit", hour12: !prefs.clock24h, }), ); update(); const interval = setInterval(update, 1000); return () => clearInterval(interval); }, [prefs.clock24h]); return ( {time} ); } export function AdminLayout() { const { user, logout } = useAuth(); const navigate = useNavigate(); const location = useLocation(); const connected = useAdminStream("__noop__", () => undefined); const [collapsed, setCollapsed] = useState(false); const [mobileOpen, setMobileOpen] = useState(false); const isDesktop = useIsDesktop(); const sidebarWidth = collapsed ? SIDEBAR_COLLAPSED : SIDEBAR_WIDE; const showCollapsed = collapsed && isDesktop; const drawerRef = useRef(null); const toggleRef = useRef(null); const isAdmin = user?.role === "admin"; // Fold the admin-only sections into their user counterparts by title so // each section renders exactly once. USER_NAV_SECTIONS and // ADMIN_ONLY_SECTIONS both define "Configuration" and "Admin" sections — // concatenating them produced duplicate React keys AND duplicated // headers for admins. Merging keeps a single section per title while an // admin still sees every item. const navSections = useMemo(() => { const merged: NavSection[] = []; const byTitle = new Map(); const lists = isAdmin ? [USER_NAV_SECTIONS, ADMIN_ONLY_SECTIONS] : [USER_NAV_SECTIONS]; for (const list of lists) { for (const section of list) { const existing = byTitle.get(section.title); if (existing) { existing.items.push(...section.items); } else { const copy: NavSection = { title: section.title, items: [...section.items] }; byTitle.set(section.title, copy); merged.push(copy); } } } return merged; }, [isAdmin]); // Cmd/Ctrl+B toggles the sidebar useEffect(() => { const onKey = (e: KeyboardEvent) => { if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "b") { e.preventDefault(); setCollapsed((c) => !c); } }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, []); // Close mobile sidebar on route change useEffect(() => setMobileOpen(false), [location.pathname]); // Leaving the mobile breakpoint drops the overlay drawer entirely. useEffect(() => { if (isDesktop) setMobileOpen(false); }, [isDesktop]); // Drawer a11y + ergonomics: Escape closes, background scroll locks, focus // moves into the panel and returns to the toggle afterwards. const wasOpen = useRef(false); useEffect(() => { if (!mobileOpen) { if (wasOpen.current) toggleRef.current?.focus(); wasOpen.current = false; return; } wasOpen.current = true; drawerRef.current?.focus(); const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") setMobileOpen(false); }; const prevOverflow = document.body.style.overflow; document.body.style.overflow = "hidden"; window.addEventListener("keydown", onKey); return () => { window.removeEventListener("keydown", onKey); document.body.style.overflow = prevOverflow; }; }, [mobileOpen]); const path = location.pathname.length > 1 ? location.pathname.replace(/\/+$/, "") : "/"; const oauthSegments = path.startsWith("/console/oauth/") ? path.slice("/console/oauth/".length).split("/").filter(Boolean) : []; const oauthMeta = oauthSegments.length === 1 ? { title: OAUTH_TITLES[oauthSegments[0]?.toLowerCase() ?? ""] ?? "Connected accounts", section: "Configuration", } : oauthSegments.length >= 2 ? { title: "Account", section: "Configuration" } : null; const meta = PAGE_META[path] ?? oauthMeta ?? { title: "wiwi", section: "Admin" }; const maskedKey = (() => { const k = getToken(); return k.length > 17 ? `${k.slice(0, 13)}…${k.slice(-4)}` : "master key"; })(); const identityName = user ? user.username : "user"; const identityRole = isAdmin ? "admin" : "user"; const identityBadge = isAdmin ? (user?.id === "master" ? "root" : "admin") : "user"; return (
{/* Ambient background: faint grid + radial glows */}
{/* ── Mobile backdrop ── */} {/* Kept mounted so it can fade; z-40 puts it above the topbar (z-30) so the whole screen dims, not just the content. */}
setMobileOpen(false)} aria-hidden /> {/* ── Sidebar ── */} {/* ── Topbar ── */}
{/* Mobile sidebar toggle */}
{meta.section}

{meta.title}

{/* Centered on desktop (absolute), trailing on mobile */}
{connected ? "live" : "offline"} {/* Compact dot-only status on narrow screens */}
{/* ── Scrollable content ── */}
); }