import { type ReactNode, type PropsWithChildren, useState } from 'react' import { usePathname } from 'next/navigation' import { ActionButton } from '@keystar/ui/button' import { DialogContainer } from '@keystar/ui/dialog' import { Icon } from '@keystar/ui/icon' import { bookTextIcon } from '@keystar/ui/icon/icons/bookTextIcon' import { constructionIcon } from '@keystar/ui/icon/icons/constructionIcon' import { fileJson2Icon } from '@keystar/ui/icon/icons/fileJson2Icon' import { githubIcon } from '@keystar/ui/icon/icons/githubIcon' import { heartHandshakeIcon } from '@keystar/ui/icon/icons/heartHandshakeIcon' import { MenuTrigger, Menu, Item } from '@keystar/ui/menu' import { Divider, HStack, VStack } from '@keystar/ui/layout' import { type NavListProps, NavList as KeystarNavList, NavItem as KeystarNavItem, } from '@keystar/ui/nav-list' import { TooltipTrigger, Tooltip } from '@keystar/ui/tooltip' import { Text } from '@keystar/ui/typography' import type { ListMeta } from '../../types' import { useKeystone } from '../context' import { WelcomeDialog } from './WelcomeDialog' type NavItemProps = { /** * The content of the item. */ children: ReactNode /** * The URL to navigate to when the item is clicked. Omit the origin, as it * interferes with client-side routing. Use `/` for the dashboard. */ href: string /** * Optionally override the selected state of the item. By default, this is * determined by the current route and the `href` provided. */ isSelected?: boolean } export function getHrefFromList( list: Pick, adminPath: string = '' ) { return `${adminPath}/${list.path}${list.isSingleton ? '/1' : ''}` } /** A navigation item represents a page in the AdminUI. */ export function NavItem(props: NavItemProps) { const { children, href, isSelected: isSelectedProp } = props const pathname = usePathname() const { adminPath } = useKeystone() let ariaCurrent: 'page' | boolean | undefined = isSelectedProp if (!ariaCurrent) { if (pathname === href) { ariaCurrent = 'page' } else if ( pathname.replace(`${adminPath}`, '').split('/')[1] === href.replace(`${adminPath}`, '').split('/')[1] ) { ariaCurrent = true } } return ( {children} ) } /** Thin wrapper around "@keystar/ui" component */ export function NavList(props: NavListProps) { return } /** The root navigation component for the AdminUI */ export function NavContainer({ children }: PropsWithChildren) { return ( {children} ) } /** @private Exported for internal consumption only. */ export function Navigation() { const { adminConfig, lists: allLists, adminPath } = useKeystone() const visibleLists = Object.values(allLists).filter(x => !x.hideNavigation) if (adminConfig?.components?.Navigation) return return ( Dashboard {visibleLists.map((list: ListMeta) => ( {list.label} ))} ) } /** Should be displayed below the `NavList` component. */ export function NavFooter(props: PropsWithChildren) { return ( {props.children} ) } /** A footer item in the navigation. */ export function DeveloperResourcesMenu() { const { apiPath } = useKeystone() const [dialogIsOpen, setDialogIsOpen] = useState(false) if (process.env.NODE_ENV === 'production') return null if (!apiPath) return null // TODO: FIXME: ? return ( <> Developer resources { if (key === 'community') { setDialogIsOpen(true) } }} > API explorer GitHub repository Documentation Community setDialogIsOpen(false)}> {dialogIsOpen && } ) }