bytelyst-devops-tools/dashboard/web/src/components/sidebar-nav.tsx
Hermes VM 1099d518ef improve: dashboard security, code quality, and UX fixes
Security (backend):
- env/routes: add requireAdmin to all 6 env endpoints — GET /env was
  fully open, exposing all secret values to unauthenticated requests
- deployments/routes: add requireAdmin to all 4 GET endpoints (deployment
  history and logs were publicly readable)
- health/routes: remove duplicate requireAdmin call from DELETE /health/cache
  handler body (was already enforced via preHandler)

Frontend — auth/api:
- system/page: replace raw fetch + localStorage token with apiRequest
  (mutations now go through CSRF flow)
- vm/page: same — replace raw fetch with vmApi from api.ts
- api.ts: add vmApi (getHealth, getCleanupLog, runCleanup) + shared
  VmHealthResult / VmCheck / VmCheckLevel types

Shared utilities:
- utils.ts: add formatBytes() and getStatusColor() shared helpers
- system/page: remove duplicate formatBytes, import from utils
- health/page: remove duplicate getStatusColor, import from utils
- page.tsx (home): remove duplicate getStatusColor, import from utils

UX improvements:
- page.tsx: remove Seed Services button from normal header (debug tool)
- page.tsx: deploy button now always enabled; shows inline warning banner
  when service is not 'up' instead of silently disabling the button
- metrics: fix bar chart — bars now grow from bottom (flex-col-reverse),
  add empty state, fix date parsing timezone edge case
- sidebar-nav: theme toggle now functional — persists to localStorage and
  toggles document.documentElement class 'dark'

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-27 18:53:20 +00:00

169 lines
5.5 KiB
TypeScript

'use client';
import { useState, useEffect } from 'react';
import Link from 'next/link';
import { usePathname, useRouter } from 'next/navigation';
import {
LayoutDashboard,
Activity,
BarChart3,
Cpu,
Key,
Code2,
Settings,
LogOut,
Menu,
X,
Sun,
Moon,
HeartPulse,
Sparkles,
Server,
} from 'lucide-react';
import { useAuth } from '@/lib/auth';
const navItems = [
{ href: '/', label: 'Dashboard', icon: LayoutDashboard },
{ href: '/hermes', label: 'Hermes', icon: Sparkles },
{ href: '/health', label: 'Health', icon: HeartPulse },
{ href: '/metrics', label: 'Metrics', icon: BarChart3 },
{ href: '/vm', label: 'VM Health', icon: Server },
{ href: '/system', label: 'System', icon: Cpu },
{ href: '/env', label: 'Environment', icon: Key },
{ href: '/code-quality', label: 'Code Quality', icon: Code2 },
{ href: '/settings/cosmos', label: 'Settings', icon: Settings },
];
export function SidebarNav() {
const pathname = usePathname();
const router = useRouter();
const { user, logout } = useAuth();
const [mobileOpen, setMobileOpen] = useState(false);
const [theme, setTheme] = useState<'light' | 'dark'>('light');
// Sync theme from localStorage on mount
useEffect(() => {
const saved = (localStorage.getItem('theme') as 'light' | 'dark') || 'light';
setTheme(saved);
document.documentElement.classList.toggle('dark', saved === 'dark');
}, []);
const handleLogout = () => {
logout();
router.push('/login');
};
const initials = user?.email
? user.email.slice(0, 2).toUpperCase()
: '??';
const sidebarContent = (
<>
{/* Logo */}
<div className="flex h-16 items-center justify-between border-b px-6">
<div className="flex items-center gap-2">
<div className="flex h-8 w-8 items-center justify-center rounded-lg bg-blue-600">
<LayoutDashboard className="h-4 w-4 text-white" />
</div>
<div>
<h1 className="text-sm font-bold">DevOps</h1>
<p className="text-[10px] text-gray-500">Dashboard</p>
</div>
</div>
{/* Mobile close button */}
<button
className="md:hidden text-gray-500 hover:text-gray-700"
onClick={() => setMobileOpen(false)}
>
<X className="h-5 w-5" />
</button>
</div>
{/* Navigation */}
<nav className="flex-1 space-y-1 px-3 py-4">
{navItems.map(item => {
const isActive = pathname === item.href ||
(item.href !== '/' && pathname.startsWith(item.href));
return (
<Link
key={item.href}
href={item.href}
onClick={() => setMobileOpen(false)}
className={`flex items-center gap-3 rounded-lg px-3 py-2 text-sm font-medium transition-colors ${
isActive
? 'bg-blue-600 text-white'
: 'text-gray-700 hover:bg-gray-100'
}`}
>
<item.icon className="h-4 w-4" />
<span>{item.label}</span>
</Link>
);
})}
</nav>
{/* Footer — theme toggle + user info + logout */}
<div className="border-t p-4 space-y-3">
<button
onClick={() => {
const next = theme === 'dark' ? 'light' : 'dark';
setTheme(next);
localStorage.setItem('theme', next);
document.documentElement.classList.toggle('dark', next === 'dark');
}}
className="flex w-full items-center gap-3 rounded-lg px-3 py-2 text-sm text-gray-700 hover:bg-gray-100 transition-colors"
>
{theme === 'dark' ? <Sun className="h-4 w-4" /> : <Moon className="h-4 w-4" />}
{theme === 'dark' ? 'Light Mode' : 'Dark Mode'}
</button>
<div className="flex items-center gap-3">
<div className="flex h-8 w-8 items-center justify-center rounded-full bg-gray-200 text-xs font-bold text-gray-700">
{initials}
</div>
<div className="flex-1 min-w-0">
<p className="text-sm font-medium truncate text-gray-900">{user?.email ?? 'Admin'}</p>
<p className="text-xs text-gray-500 truncate">{user?.role ?? 'admin'}</p>
</div>
<button
onClick={handleLogout}
title="Sign out"
aria-label="Sign out"
className="text-gray-500 hover:text-gray-700"
>
<LogOut className="h-4 w-4" />
</button>
</div>
</div>
</>
);
return (
<>
{/* Mobile hamburger */}
<div className="fixed top-0 left-0 right-0 z-30 flex h-14 items-center border-b bg-white px-4 md:hidden">
<button onClick={() => setMobileOpen(true)}>
<Menu className="h-6 w-6" />
</button>
<span className="ml-3 text-sm font-bold">DevOps Dashboard</span>
</div>
{/* Spacer for mobile top bar */}
<div className="h-14 md:hidden" />
{/* Mobile overlay */}
{mobileOpen && (
<div
className="fixed inset-0 z-40 bg-black/50 md:hidden"
onClick={() => setMobileOpen(false)}
/>
)}
{/* Sidebar — static on desktop, fixed on mobile */}
<aside
className={`hidden md:flex md:w-64 md:shrink-0 md:flex-col md:border-r md:bg-white fixed inset-y-0 left-0 z-50 flex w-64 flex-col border-r bg-white transition-transform duration-200 translate-x-[-100%] md:translate-x-0 ${mobileOpen ? 'translate-x-0' : ''}`}
>
{sidebarContent}
</aside>
</>
);
}