import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks"; import { Check, ChevronDown } from "lucide-preact"; import { cn } from "@/lib/utils"; type ModelComboboxProps = { options: string[]; value: string; onChange: (value: string) => void; disabled?: boolean; className?: string; id?: string; label?: string; inlineOptions?: boolean; }; export function ModelCombobox({ options, value, onChange, disabled = false, className, id, label = "Model", inlineOptions = false }: ModelComboboxProps) { const generatedId = useId(); const fieldId = id || generatedId; const [open, setOpen] = useState(false); const [draftValue, setDraftValue] = useState(value); const [activeIndex, setActiveIndex] = useState(-1); const rootRef = useRef(null); const inputRef = useRef(null); const normalizedDraftValue = draftValue.trim(); const choices = useMemo(() => { const needle = normalizedDraftValue.toLowerCase(); const listed = options.filter(option => !needle || option.toLowerCase().includes(needle)).map(option => ({ value: option, label: option })); return normalizedDraftValue && !options.includes(normalizedDraftValue) ? [{ value: normalizedDraftValue, label: `Use "${normalizedDraftValue}"` }, ...listed] : listed; }, [normalizedDraftValue, options]); useLayoutEffect(() => { if (!open) setDraftValue(value); }, [open, value]); useLayoutEffect(() => { if (open) inputRef.current?.focus(); }, [open]); useEffect(() => { if (activeIndex >= 0) document.getElementById(`${fieldId}-option-${activeIndex}`)?.scrollIntoView({ block: "nearest" }); }, [activeIndex, fieldId]); const commit = (next = normalizedDraftValue) => { if (next !== value) onChange(next); setDraftValue(next); setActiveIndex(-1); setOpen(false); }; useEffect(() => { if (!open) return; const handlePointerDown = (event: PointerEvent) => { if (!rootRef.current?.contains(event.target as Node)) commit(); }; window.addEventListener("pointerdown", handlePointerDown); return () => window.removeEventListener("pointerdown", handlePointerDown); }, [open, normalizedDraftValue, value, onChange]); return
{ if (open && !rootRef.current?.contains(event.relatedTarget as Node | null)) commit(); }} onKeyDown={(event) => { if (event.key === "Escape" && open) { event.preventDefault(); event.stopPropagation(); setOpen(false); setDraftValue(value); inputRef.current?.focus(); } }}>
= 0 && choices[activeIndex] ? `${fieldId}-option-${activeIndex}` : undefined} value={draftValue} onFocus={() => { if (!disabled) { setDraftValue(value); setActiveIndex(-1); setOpen(true); } }} onInput={(event) => { setDraftValue(event.currentTarget.value); setActiveIndex(-1); setOpen(true); }} onKeyDown={(event) => { if (event.key === "ArrowDown" || event.key === "ArrowUp") { event.preventDefault(); setOpen(true); setActiveIndex(current => { if (!choices.length) return -1; if (current < 0) return event.key === "ArrowDown" ? 0 : choices.length - 1; return (current + (event.key === "ArrowDown" ? 1 : -1) + choices.length) % choices.length; }); } else if (event.key === "Enter") { event.preventDefault(); commit(open && choices[activeIndex] ? choices[activeIndex].value : normalizedDraftValue); } }} className="h-full min-w-0 flex-1 bg-transparent outline-none placeholder:text-muted-foreground" placeholder="Select or type model" disabled={disabled} />
{open ?
{choices.map((option, index) => )} {!choices.length ?

No models found

: null}
: null}
; }