'use client'; import React, { useState, useEffect } from 'react'; import Link from 'next/link'; import { usePathname, useRouter } from 'next/navigation'; import { Menu, Transition } from '@headlessui/react'; import { Fragment } from 'react'; import { useTheme } from 'next-themes'; import { ChevronLeftIcon, ChevronRightIcon, ChevronDownIcon, UserCircleIcon, ArrowRightOnRectangleIcon, SunIcon, MoonIcon, Cog6ToothIcon, XMarkIcon, } from '@heroicons/react/24/outline'; export interface MenuItem { id: string; label: string; href: string; icon: any; subItems?: { label: string; href: string; }[]; } interface SidebarRailProps { activeTab: string; onTabChange: (tab: string) => void; isExpanded: boolean; onToggle: () => void; menuItems: MenuItem[]; } export const SidebarRail: React.FC = ({ activeTab, onTabChange, isExpanded, onToggle, menuItems, }) => { const pathname = usePathname(); const router = useRouter(); const { theme, setTheme } = useTheme(); const [mounted, setMounted] = useState(false); const [openSubmenu, setOpenSubmenu] = useState(null); useEffect(() => { setMounted(true); }, []); // Auto-open submenu if active useEffect(() => { if (isExpanded && pathname) { const activeItem = menuItems.find(item => item.subItems?.some(sub => pathname === sub.href || pathname.startsWith(sub.href)) ); if (activeItem) { setOpenSubmenu(activeItem.id); } } }, [pathname, isExpanded, menuItems]); const handleLogout = () => { localStorage.removeItem('token'); localStorage.removeItem('user'); router.push('/login'); }; const toggleTheme = () => { setTheme(theme === 'dark' ? 'light' : 'dark'); }; // Encontrar o item ativo para renderizar o submenu const activeMenuItem = menuItems.find(item => item.id === openSubmenu); return (
{/* Toggle Button - Floating on the border */} {/* Header com Logo */}
{/* Logo */}
A
{/* Título com animação */}
Aggios
{/* Navegação */}
{menuItems.map((item) => ( { if (item.subItems) { setOpenSubmenu(openSubmenu === item.id ? null : item.id); } else { onTabChange(item.id); setOpenSubmenu(null); } }} isExpanded={isExpanded} hasSubItems={!!item.subItems} isOpen={openSubmenu === item.id} /> ))}
{/* Separador antes do menu de usuário */}
{/* User Menu - Footer */}
Agência
{({ active }) => ( )} {({ active }) => ( Configurações )} {({ active }) => ( )}
{({ active }) => ( )}
{/* Submenu Flyout Panel */}
{activeMenuItem && ( <>

{activeMenuItem.label}

{activeMenuItem.subItems?.map((sub) => ( setOpenSubmenu(null)} // Fecha ao clicar className={` flex items-center gap-2 px-3 py-2.5 rounded-lg text-xs font-medium transition-colors mb-1 ${pathname === sub.href ? 'bg-brand-50 dark:bg-brand-900/10 text-brand-600 dark:text-brand-400' : 'text-gray-600 dark:text-gray-400 hover:bg-gray-50 dark:hover:bg-zinc-800 hover:text-gray-900 dark:hover:text-white' } `} > {sub.label} ))}
)}
); }; // Subcomponente do Botão interface RailButtonProps { label: string; icon: React.ComponentType<{ className?: string }>; href: string; active: boolean; onClick: () => void; isExpanded: boolean; hasSubItems?: boolean; isOpen?: boolean; } const RailButton: React.FC = ({ label, icon: Icon, href, active, onClick, isExpanded, hasSubItems, isOpen }) => { const Wrapper = hasSubItems ? 'button' : Link; const props = hasSubItems ? { onClick, type: 'button' } : { href, onClick }; // Determine styling based on state let baseClasses = "flex items-center p-2 rounded-lg transition-all duration-300 group relative overflow-hidden w-full "; if (active && !hasSubItems) { // Active leaf item (Dashboard, etc) baseClasses += "text-white shadow-md"; } else if (isOpen) { // Open submenu parent - Highlight to show active state baseClasses += "bg-gray-100 dark:bg-zinc-800 text-gray-900 dark:text-white"; } else { // Inactive item baseClasses += "hover:bg-gray-100 dark:hover:bg-zinc-800 hover:text-gray-900 dark:hover:text-white text-gray-600 dark:text-gray-400"; } return ( {/* Ícone */} {/* Lógica Mágica do Texto: Max-Width Transition */}
{label} {hasSubItems && ( )}
{/* Indicador de Ativo (Barra lateral pequena quando fechado) */} {active && !isExpanded && !hasSubItems && (
)} ); };