'use client' import { MenuItem as _MenuItem, Menu, MenuButton, MenuItems } from '@headlessui/react' import cn from 'clsx' import { Anchor, Button } from 'nextra/components' import { useFSRoute } from 'nextra/hooks' import { ArrowRightIcon, MenuIcon } from 'nextra/icons' import type { MenuItem } from 'nextra/normalize-pages' import type { FC, ReactNode } from 'react' import { setMenu, useConfig, useMenu, useThemeConfig } from 'nextra-theme-docs' import { usePathname } from 'next/navigation' import { normalizePages } from 'nextra/normalize-pages' import { PageMapItem } from 'nextra' const classes = { link: cn( 'x:text-sm x:contrast-more:text-gray-700 x:contrast-more:dark:text-gray-100 x:whitespace-nowrap', 'x:text-gray-600 x:hover:text-black x:dark:text-gray-400 x:dark:hover:text-gray-200', 'x:ring-inset x:transition-colors' ) } const NavbarMenu: FC<{ menu: MenuItem children: ReactNode }> = ({ menu, children }) => { const routes = Object.fromEntries( (menu.children || []).map(route => [route.name, route]) ) return ( cn( classes.link, 'x:items-center x:flex x:gap-1.5 x:cursor-pointer', focus && 'x:nextra-focus' ) } > {children} {Object.entries( // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- fixme (menu.items as Record) || {} ).map(([key, item]) => ( <_MenuItem key={key} as={Anchor} href={item.href || routes[key]?.route} className={({ focus }) => cn( 'x:block x:py-1.5 x:transition-colors x:ps-3 x:pe-9', focus ? 'x:text-gray-900 x:dark:text-gray-100' : 'x:text-gray-600 x:dark:text-gray-400' ) } > {item.title} ))} ) } const isMenu = (page: any): page is MenuItem => page.type === 'menu' export const ClientNavbar: FC<{ pageMap: PageMapItem[] children: ReactNode className?: string }> = ({ pageMap, children, className }) => { const { topLevelNavbarItems } = normalizePages({ list: pageMap, route: usePathname() }) // filter out titles for elements in topLevelNavbarItems with non empty route const existingCourseNames = new Set( topLevelNavbarItems.filter( item => !('href' in item) ).map(item => item.title) ) // console.log(existingCourseNames) // filter out elements in topLevelNavbarItems with url but have title in existingCourseNames const filteredTopLevelNavbarItems = topLevelNavbarItems.filter(item => !('href' in item && existingCourseNames.has(item.title))) // const items = topLevelNavbarItems // use filteredTopLevelNavbarItems to generate items const items = filteredTopLevelNavbarItems // console.log(filteredTopLevelNavbarItems) const themeConfig = useThemeConfig() const pathname = useFSRoute() const menu = useMenu() return ( <>
{items.map((page, _index, arr) => { if ('display' in page && page.display === 'hidden') return if (isMenu(page)) { return ( {page.title} ) } const href = // If it's a directory ('frontMatter' in page ? page.route : page.firstChildRoute) || page.href || page.route const isCurrentPage = href === pathname || (pathname.startsWith(page.route + '/') && arr.every(item => !('href' in item) || item.href !== pathname)) || undefined return ( {page.title} ) })}
{themeConfig.search && (
{themeConfig.search}
)} {children} ) }