"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 ( {activeRoute?.context?.icon && ( {getIcon(activeRoute.context.icon)} )} {activeRoute?.context?.title || activeRoute?.title || 'Select context'} {contextRoutes.map((route) => { const isActive = activeRoute?.href === route.href; const firstItemPath = getFirstItemHref(route); const contextPath = `/docs${firstItemPath}`; return ( router.push(contextPath)} className={cn( "relative flex w-full items-center gap-2 rounded px-2 py-1.5 text-sm", "text-left outline-none transition-colors", isActive ? "bg-primary/20 text-primary dark:bg-accent/20 dark:text-accent" : "text-foreground/80 hover:bg-primary/20 dark:text-foreground/60 dark:hover:bg-accent/20" )} > {route.context?.icon && ( {getIcon(route.context.icon)} )} {route.context?.title || route.title} {route.context?.description && ( {route.context.description} )} {isActive && ( )} ); })} ); }