'use client'; import { useMemo, type ReactNode } from 'react'; import { usePathname } from 'next/navigation'; import { SwitcherDropdown } from '@/components/docs/switcherDropdown'; import { cn } from '@/lib/cn'; export type CategorySwitcherOption = { icon?: ReactNode; title: ReactNode; url: string; urls?: Set; }; type CategorySwitcherProps = { options: CategorySwitcherOption[]; className?: string; }; const getCategoryKey = (path: string): string => { const segments = path.split('/').filter(Boolean); return segments[1] ?? ''; }; export function CategorySwitcher({ options, className }: CategorySwitcherProps) { const pathname = usePathname(); const selected = useMemo(() => { const lookup = pathname.endsWith('/') ? pathname.slice(0, -1) : pathname; const currentCategory = getCategoryKey(lookup); return ( options.find((item) => item.urls?.has(lookup)) ?? options.find((item) => getCategoryKey(item.url) === currentCategory) ?? options[0] ); }, [options, pathname]); return ( ({ key: item.url, label: item.title, icon: item.icon, href: item.url, active: item === selected }))} /> ); }