'use client'; import { useVconRole } from '@/components/layout/VconShell'; import { Button } from '@/components/ui/Button'; import { Icon } from '@/components/ui/Icon'; import { cn } from '@/lib/utils'; import { getCached, setCached, clearAuthCaches } from '@/lib/vconCache'; import Link from 'next/link'; import { usePathname } from 'next/navigation'; import React, { useEffect, useState } from 'react'; interface SidebarProps { isOpen?: boolean; onClose?: () => void; } interface CachedProfile { email?: string; role?: string; } function formatShellRoleLabel(role?: string | null): string { const clean = String(role || 'viewer').trim().toLowerCase(); if (clean === 'owner') return 'Owner'; if (clean === 'admin') return 'Admin'; if (clean === 'operator') return 'Operator'; return 'Viewer'; } const NAVIGATION_SECTIONS = [ { group: 'Main', items: [ { name: 'Dashboard', href: '/vcon/dashboard', icon: 'LayoutDashboard' }, { name: 'Events', href: '/vcon/data', icon: 'Activity' }, { name: 'Clients', href: '/vcon/clients', icon: 'Key' }, { name: 'Export', href: '/vcon/export', icon: 'Download' }, { name: 'Storage', href: '/vcon/storage', icon: 'HardDrive' }, { name: 'Files Manager', href: '/vcon/files', icon: 'FolderOpen' }, ], }, { group: 'System', items: [ { name: 'Client SDKs', href: '/vcon/sdk', icon: 'FileCode2' }, { name: 'Simulator', href: '/vcon/simulator', icon: 'Play' }, { name: 'Team & RBAC', href: '/vcon/team', icon: 'Users' }, { name: 'Audit Log', href: '/vcon/audit', icon: 'FileText' }, { name: 'MCP Server', href: '/vcon/mcp', icon: 'Cpu' }, { name: 'Settings', href: '/vcon/settings', icon: 'Sliders' }, { name: 'Profile', href: '/vcon/profile', icon: 'User' }, ], }, ]; export const Sidebar: React.FC = ({ isOpen = false, onClose, }) => { const pathname = usePathname(); const shellRole = useVconRole(); const initialRoleLabel = formatShellRoleLabel(shellRole); const [isSigningOut, setIsSigningOut] = useState(false); const [theme, setTheme] = useState<'dark' | 'light'>('dark'); const [adminEmail, setAdminEmail] = useState( () => getCached('vcon:profile', {}).email || 'Admin' ); const [adminRole, setAdminRole] = useState( () => getCached('vcon:profile', {}).role || initialRoleLabel ); useEffect(() => { const saved = localStorage.getItem('datastack-theme') as 'dark' | 'light' | null; const initial = saved === 'light' ? 'light' : 'dark'; setTheme(initial); document.documentElement.classList.toggle('light', initial === 'light'); document.documentElement.classList.toggle('dark', initial === 'dark'); }, []); useEffect(() => { const cached = getCached('vcon:profile', {}); if (cached.email) { setAdminEmail(cached.email); if (cached.role) setAdminRole(cached.role); } const handleProfileUpdated = (event: Event) => { const detail = (event as CustomEvent).detail; if (detail?.email) { setAdminEmail(detail.email); if (detail.role) setAdminRole(detail.role); } }; window.addEventListener('datastack:profile-updated', handleProfileUpdated); fetch('/api/auth/profile', { credentials: 'include', cache: 'no-store', }) .then((res) => { if (res.status === 401) { window.location.assign('/vcon/login'); return null; } return res.ok ? res.json() : null; }) .then((data) => { if (data?.profile?.email) { setCached('vcon:profile', data.profile); setCached('vcon:profile-loaded', true); setAdminEmail(data.profile.email); setAdminRole(data.profile.role || initialRoleLabel); } }) .catch(() => { // Keep cached or shell role identity label if offline }); return () => { window.removeEventListener('datastack:profile-updated', handleProfileUpdated); }; }, [pathname, initialRoleLabel]); const toggleTheme = () => { const nextTheme = theme === 'dark' ? 'light' : 'dark'; setTheme(nextTheme); localStorage.setItem('datastack-theme', nextTheme); document.documentElement.classList.toggle('light', nextTheme === 'light'); document.documentElement.classList.toggle('dark', nextTheme === 'dark'); }; const signOut = async () => { setIsSigningOut(true); try { const response = await fetch('/api/auth/logout', { method: 'POST', credentials: 'include', }); if (!response.ok) throw new Error('Unable to sign out'); await clearAuthCaches(); window.location.assign('/vcon/login'); } catch { setIsSigningOut(false); } }; const avatarInitial = adminEmail.trim().charAt(0).toUpperCase() || 'A'; return ( <> {isOpen && (
)} ); }; export default Sidebar;