"use client"; import { ChevronDown } from "lucide-react"; import { type HTMLAttributes, type ReactNode, useCallback, useMemo, useState, } from "react"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { cn } from "@/lib/utils"; import { useSidebar } from "fumadocs-ui/provider"; export function isActive( url: string, pathname: string, nested = true, root = false ): boolean { const isActive = url === pathname || (nested && pathname.startsWith(root ? url : `${url}/`)); return isActive; } export interface Option { /** * Redirect URL of the folder, usually the index page */ url: string; icon?: ReactNode; title: ReactNode; description?: ReactNode; bgGradient: string; selectedStyle?: string; props?: HTMLAttributes; } export function SubdocsMenu({ options, ...props }: { options: Option[]; } & HTMLAttributes): React.ReactElement { const { closeOnRedirect } = useSidebar(); const pathname = usePathname(); const selected = useMemo(() => { // First, try all non-root options const nonRootOptions = options.filter((item) => item.url !== "/"); const activeNonRootOption = nonRootOptions.find((item) => isActive(item.url, pathname, true) ); if (activeNonRootOption) { return activeNonRootOption; } // If no non-root options are active, try the root options ("/*") return options.find((item) => isActive(item.url, pathname, true, true)); }, [options, pathname]); const onClick = useCallback(() => { closeOnRedirect.current = false; }, [closeOnRedirect]); return (
{options.map((item) => (
{item.icon}
{item.title}
))}
); }