import { NavSectionCaret, NavSectionContent, NavSectionList, } from '~/components/Navigation/NavSection' import { useTheme } from 'next-themes' import Image from 'next/legacy/image' import Link from 'next/link' import { usePathname } from 'next/navigation' import { Accordion } from 'radix-ui' import React, { useEffect, useRef } from 'react' import MenuIconPicker from './MenuIconPicker' type NavAccordionItem = { url?: string enabled?: boolean items?: NavAccordionItem[] } function hasActiveDescendant(item: NavAccordionItem, pathname: string): boolean { if (item.enabled === false) return false if (item.url === pathname) return true return item.items?.some((child) => hasActiveDescendant(child, pathname)) ?? false } function isRenderable(item: NavAccordionItem): boolean { if (item.enabled === false) return false if (item.url) return true return item.items?.some(isRenderable) ?? false } const HeaderLink = React.memo(function HeaderLink(props: { title: string id: string url: string }) { const pathname = usePathname() return ( {props.title ?? props.id} ) }) const ContentAccordionLink = React.memo(function ContentAccordionLink(props: any) { const pathname = usePathname() const { resolvedTheme } = useTheme() const childItems = props.subItem.items ?? [] const activeItem = props.subItem.url === pathname && !childItems.some((child: NavAccordionItem) => child.url === pathname) const activeItemRef = useRef(null) const enabledChildren = childItems.filter(isRenderable) const hasChildren = enabledChildren.length > 0 const isChildActive = enabledChildren.some((child: NavAccordionItem) => hasActiveDescendant(child, pathname) ) const accordionValue = props.subItem.url || props.subItem.name const LinkContainer = (props) => { const isExternal = props.url.startsWith('https://') return ( {props.children} ) } useEffect(() => { // scroll to active item if (activeItem && activeItemRef.current) { // this is a hack, but seems a common one on Stackoverflow setTimeout(() => { activeItemRef.current?.scrollIntoView({ behavior: 'smooth', block: 'nearest' }) }, 0) } }) if (!hasChildren && !props.subItem.url) return null return (
  • {hasChildren ? (
    {props.subItem.icon && ( {props.subItem.name} )} {props.subItem.name}
    {enabledChildren.map((child) => { if (child.items && child.items.length > 0) { return } return (
  • {child.url === pathname && ( )} {child.name}
  • ) })} ) : (
    {props.subItem.icon && ( {props.subItem.name} )} {props.subItem.name}
    )} ) }) const ContentLink = React.memo(function ContentLink(props: any) { const pathname = usePathname() return (
  • {props.icon && ( {props.icon} )} {props.name}
  • ) }) const Content = (props) => { const { menu, id } = props if (menu.enabled === false) { return null } return (
    ) } export default React.memo(Content)