"use client" import { useState, useRef, useEffect } from "react" import Link from "next/link" import { useAuth } from "@/hooks/useAuth" import { UserRole } from "@/types/auth" import { Pill, ChevronDown } from "lucide-react" interface NavItem { label: string href: string } const LojaVirtualMenu = () => { const [isOpen, setIsOpen] = useState(false) const timeoutRef = useRef(null) const dropdownRef = useRef(null) const { user } = useAuth() // Função para filtrar itens baseado no role do usuário const filterItemsByRole = (items: NavItem[], userRole?: UserRole): NavItem[] => { if (!userRole) return [] // COLABORADOR: acesso básico if (userRole === UserRole.COLABORADOR) { const allowedPaths = [ '/entregas', '/produtos', '/carrinhos', '/pedidos' ] return items.filter(item => allowedPaths.includes(item.href)) } // ADMIN: acesso intermediário if (userRole === UserRole.ADMIN) { const restrictedPaths = [ '/usuarios', // Apenas SUPERADMIN '/enderecos', // Apenas SUPERADMIN '/dashboard/empresa-novo' // Apenas SUPERADMIN ] return items.filter(item => !restrictedPaths.includes(item.href)) } // SUPERADMIN: acesso completo a todos os itens if (userRole === UserRole.SUPERADMIN) { return items // Mostra todos os itens } return [] } const allItems: NavItem[] = [ { label: "Produtos", href: "/produtos" }, { label: "Categorias", href: "/categorias" }, { label: "Carrinhos", href: "/carrinhos" }, { label: "Pedidos", href: "/pedidos" }, { label: "Entregas", href: "/entregas" }, { label: "Faturas", href: "/faturas" }, { label: "Pagamentos", href: "/pagamentos" }, { label: "Laboratórios", href: "/laboratorios" }, { label: "Usuários", href: "/usuarios" }, { label: "Endereços", href: "/enderecos" }, { label: "Empresas", href: "/dashboard/empresa-novo" }, ] // Filtrar itens baseado no role do usuário const items = filterItemsByRole(allItems, user?.nivel as UserRole) const handleMouseEnter = () => { if (timeoutRef.current) { clearTimeout(timeoutRef.current) } setIsOpen(true) } const handleMouseLeave = () => { timeoutRef.current = setTimeout(() => { setIsOpen(false) }, 150) // 150ms delay antes de fechar } useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setIsOpen(false) } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); if (timeoutRef.current) { clearTimeout(timeoutRef.current) } } }, []) // Se não há itens visíveis para o usuário, não renderiza o menu if (items.length === 0) return null return (
{/* Dropdown Menu */} {isOpen && (
{items.map((item) => ( setIsOpen(false)} > {item.label} ))}
)}
) } export default LojaVirtualMenu