import { Button } from '@/components/ui/button'; import { Command, CommandGroup, CommandInput, CommandItem, CommandList, } from '@/components/ui/command'; import { HoverCard, HoverCardContent, HoverCardTrigger, } from '@/components/ui/hover-card'; import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover'; import { Separator } from '@/components/ui/separator'; import { cn } from '@/lib/utils'; import { get } from 'lodash'; import { ChevronDownIcon, XIcon } from 'lucide-react'; import * as React from 'react'; import { useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { VariableType } from '../../constant'; import { useFilterStructuredOutputByValue, useFindAgentStructuredOutputLabel, useShowSecondaryMenu, } from '../../hooks/use-build-structured-output'; import { StructuredOutputSecondaryMenu } from './structured-output-secondary-menu'; type Item = { label: string; value: string; }; type Option = { label: string; value: string; parentLabel?: string; children?: Item[]; }; type Group = { label: string | React.ReactNode; options: Option[]; }; interface GroupedSelectWithSecondaryMenuProps { options: Group[]; value?: string; onChange?: (value: string) => void; placeholder?: string; type?: VariableType; } export function GroupedSelectWithSecondaryMenu({ options, value, onChange, placeholder, type, }: GroupedSelectWithSecondaryMenuProps) { const { t } = useTranslation(); const [open, setOpen] = React.useState(false); const showSecondaryMenu = useShowSecondaryMenu(); const filterStructuredOutput = useFilterStructuredOutputByValue(); const findAgentStructuredOutputLabel = useFindAgentStructuredOutputLabel(); // Find the label of the selected item const flattenedOptions = options.flatMap((g) => g.options); let selectedItem = flattenedOptions .flatMap((o) => [o, ...(o.children || [])]) .find((o) => o.value === value); if (!selectedItem && value) { selectedItem = findAgentStructuredOutputLabel(value, flattenedOptions); } // Handle clear click const handleClear = (e: React.MouseEvent) => { e.stopPropagation(); onChange?.(''); setOpen(false); }; const handleSecondaryMenuClick = useCallback( (record: Item) => { onChange?.(record.value); setOpen(false); }, [onChange], ); return ( {options.map((group, idx) => ( {group.options.map((option) => { const shouldShowSecondary = showSecondaryMenu( option.value, option.label, ); if (shouldShowSecondary) { const filteredStructuredOutput = filterStructuredOutput( option.value, ); return ( ); } return option.children ? ( {}} className="flex items-center justify-between cursor-default" > {option.label} {option.children.map((child) => (
{ onChange?.(child.value); setOpen(false); }} > {child.label}
))}
) : ( { onChange?.(option.value); setOpen(false); }} > {option.label} ); })}
))}
); }