'use client'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import React, { FC, useEffect, useState } from 'react'
import { Badge, cn } from 'ui'
import {
TopNavMenu,
TopNavMenuGroup,
TopNavMenuGroupLink,
TopNavMenuItem,
TopNavMenuLink,
TopNavMenuPanel,
} from 'ui-patterns/TopNav/TopNavMenu'
import type { DropdownMenuItem, GlobalMenuItems } from '../Navigation.types'
import MenuIconPicker from './MenuIconPicker'
import { IA_NAV_PENDING_CLASS, useIaNavReady, useNavItems } from '@/features/IaNav'
/**
* Get TopNav active label based on current pathname
*/
export const useActiveMenuLabel = (menu: GlobalMenuItems) => {
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 { GLOBAL_MENU_ITEMS } = useNavItems()
const isNavReady = useIaNavReady()
const activeLabel = useActiveMenuLabel(GLOBAL_MENU_ITEMS)
const itemClassName = cn(!isNavReady && IA_NAV_PENDING_CLASS)
return (
{GLOBAL_MENU_ITEMS.filter(([section]) => section.enabled !== false).map(([section]) => {
const label =
section.label === 'Home' ? : section.label
const ariaCurrent = activeLabel === section.label ? 'true' : undefined
return section.menuItems ? (
{section.menuItems.map((group, index) => (
))}
) : (
{label}
)
})}
)
}
// an item without a link is the heading of its group. the first group falls back to the menu name
const GlobalMenuGroup = ({
items,
fallbackLabel,
}: {
items: DropdownMenuItem[]
fallbackLabel?: string
}) => {
const enabledItems = items.filter((item) => item.enabled !== false)
return (
!item.href)?.label ?? fallbackLabel}>
{enabledItems
.filter((item) => item.href)
.map((item) => (
{item.label}
{item.community ? Community : null}
{item.new ? New : null}
{item.experimental ? Experimental : null}
))}
)
}
export const MenuItem = React.forwardRef<
React.ElementRef<'a'>,
React.ComponentPropsWithoutRef<'a'> & {
icon?: string
community?: boolean
new?: boolean
experimental?: boolean
}
>(
(
{ className, title, href = '', icon, community, new: isNew, experimental, children, ...props },
ref
) => {
return (
{children ?? (
<>
{icon && }
{title}
{community && Community}
{isNew && New}
{experimental && Experimental}
>
)}
)
}
)
GlobalNavigationMenu.displayName = 'GlobalNavigationMenu'
MenuItem.displayName = 'MenuItem'
export default GlobalNavigationMenu