'use client' import Link from 'next/link' import { usePathname } from 'next/navigation' import React, { FC, Fragment, useEffect, useState } from 'react' import { Badge, cn, MenubarSeparator, NavigationMenu, NavigationMenuContent, NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuTrigger, navigationMenuTriggerStyle, } from 'ui' import MenuIconPicker from './MenuIconPicker' import { GLOBAL_MENU_ITEMS } from './NavigationMenu.constants' /** * Get TopNav active label based on current pathname */ export const useActiveMenuLabel = (menu: typeof GLOBAL_MENU_ITEMS) => { const pathname = usePathname() const [activeLabel, setActiveLabel] = useState('') useEffect(() => { // check if homepage if (pathname === '/') { return setActiveLabel('Home') } for (let index = 0; index < menu.length; index++) { const section = menu[index] if (section[0].enabled === false) continue // check if first level menu items match beginning of url if (section[0].href?.startsWith(pathname)) { return setActiveLabel(section[0].label) } // check if second level menu items match beginning of url if (section[0].menuItems) { section[0].menuItems.map((menuItemGroup) => menuItemGroup .filter((menuItem) => menuItem.enabled !== false) .map( (menuItem) => menuItem.href?.startsWith(pathname) && setActiveLabel(section[0].label) ) ) } } }, [pathname, menu]) return activeLabel } const GlobalNavigationMenu: FC = () => { const activeLabel = useActiveMenuLabel(GLOBAL_MENU_ITEMS) const triggerClassName = 'h-(--header-height) p-2 bg-transparent border-0 border-b-2 border-transparent font-normal rounded-none text-foreground-light hover:text-foreground data-open:text-foreground! data-radix-collection-item:focus-visible:ring-2 data-radix-collection-item:focus-visible:ring-foreground-lighter data-radix-collection-item:focus-visible:text-foreground h-full focus-visible:rounded-sm shadow-none! outline-hidden transition-all outline-0 focus-visible:outline-4 focus-visible:outline-offset-1 focus-visible:outline-brand-600' return (