import { Fragment } from 'react'; import { Link, useNavigate } from 'react-router-dom'; import { Disclosure, Menu, Transition } from '@headlessui/react'; import { Bars3Icon, XMarkIcon } from '@heroicons/react/24/outline'; import { useAuth } from '../../contexts/AuthContext'; function classNames(...classes) { return classes.filter(Boolean).join(' '); } export default function Navbar() { const { currentUser, logout, hasRole } = useAuth(); const navigate = useNavigate(); const handleLogout = () => { logout(); navigate('/login'); }; // Define navigation items based on user role const getNavigationItems = () => { const items = [ { name: 'Dashboard', href: '/dashboard', current: false }, { name: 'Products', href: '/products', current: false } ]; // Add Companies link for superadmin if (hasRole('superadmin')) { items.push({ name: 'Companies', href: '/companies', current: false }); } // Add Users link for superadmin and companyadmin if (hasRole(['superadmin', 'companyadmin'])) { items.push({ name: 'Users', href: '/users', current: false }); } return items; }; return ( {({ open }) => ( <>
{/* Mobile menu button*/} Open main menu {open ? (
InInventer
{currentUser && getNavigationItems().map((item) => ( {item.name} ))}
{currentUser ? (
Open user menu
{currentUser.email.charAt(0).toUpperCase()}
{({ active }) => (
{currentUser.email}
Role: {currentUser.role}
)}
{({ active }) => ( )}
) : ( Login )}
{currentUser && getNavigationItems().map((item) => ( {item.name} ))}
)}
); }