import { useNavigate, useLocation, Link } from '@tanstack/react-router' import { useQuery } from '@tanstack/react-query' import { Home, Search, Clock, BarChart2, Settings2, LogOut, Sun, Moon, ChevronLeft, ChevronRight, Zap, Bell, } from 'lucide-react' import { useUIStore } from '@/stores/uiStore' import { useAuth } from '@/hooks/useAuth' import { useNotifications } from '@/hooks/useNotifications' import { NotificationCenter } from './NotificationCenter' import { apiFetch } from '@/lib/http' import { cn } from '@/lib/utils' import { useState } from 'react' // ─── Types ──────────────────────────────────────────────────────────────────── interface RecentQuery { id: string query: string created_at: string success: boolean } // ─── Data fetching ──────────────────────────────────────────────────────────── async function fetchRecentQueries(): Promise { const res = await apiFetch('/api/workspace/history?page=1&limit=6') const data = await res.json() return (data.items ?? []) as RecentQuery[] } // ─── Nav definition ─────────────────────────────────────────────────────────── const BASE_NAV = [ { to: '/', Icon: Home, label: 'Home' }, { to: '/query', Icon: Search, label: 'Ask' }, { to: '/workspace', Icon: Clock, label: 'History' }, { to: '/analytics', Icon: BarChart2, label: 'Analytics' }, ] const ADMIN_NAV = { to: '/admin', Icon: Settings2, label: 'Admin' } // ─── Sidebar ────────────────────────────────────────────────────────────────── export function Sidebar() { const { sidebarOpen, toggleSidebar, theme, toggleTheme } = useUIStore() const { user, signOut } = useAuth() const navigate = useNavigate() const { pathname } = useLocation() const { unreadCount } = useNotifications() const [notifOpen, setNotifOpen] = useState(false) const { data: recent = [] } = useQuery({ queryKey: ['sidebar-recent'], queryFn: fetchRecentQueries, staleTime: 30_000, refetchInterval: 60_000, enabled: !!user, }) const handleSignOut = async () => { await signOut() navigate({ to: '/login' }) } const navItems = (user?.role === 'admin' || user?.role === 'org_admin') ? [...BASE_NAV, ADMIN_NAV] : BASE_NAV return ( <> {/* Notification center — portal-rendered */} setNotifOpen(false)} /> ) }