'use client' import { type ReactNode, useEffect, useState } from 'react' import type { PageTree } from 'fumadocs-core/server' import { ChevronRight } from 'lucide-react' import Link from 'next/link' import { usePathname } from 'next/navigation' import { cn } from '@/lib/utils' function isActive(url: string, pathname: string, nested = true): boolean { return url === pathname || (nested && pathname.startsWith(`${url}/`)) } export function SidebarItem({ item }: { item: PageTree.Item }) { const pathname = usePathname() const active = isActive(item.url, pathname, false) return (
  • {item.name}
  • ) } export function SidebarFolder({ item, level, children, }: { item: PageTree.Folder level: number children: ReactNode }) { const pathname = usePathname() const hasActiveChild = checkHasActiveChild(item, pathname) const [open, setOpen] = useState(hasActiveChild) useEffect(() => { setOpen(hasActiveChild) }, [hasActiveChild]) return (
  • {item.index ? (
    {item.name}
    ) : ( )}
  • ) } export function SidebarSeparator({ item }: { item: PageTree.Separator }) { return (

    {item.name}

    ) } function checkHasActiveChild(node: PageTree.Folder, pathname: string): boolean { if (node.index && isActive(node.index.url, pathname)) { return true } for (const child of node.children) { if (child.type === 'page' && isActive(child.url, pathname)) { return true } if (child.type === 'folder' && checkHasActiveChild(child, pathname)) { return true } } return false }