'use client'
import { cva } from 'class-variance-authority'
import { ChevronRight } from 'lucide-react'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import { cn, Collapsible, CollapsibleContent, CollapsibleTrigger } from 'ui'
import type { DropdownMenuItem } from '../Navigation.types'
import { MenuItem } from './GlobalNavigationMenu'
import { GLOBAL_MENU_ITEMS } from './NavigationMenu.constants'
interface DocsSidebarTreeProps {
className?: string
}
interface SidebarGroupProps {
section: DropdownMenuItem
pathname: string
}
interface SidebarLinkProps {
item: DropdownMenuItem
isActive: boolean
}
const sectionItemVariants = cva(
'flex h-8 items-center text-sm font-medium transition-colors focus-ring rounded-md',
{
variants: {
isActive: {
true: 'text-foreground',
false: 'text-foreground-light hover:text-foreground',
},
},
}
)
const isEnabled = (item: DropdownMenuItem) => item.enabled !== false
const isActiveHref = (href: string | undefined, pathname: string) => {
if (!href?.startsWith('/')) return false
const sectionPrefix = href.split('/').slice(0, 3).join('/')
return pathname === sectionPrefix || pathname.startsWith(`${sectionPrefix}/`)
}
export const DocsSidebarTree = ({ className }: DocsSidebarTreeProps) => {
const pathname = usePathname() ?? ''
const sections = GLOBAL_MENU_ITEMS.map(([section]) => section).filter(isEnabled)
return (
)
}
const SidebarGroup = ({ section, pathname }: SidebarGroupProps) => {
const groups = (section.menuItems ?? []).map((group) => group.filter(isEnabled))
const isGroupActive = groups.some((group) =>
group.some((item) => isActiveHref(item.href, pathname))
)
return (
{section.label}
{groups.map((group, groupIdx) =>
group.map((item) => (
- 0 && item === group[0] && 'pt-2')}
>
{item.href ? (
) : (
{item.label}
)}
))
)}
)
}
const SidebarLink = ({ item, isActive }: SidebarLinkProps) => (
)