'use client' import { type ReactNode, useEffect, useState } from 'react' import type { Folder, Item, Separator } from 'fumadocs-core/page-tree' import { ChevronRight } from 'lucide-react' import Link from 'next/link' import { usePathname } from 'next/navigation' import { cn } from '@/lib/utils' const LANG_PREFIXES = ['/en', '/es', '/fr', '/de', '/ja', '/zh'] function stripLangPrefix(path: string): string { for (const prefix of LANG_PREFIXES) { if (path === prefix) return '/' if (path.startsWith(`${prefix}/`)) return path.slice(prefix.length) } return path } function isActive(url: string, pathname: string, nested = true): boolean { const normalizedPathname = stripLangPrefix(pathname) const normalizedUrl = stripLangPrefix(url) return ( normalizedUrl === normalizedPathname || (nested && normalizedPathname.startsWith(`${normalizedUrl}/`)) ) } export function SidebarItem({ item }: { item: Item }) { const pathname = usePathname() const active = isActive(item.url, pathname, false) return ( {item.name} ) } export function SidebarFolder({ item, children }: { item: Folder; children: ReactNode }) { const pathname = usePathname() const hasActiveChild = checkHasActiveChild(item, pathname) const hasChildren = item.children.length > 0 const [open, setOpen] = useState(hasActiveChild) useEffect(() => { setOpen(hasActiveChild) }, [hasActiveChild]) const active = item.index ? isActive(item.index.url, pathname, false) : false if (item.index && !hasChildren) { return ( {item.name} ) } return (
{item.name}
) } function checkHasActiveChild(node: 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 }