import { useCallback, useRef, useState } from 'react' import { Codicon } from '@/components/ui/codicon' import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command' import { controlVariants } from '@/components/ui/control' import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover' import { useI18n } from '@/i18n' import { cn } from '@/lib/utils' /** * cmdk filter score for one option. Case-insensitive substring match, with * the final path segment (after the last "/") ranked above matches anywhere * else so "york" ranks "America/New_York" over "America/New_York/Special". * Exported for tests. */ export function rankSearchOption(option: string, search: string): number { const lower = search.toLowerCase() const itemLower = option.toLowerCase() const slash = itemLower.lastIndexOf('/') if (slash !== -1 && itemLower.slice(slash + 1).includes(lower)) { return 2 } if (itemLower.includes(lower)) { return 1 } return 0 } /** * Searchable select for large option lists (e.g. ~590 IANA timezones). * Built on Popover + cmdk Command — the same stack as Shadcn's Combobox. * * The trigger renders like the existing closed ` pattern of EMPTY_SELECT_VALUE + "(none)". */ clearLabel?: string }) { const { t } = useI18n() const searchPlaceholder = placeholder ?? t.settings.runtime.search const [open, setOpen] = useState(false) const triggerRef = useRef(null) const handleSelect = useCallback( (selected: string) => { onChange(selected) setOpen(false) }, [onChange] ) const displayValue = value !== '' && value !== undefined ? value : searchPlaceholder return ( {emptyMessage ?? t.settings.runtime.noResults} {clearLabel && ( handleSelect('')} value={clearLabel}> {clearLabel} )} {options.map(option => ( handleSelect(option)} value={option}> {option} ))} ) }