'use client';
import Link from 'next/link';
import { Check, ChevronDown } from 'lucide-react';
import { useState, type ReactNode } from 'react';
import {
Popover,
PopoverContent,
PopoverTrigger
} from 'fumadocs-ui/components/ui/popover';
import { useSidebar } from 'fumadocs-ui/provider';
import { cn } from '@/lib/cn';
export type SwitcherDropdownOption = {
key: string;
label: ReactNode;
icon?: ReactNode;
href?: string;
active?: boolean;
onSelect?: () => void;
};
type SwitcherDropdownProps = {
options: SwitcherDropdownOption[];
className?: string;
triggerClassName?: string;
contentClassName?: string;
optionClassName?: string;
iconClassName?: string;
align?: 'start' | 'center' | 'end';
keepSidebarOpenOnSelect?: boolean;
};
export function SwitcherDropdown({
options,
className,
triggerClassName,
contentClassName,
optionClassName,
iconClassName,
align = 'end',
keepSidebarOpenOnSelect = false
}: SwitcherDropdownProps) {
const [open, setOpen] = useState(false);
const { closeOnRedirect } = useSidebar();
const selected = options.find((item) => item.active) ?? options[0];
const selectItem = (item: SwitcherDropdownOption) => {
if (keepSidebarOpenOnSelect) {
closeOnRedirect.current = false;
}
item.onSelect?.();
setOpen(false);
};
const renderOption = (item: SwitcherDropdownOption) => {
const active = item === selected;
const content = (
<>
{item.icon && (
{item.icon}
)}
{item.label}
>
);
const className = cn(
'flex h-8 w-full cursor-pointer items-center gap-2 rounded-lg px-2.5 text-left text-fd-popover-foreground transition-colors hover:bg-fd-accent',
active && 'font-semibold text-[#3370FF] dark:text-blue-400',
optionClassName
);
if (item.href) {
return (
selectItem(item)}
className={className}
role="option"
aria-selected={active}
>
{content}
);
}
return (
);
};
return (
{selected?.icon && (
{selected.icon}
)}
{selected?.label}
{options.map(renderOption)}
);
}