'use client'; import { Button } from '@/components/ui/Button'; import { Icon } from '@/components/ui/Icon'; import { cn } from '@/lib/utils'; import { getCached, setCached } 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; } 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' }, ], }, { group: 'System', items: [ { name: 'Python SDK', href: '/vcon/sdk', icon: 'FileCode2' }, { name: 'Simulator', href: '/vcon/simulator', icon: 'Play' }, { 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 [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 || 'Owner' ); 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); } fetch('/api/auth/profile', { credentials: 'include' }) .then((res) => (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 || 'Owner'); } }) .catch(() => { // Keep cached or default identity label if offline }); }, [pathname]); 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'); window.location.assign('/vcon/login'); } catch { setIsSigningOut(false); } }; const avatarInitial = adminEmail.trim().charAt(0).toUpperCase() || 'A'; return ( <> {isOpen && (
)} ); }; export default Sidebar;