import React, { useState } from 'react'; import { Root, Trigger, Content, Portal } from '@radix-ui/react-popover'; import MenuItem from '~/components/Chat/Menus/UI/MenuItem'; import type { Option } from '~/common'; import { useLocalize } from '~/hooks'; import { cn } from '~/utils/'; import { useMultiSearch } from './MultiSearch'; type SelectDropDownProps = { id?: string; title?: string; value: string | null | Option; disabled?: boolean; setValue: (value: string) => void; availableValues: string[] | Option[]; emptyTitle?: boolean; showAbove?: boolean; showLabel?: boolean; iconSide?: 'left' | 'right'; renderOption?: () => React.ReactNode; footer?: React.ReactNode; }; function SelectDropDownPop({ title: _title, value, availableValues, setValue, showAbove = false, showLabel = true, emptyTitle = false, footer, }: SelectDropDownProps) { const localize = useLocalize(); const [open, setOpen] = useState(false); const transitionProps = { className: 'top-full mt-3' }; if (showAbove) { transitionProps.className = 'bottom-full mb-3'; } let title = _title; if (emptyTitle) { title = ''; } else if (!title) { title = localize('com_ui_model'); } // Detemine if we should to convert this component into a searchable select. If we have enough elements, a search // input will appear near the top of the menu, allowing correct filtering of different model menu items. This will // reset once the component is unmounted (as per a normal search) const [filteredValues, searchRender] = useMultiSearch({ availableOptions: availableValues, }); const hasSearchRender = Boolean(searchRender); const options = hasSearchRender ? filteredValues : availableValues; const handleSelect = (selectedValue: string) => { setValue(selectedValue); setOpen(false); }; return (
{searchRender} {options.map((option) => { if (typeof option === 'string') { return ( handleSelect(option)} /> ); } return ( handleSelect(option.value)} /> ); })} {footer}
); } export default SelectDropDownPop;