import { useCallback, useEffect, useRef, useState } from 'react' import { Link, useLocation } from 'react-router-dom' import type { LucideIcon } from 'lucide-react' import { LayoutGrid, Clock, AlertTriangle, GitBranch, Layers, Code2, Wand2, ListChecks, Download, BarChart3, Rocket, BookOpen, MessageSquare, Settings, Pin, PinOff, } from 'lucide-react' import { cn } from '@/lib/utils' import { useUserPreferencesStore } from '@/store/userPreferencesStore' import { sidebarApi } from '@/api' import type { SidebarStatsResponse } from '@/api/sidebar' import { prefetchForRoute } from '@/lib/routePrefetch' /* ── Types ──────────────────────────────────────────── */ interface NavSubItem { href: string label: string count?: number } interface NavEntry { href: string icon: LucideIcon label: string shortLabel: string badge?: number matchPaths?: string[] children?: NavSubItem[] } interface NavSection { title: string items: NavEntry[] } /* ── Sidebar component ──────────────────────────────── */ export function Sidebar() { const location = useLocation() const sidebarPinned = useUserPreferencesStore(s => s.sidebarPinned) const toggleSidebarPinned = useUserPreferencesStore(s => s.toggleSidebarPinned) const [stats, setStats] = useState(null) const [flyoutIndex, setFlyoutIndex] = useState(null) const flyoutTimeout = useRef | null>(null) const sidebarRef = useRef(null) /* ── Stats fetching ───────────────────────────────── */ const refreshStats = useCallback(() => { sidebarApi.getStats().then(setStats).catch(() => {}) }, []) useEffect(() => { refreshStats() }, [location.pathname, refreshStats]) useEffect(() => { window.addEventListener('session-changed', refreshStats) return () => window.removeEventListener('session-changed', refreshStats) }, [refreshStats]) /* ── Navigation data ──────────────────────────────── */ const sections: NavSection[] = [ { title: 'RESOLVE', items: [ { href: '/', icon: LayoutGrid, label: 'Dashboard', shortLabel: 'Dash' }, { href: '/sessions', icon: Clock, label: 'Active Sessions', shortLabel: 'Sessions', badge: stats?.active_count || undefined, matchPaths: ['/sessions'] }, { href: '/escalations', icon: AlertTriangle, label: 'Escalations', shortLabel: 'Escal', badge: stats?.escalation_count || undefined }, ], }, { title: 'KNOWLEDGE', items: [ { href: '/trees', icon: GitBranch, label: 'Flows', shortLabel: 'Flows', badge: stats?.tree_counts.total || undefined, matchPaths: ['/trees', '/flows', '/my-trees'], children: [ { href: '/trees', label: 'All Flows' }, { href: '/trees?type=troubleshooting', label: 'Troubleshooting', count: stats?.tree_counts.troubleshooting || undefined }, { href: '/trees?type=procedural', label: 'Projects', count: stats?.tree_counts.procedural || undefined }, { href: '/trees?type=maintenance', label: 'Maintenance', count: stats?.tree_counts.maintenance || undefined }, ], }, { href: '/step-library', icon: Layers, label: 'Step Library', shortLabel: 'Steps' }, { href: '/scripts', icon: Code2, label: 'Scripts', shortLabel: 'Scripts' }, { href: '/script-builder', icon: Wand2, label: 'Script Builder', shortLabel: 'Builder' }, { href: '/review-queue', icon: ListChecks, label: 'Review Queue', shortLabel: 'Review' }, ], }, { title: 'INSIGHTS', items: [ { href: '/shares', icon: Download, label: 'Exports', shortLabel: 'Export' }, { href: '/analytics', icon: BarChart3, label: 'Analytics', shortLabel: 'Stats' }, { href: '/analytics/flowpilot', icon: Rocket, label: 'FlowPilot Analytics', shortLabel: 'FPilot' }, ], }, ] const footerItems: NavEntry[] = [ { href: '/guides', icon: BookOpen, label: 'User Guides', shortLabel: 'Guides' }, { href: '/feedback', icon: MessageSquare, label: 'Feedback', shortLabel: 'Feedbk' }, { href: '/account', icon: Settings, label: 'Account', shortLabel: 'Acct' }, ] /* ── Active detection ─────────────────────────────── */ const isActive = (item: NavEntry) => { if (item.matchPaths) return item.matchPaths.some(p => location.pathname.startsWith(p)) if (item.href === '/') return location.pathname === '/' return location.pathname.startsWith(item.href) } const isChildActive = (child: NavSubItem) => { const fullPath = location.pathname + location.search return fullPath === child.href || fullPath.startsWith(child.href + '&') } /* ── Flyout management ────────────────────────────── */ const openFlyout = (key: string) => { if (flyoutTimeout.current) clearTimeout(flyoutTimeout.current) setFlyoutIndex(key) } const closeFlyout = () => { flyoutTimeout.current = setTimeout(() => setFlyoutIndex(null), 120) } const keepFlyout = () => { if (flyoutTimeout.current) clearTimeout(flyoutTimeout.current) } /* Close flyout on Escape */ useEffect(() => { const handleKey = (e: KeyboardEvent) => { if (e.key === 'Escape') setFlyoutIndex(null) } document.addEventListener('keydown', handleKey) return () => document.removeEventListener('keydown', handleKey) }, []) /* ── Wheel forwarding (when sidebar can't scroll) ── */ const handleWheel = (e: React.WheelEvent) => { const el = e.currentTarget const canScroll = el.scrollHeight > el.clientHeight const atTop = el.scrollTop <= 0 const atBottom = el.scrollTop + el.clientHeight >= el.scrollHeight - 1 if (!canScroll || (e.deltaY < 0 && atTop) || (e.deltaY > 0 && atBottom)) { const main = document.querySelector('.main-content') as HTMLElement | null if (main) { main.scrollTop += e.deltaY; e.preventDefault() } } } /* ── Render helpers ───────────────────────────────── */ const renderRailItem = (item: NavEntry, key: string) => { const active = isActive(item) const Icon = item.icon const hasChildren = item.children && item.children.length > 0 return (
hasChildren && !sidebarPinned ? openFlyout(key) : undefined} onMouseLeave={() => hasChildren && !sidebarPinned ? closeFlyout() : undefined} > prefetchForRoute(item.href)} onFocus={() => hasChildren && !sidebarPinned ? openFlyout(key) : undefined} onBlur={() => hasChildren && !sidebarPinned ? closeFlyout() : undefined} className={cn( 'group relative flex flex-col items-center justify-center rounded-lg px-1 py-2 transition-all duration-150', active ? 'bg-[rgba(34,211,238,0.10)] text-[#67e8f9]' : 'text-[#6b7280] hover:text-[#848b9b]' )} title={item.label} > {item.badge !== undefined && item.badge > 0 && ( {item.badge > 99 ? '99+' : item.badge} )} {item.shortLabel} {/* Flyout panel (icon rail only) */} {hasChildren && !sidebarPinned && flyoutIndex === key && (
{ const itemEl = sidebarRef.current.querySelector(`[data-flyout-key="${key}"]`) if (itemEl) { const rect = itemEl.getBoundingClientRect() return `${rect.top}px` } return '0px' })() : '0px', maxHeight: '70vh', overflowY: 'auto', }} onMouseEnter={keepFlyout} onMouseLeave={closeFlyout} >
{item.children!.map(child => ( {child.label} {child.count !== undefined && ( {child.count} )} ))}
)}
) } const renderPinnedItem = (item: NavEntry, key: string) => { const active = isActive(item) const Icon = item.icon const fullPath = location.pathname + location.search const activeChild = item.children?.find(c => fullPath === c.href || fullPath.startsWith(c.href + '&')) const isParentDimmed = !!activeChild && active return (
prefetchForRoute(item.href)} className={cn( 'group relative flex items-center gap-3 rounded-lg px-3 py-2 text-[0.8125rem] font-medium transition-all duration-150', active ? isParentDimmed ? 'bg-[rgba(34,211,238,0.05)] text-[#e2e5eb]/70' : 'bg-[rgba(34,211,238,0.10)] text-[#e2e5eb]' : 'text-[#848b9b] hover:bg-[#191c25] hover:text-[#e2e5eb]' )} > {active && !isParentDimmed && (
)} {item.label} {item.badge !== undefined && item.badge > 0 && ( {item.badge} )} {/* Sub-items for pinned mode */} {item.children && item.children.length > 0 && (
{item.children.map(child => { const childActive = isChildActive(child) return ( {child.label} {child.count !== undefined && ( {child.count} )} ) })}
)}
) } /* ── Flyout positioning: use data attribute for lookup ── */ const renderRailItemWithRef = (item: NavEntry, key: string) => { return (
{renderRailItem(item, key + '-inner')}
) } /* ── Main render ──────────────────────────────────── */ if (sidebarPinned) { return ( ) } /* Icon Rail (default) */ return ( ) }