'use client'; import React, { useState, useEffect } from 'react'; import Link from 'next/link'; import { usePathname } from 'next/navigation'; import * as Icons from '@zen/core/shared/icons'; import { ChevronDownIcon } from '@zen/core/shared/icons'; /** * Resolve icon name (string) to icon component * Icons are passed as strings from server to avoid serialization issues */ function resolveIcon(iconNameOrComponent) { if (typeof iconNameOrComponent === 'function') { return iconNameOrComponent; } if (typeof iconNameOrComponent === 'string') { return Icons[iconNameOrComponent] || Icons.DashboardSquare03Icon; } return Icons.DashboardSquare03Icon; } const AdminSidebar = ({ isMobileMenuOpen, setIsMobileMenuOpen, appName, enabledModules, navigationSections: serverNavigationSections }) => { const pathname = usePathname(); const [collapsedSections, setCollapsedSections] = useState(new Set()); const toggleSection = (sectionId) => { setCollapsedSections(prev => { const newCollapsed = new Set(prev); if (newCollapsed.has(sectionId)) { newCollapsed.delete(sectionId); } else { newCollapsed.add(sectionId); } return newCollapsed; }); }; const handleMobileLinkClick = () => { setIsMobileMenuOpen(false); }; useEffect(() => { const handleResize = () => { if (window.innerWidth >= 1024) { setIsMobileMenuOpen(false); } }; window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); }, [setIsMobileMenuOpen]); const isSectionActive = (section) => { return section.items.some(item => item.current); }; const shouldRenderAsDirectLink = (section) => { return section.items.length === 1 && section.items[0].name.toLowerCase() === section.title.toLowerCase(); }; useEffect(() => { setCollapsedSections(prev => { const newSet = new Set(prev); navigationSections.forEach(section => { if (isSectionActive(section)) { newSet.add(section.id); } }); return newSet; }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [pathname]); const navigationSections = serverNavigationSections.map(section => ({ ...section, items: section.items.map(item => ({ ...item, current: pathname === item.href || pathname.startsWith(item.href + '/') })) })); const base = 'w-full flex items-center justify-between px-[10px] py-[7px] rounded-lg text-[13px] transition-colors duration-[120ms] ease-out'; const inactive = 'font-normal text-neutral-500 dark:text-neutral-400 hover:bg-neutral-100 dark:hover:bg-neutral-900 hover:text-neutral-900 dark:hover:text-white'; const parentBase = `${base}`; const parentActif = 'bg-neutral-100 dark:bg-neutral-900 text-black dark:text-white font-medium'; const parentActifOuvert = 'text-black dark:text-white font-medium hover:bg-neutral-100 dark:hover:bg-neutral-900'; const subItemBase = base; const subItemActif = 'bg-neutral-100 dark:bg-neutral-900 text-black dark:text-white font-medium'; const renderNavSection = (section) => { const Icon = resolveIcon(section.icon); if (shouldRenderAsDirectLink(section)) { const item = section.items[0]; return (
{section.title}
{item.badge && ( {item.badge} )}
); } const isCollapsed = !collapsedSections.has(section.id); const isActive = isSectionActive(section); return (
); }; const renderNavItem = (item) => { return (
  • {item.name}
    {item.badge && ( {item.badge} )}
  • ); }; return ( <> {/* Mobile overlay */} {isMobileMenuOpen && (
    setIsMobileMenuOpen(false)} /> )} {/* Sidebar */}
    {/* Logo */}

    {appName}

    {/* Navigation */}
    ); }; export default AdminSidebar;