"use client"; import { usePathname, useRouter } from "next/navigation"; import { useState, useEffect } from "react"; import { ROUTES, EachRoute } from "@/lib/routes-config"; import { cn } from "@/lib/utils"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Button } from "@/components/ui/button"; import * as LucideIcons from "lucide-react"; import { ChevronsUpDown, Check, type LucideIcon } from "lucide-react"; interface ContextPopoverProps { className?: string; } // Get all root-level routes with context function getContextRoutes(): EachRoute[] { return ROUTES.filter(route => route.context); } // Get the first item's href from a route function getFirstItemHref(route: EachRoute): string { return route.items?.[0]?.href ? `${route.href}${route.items[0].href}` : route.href; } // Get the active context route from the current path function getActiveContextRoute(path: string): EachRoute | undefined { if (!path.startsWith('/docs')) return undefined; const docPath = path.replace(/^\/docs/, ''); return getContextRoutes().find(route => docPath.startsWith(route.href)); } // Get icon component by name function getIcon(name: string) { const Icon = LucideIcons[name as keyof typeof LucideIcons] as LucideIcon | undefined; if (!Icon) return ; return ; } export default function ContextPopover({ className }: ContextPopoverProps) { const pathname = usePathname(); const router = useRouter(); const [activeRoute, setActiveRoute] = useState(); const contextRoutes = getContextRoutes(); useEffect(() => { if (pathname.startsWith("/docs")) { setActiveRoute(getActiveContextRoute(pathname)); } else { setActiveRoute(undefined); } }, [pathname]); if (!pathname.startsWith("/docs") || contextRoutes.length === 0) { return null; } return (
{contextRoutes.map((route) => { const isActive = activeRoute?.href === route.href; const firstItemPath = getFirstItemHref(route); const contextPath = `/docs${firstItemPath}`; return ( ); })}
); }