import React, { useState, useEffect } from 'react'; import clsx from 'clsx'; import { NavLink, useLocation } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { useUIStore } from '@/store/uiStore'; import { Calendar, ChevronRight, FileText, Home, Mail, Monitor, Users } from 'lucide-react'; interface NavLeaf { to: string; labelKey: string; } interface NavTreeItem { labelKey: string; icon: React.ReactNode; children: NavLeaf[]; } interface NavSingleItem { to: string; labelKey: string; icon: React.ReactNode; } const chevronIcon = (expanded: boolean) => ( ); const singleItems: NavSingleItem[] = [ { to: '/dashboard', labelKey: 'nav.dashboard', icon: }, { to: '/contacts', labelKey: 'nav.contacts', icon: }, ]; const treeItems: NavTreeItem[] = [ { labelKey: 'nav.calendar', icon: , children: [ { to: '/calendar', labelKey: 'nav.calendar' }, { to: '/calendar/kanban', labelKey: 'nav.calendarKanban' }, ], }, { labelKey: 'nav.files', icon: , children: [ { to: '/dms', labelKey: 'nav.files' }, { to: '/dms/trash', labelKey: 'nav.filesTrash' }, ], }, { labelKey: 'nav.email', icon: , children: [ { to: '/mail', labelKey: 'nav.email' }, { to: '/mail/settings', labelKey: 'nav.emailSettings' }, ], }, ]; const bottomItems: NavSingleItem[] = [ { to: '/ai-assistant', labelKey: 'nav.aiAssistant', icon: }, ]; export function Sidebar() { const { t } = useTranslation(); const { sidebarOpen, setSidebarOpen } = useUIStore(); const location = useLocation(); const initialExpanded = treeItems .filter((item) => item.children.some((child) => location.pathname.startsWith(child.to))) .map((item) => item.labelKey); const [expandedItems, setExpandedItems] = useState>(new Set(initialExpanded)); useEffect(() => { setExpandedItems((prev) => { const next = new Set(prev); treeItems.forEach((item) => { const isActive = item.children.some((child) => location.pathname.startsWith(child.to)); if (isActive) next.add(item.labelKey); }); return next; }); }, [location.pathname]); const toggleExpand = (labelKey: string) => { setExpandedItems((prev) => { const next = new Set(prev); if (next.has(labelKey)) next.delete(labelKey); else next.add(labelKey); return next; }); }; const isChildActive = (children: NavLeaf[]) => children.some((child) => location.pathname.startsWith(child.to)); return ( <> {sidebarOpen && ( setSidebarOpen(false)} aria-hidden="true" data-testid="sidebar-overlay" /> )} > ); }