yapapa / web /src /components /Layout.tsx
wiwi deploy
Deploy wiwi 5c54077
b5d4af3
Raw History Blame Contribute Delete
25.8 kB
// 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<string, string> = {
cline: "Cline",
workbuddy: "WorkBuddy",
hermes: "Hermes",
};
const PAGE_META: Record<string, { title: string; section: string }> = {
"/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 (
<span className="font-mono text-[10px] tabular-nums tracking-wider text-[var(--admin-text-dim)]">
{time}
</span>
);
}
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<HTMLElement>(null);
const toggleRef = useRef<HTMLButtonElement>(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<NavSection[]>(() => {
const merged: NavSection[] = [];
const byTitle = new Map<string, NavSection>();
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 (
<div data-admin className="relative z-0 h-screen overflow-hidden bg-[var(--admin-bg)]">
{/* Ambient background: faint grid + radial glows */}
<div className="pointer-events-none fixed inset-0" style={{ zIndex: 0 }}>
<div
className="absolute inset-0 opacity-[0.02]"
style={{
backgroundImage: `
linear-gradient(rgba(255,255,255,0.08) 1px, transparent 1px),
linear-gradient(90deg, rgba(255,255,255,0.08) 1px, transparent 1px)
`,
backgroundSize: "64px 64px",
}}
/>
<div
className="absolute -left-40 -top-40 h-[600px] w-[600px] rounded-full"
style={{
background:
"radial-gradient(circle, rgba(59,130,246,0.04) 0%, transparent 60%)",
}}
/>
<div
className="absolute -bottom-40 -right-40 h-[500px] w-[500px] rounded-full"
style={{
background:
"radial-gradient(circle, rgba(124,58,237,0.03) 0%, transparent 60%)",
}}
/>
<div
className="absolute left-1/3 top-1/2 h-[400px] w-[400px] -translate-y-1/2 rounded-full"
style={{
background:
"radial-gradient(circle, rgba(168,85,247,0.02) 0%, transparent 60%)",
}}
/>
</div>
{/* ── 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. */}
<div
className={`fixed inset-0 z-40 bg-black/70 backdrop-blur-sm transition-opacity duration-300 lg:hidden ${
mobileOpen ? "opacity-100" : "pointer-events-none opacity-0"
}`}
onClick={() => setMobileOpen(false)}
aria-hidden
/>
{/* ── Sidebar ── */}
<aside
id="admin-sidebar"
ref={drawerRef}
tabIndex={-1}
{...(!isDesktop && mobileOpen
? { role: "dialog", "aria-modal": true, "aria-label": "Navigation" }
: {})}
className={`fixed left-0 top-0 z-50 flex h-screen flex-col border-r border-[var(--admin-border)] bg-[var(--admin-surface)] transition-all duration-300 ease-out focus:outline-none ${
showCollapsed ? "w-[72px]" : "w-[272px] max-w-[85vw]"
} ${
mobileOpen ? "translate-x-0 shadow-2xl" : "-translate-x-full lg:translate-x-0"
}`}
style={{
paddingTop: "env(safe-area-inset-top, 0px)",
paddingBottom: "env(safe-area-inset-bottom, 0px)",
}}
>
{/* Ambient glow layers */}
<div
className="pointer-events-none absolute -left-24 -top-24 h-64 w-64 rounded-full"
style={{
background:
"radial-gradient(circle, rgba(59,130,246,0.05) 0%, rgba(124,58,237,0.02) 50%, transparent 70%)",
}}
/>
<div
className="pointer-events-none absolute -bottom-24 -right-24 h-48 w-48 rounded-full"
style={{
background:
"radial-gradient(circle, rgba(168,85,247,0.03) 0%, transparent 60%)",
}}
/>
{/* Logo */}
<div className="relative flex h-[72px] shrink-0 items-center gap-2 pr-3 pl-5">
{showCollapsed ? (
<Link to="/" className="mx-auto flex items-center">
<img src="/wiwi-logo.png" alt="wiwi" className="h-9 w-9 rounded-[10px] object-cover ring-1 ring-white/[0.06] ring-inset" />
</Link>
) : (
<>
<Link to="/" className="flex min-w-0 flex-1 items-center gap-3.5">
<img src="/wiwi-logo.png" alt="wiwi" className="h-10 w-10 shrink-0 rounded-[12px] object-cover ring-1 ring-white/[0.06] ring-inset" />
<div className="min-w-0">
<h1 className="text-[15px] font-semibold tracking-[-0.01em] text-[var(--admin-text)]">
wiwi
</h1>
<span
className="font-mono text-[9px] font-semibold uppercase tracking-[0.18em]"
style={{ color: "rgba(59, 130, 246, 0.5)" }}
>
Gateway
</span>
</div>
</Link>
<button
type="button"
onClick={() => setMobileOpen(false)}
aria-label="Close sidebar"
className="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg border border-white/[0.04] bg-white/[0.02] text-[var(--admin-text-muted)] transition-colors hover:bg-white/[0.05] hover:text-[var(--admin-text)] pointer-coarse:h-11 pointer-coarse:w-11 lg:hidden"
>
<X className="h-4 w-4" />
</button>
</>
)}
</div>
{/* Navigation */}
<nav className="admin-scroll flex-1 space-y-6 overflow-y-auto px-3 py-5">
{navSections.map((section) => (
<div key={section.title}>
{!showCollapsed && <p className="admin-label mb-2.5 px-2.5">{section.title}</p>}
<div className="space-y-0.5">
{section.items.map((item) => {
const Icon = item.icon;
return (
<NavLink
key={item.to}
to={item.to}
end={item.end}
title={showCollapsed ? item.label : undefined}
className={({ isActive }) =>
`group relative flex min-h-[44px] items-center gap-3 rounded-[12px] px-3 py-[9px] text-[13px] transition-all duration-200 lg:min-h-0 ${
showCollapsed ? "justify-center px-2" : ""
} ${
isActive
? "bg-blue-500/[0.06] text-blue-200"
: "text-[var(--admin-text-muted)] hover:bg-white/[0.02] hover:text-[var(--admin-text)]"
}`
}
>
{({ isActive }) => (
<>
{isActive && (
<span className="absolute left-0 top-1/2 h-5 w-[3px] -translate-y-1/2 rounded-r-full bg-blue-400" />
)}
<Icon
className={`h-[18px] w-[18px] shrink-0 transition-colors duration-200 ${
isActive
? "text-blue-400"
: "text-white/20 group-hover:text-white/40"
}`}
/>
{!showCollapsed && (
<span className="truncate font-medium tracking-[-0.01em]">
{item.label}
</span>
)}
</>
)}
</NavLink>
);
})}
</div>
</div>
))}
</nav>
{/* Bottom: identity + collapse */}
<div className="relative p-3">
{/* hairline divider */}
<div className="mb-3 h-px bg-gradient-to-r from-transparent via-white/[0.06] to-transparent" />
{/* ── identity card ── */}
{!showCollapsed ? (
<div className="admin-identity group relative overflow-hidden rounded-[12px] border border-white/[0.05] bg-white/[0.02] transition-colors duration-300 hover:border-white/[0.09]">
{/* top accent line */}
<span className="pointer-events-none absolute inset-x-0 top-0 h-px bg-gradient-to-r from-transparent via-blue-500/30 to-transparent" />
<div className="relative flex items-center gap-3 px-3 py-2.5">
{/* avatar + status */}
<div className="relative shrink-0">
<div
className="flex h-9 w-9 items-center justify-center rounded-[10px] bg-gradient-to-br from-blue-500/20 to-violet-500/20 ring-1 ring-white/[0.08]"
style={{ boxShadow: "0 0 14px -3px rgba(59,130,246,0.14)" }}
>
<ShieldCheck className="h-4 w-4" style={{ color: "rgba(59,130,246,0.75)" }} />
</div>
<span
className={`absolute -bottom-0.5 -right-0.5 h-2.5 w-2.5 rounded-full ring-2 ring-[var(--admin-surface)] ${
connected ? "bg-emerald-400" : "bg-zinc-600"
}`}
style={connected ? { boxShadow: "0 0 6px rgba(52,211,153,0.5)" } : undefined}
/>
</div>
{/* name + role + key */}
<div className="min-w-0 flex-1">
<div className="flex items-center gap-1.5">
<p className="truncate text-[12.5px] font-semibold tracking-[-0.01em] text-[var(--admin-text)]">
{identityName}
</p>
<span className="admin-badge admin-badge-blue !px-1.5 !py-0 !text-[8px]">
{identityBadge}
</span>
</div>
<div className="mt-1 flex items-center gap-1.5">
<KeyRound className="h-3 w-3 shrink-0 text-[var(--admin-text-dim)]" />
<p className="truncate font-mono text-[10px] text-[var(--admin-text-dim)]">
{isAdmin ? maskedKey : identityRole}
</p>
</div>
</div>
</div>
</div>
) : (
/* collapsed: avatar with status dot only */
<div className="mb-2 flex justify-center">
<div className="relative" title={identityName}>
<div
className="flex h-9 w-9 items-center justify-center rounded-[10px] bg-gradient-to-br from-blue-500/20 to-violet-500/20 ring-1 ring-white/[0.08]"
style={{ boxShadow: "0 0 14px -3px rgba(59,130,246,0.14)" }}
>
<ShieldCheck className="h-4 w-4" style={{ color: "rgba(59,130,246,0.75)" }} />
</div>
<span
className={`absolute -bottom-0.5 -right-0.5 h-2.5 w-2.5 rounded-full ring-2 ring-[var(--admin-surface)] ${
connected ? "bg-emerald-400" : "bg-zinc-600"
}`}
/>
</div>
</div>
)}
{/* ── collapse button ── */}
<button
onClick={() => setCollapsed(!collapsed)}
aria-label={showCollapsed ? "Expand sidebar" : "Collapse sidebar"}
title={showCollapsed ? "Expand sidebar" : "Collapse sidebar"}
className="admin-collapse-btn mt-2 hidden w-full items-center justify-center gap-2 rounded-[10px] border border-white/[0.04] py-2.5 font-mono text-[11px] tracking-wider text-[var(--admin-text-dim)] transition-all duration-200 hover:border-white/[0.08] hover:bg-white/[0.02] hover:text-[var(--admin-text-muted)] lg:flex"
>
<ChevronLeft
className={`h-3.5 w-3.5 transition-transform duration-300 ${showCollapsed ? "rotate-180" : ""}`}
/>
{!showCollapsed && (
<>
<span>Collapse</span>
<kbd className="admin-kbd ml-0.5">⌘B</kbd>
</>
)}
</button>
</div>
</aside>
{/* ── Topbar ── */}
<div
className="fixed top-0 z-30 transition-all duration-300"
style={{ left: 0, right: 0 }}
>
<header className="admin-topbar relative">
<div
className="flex items-center gap-3 px-4 sm:gap-4 sm:px-6"
style={{ height: `${TOPBAR_H}px` }}
>
{/* Mobile sidebar toggle */}
<button
ref={toggleRef}
onClick={() => setMobileOpen(true)}
aria-label="Open sidebar"
aria-expanded={mobileOpen}
aria-controls="admin-sidebar"
className="-ml-1 flex h-10 w-10 shrink-0 items-center justify-center rounded-lg border border-white/[0.04] bg-white/[0.02] text-[var(--admin-text-muted)] transition-colors hover:bg-white/[0.05] hover:text-[var(--admin-text)] pointer-coarse:h-11 pointer-coarse:w-11 lg:hidden"
>
<Menu className="h-5 w-5" />
</button>
<div className="min-w-0 flex-1 lg:flex-none">
<span className="text-[9px] font-semibold uppercase tracking-[0.16em] text-[var(--admin-accent)] opacity-60">
{meta.section}
</span>
<h1 className="mt-0.5 truncate text-[17px] font-semibold leading-tight tracking-[-0.02em] text-[var(--admin-text)]">
{meta.title}
</h1>
</div>
{/* Centered on desktop (absolute), trailing on mobile */}
<div className="flex shrink-0 items-center gap-2 lg:absolute lg:left-1/2 lg:-translate-x-1/2">
<span className="admin-live-badge hidden lg:inline-flex">
<span
className={connected ? "admin-pulse-dot" : "h-1.5 w-1.5 rounded-full bg-zinc-600"}
/>
{connected ? "live" : "offline"}
</span>
{/* Compact dot-only status on narrow screens */}
<span
className={`h-1.5 w-1.5 rounded-full lg:hidden ${
connected ? "admin-pulse-dot" : "bg-zinc-600"
}`}
title={connected ? "live" : "offline"}
/>
</div>
<div className="ml-auto flex shrink-0 items-center gap-2">
<div className="hidden items-center gap-1.5 px-2 md:flex">
<LiveClock />
</div>
<div className="mx-1 h-5 w-px bg-white/[0.04]" />
<button
onClick={() => {
void logout();
navigate("/login");
}}
className="flex items-center gap-2 rounded-xl border border-white/[0.04] bg-white/[0.02] px-3 py-2 text-[12px] text-[var(--admin-text-dim)] transition-all duration-200 hover:border-white/[0.08] hover:bg-white/[0.03] hover:text-red-400 pointer-coarse:min-h-11"
>
<LogOut className="h-3.5 w-3.5" />
<span className="hidden sm:inline">Sign out</span>
</button>
</div>
</div>
<div className="admin-topbar-border h-px" />
</header>
</div>
{/* ── Scrollable content ── */}
<div
className="relative h-screen transition-all duration-300 lg:ml-[var(--sidebar-w)]"
style={
{
"--sidebar-w": `${sidebarWidth}px`,
paddingTop: `${TOPBAR_H + 1}px`,
zIndex: 1,
} as React.CSSProperties
}
>
<main className="admin-scroll h-full overflow-y-auto">
<div className="admin-stagger mx-auto max-w-[1400px] p-4 sm:p-6 lg:p-8">
<Outlet />
</div>
</main>
</div>
</div>
);
}