import { ContextSubmenuItemWithProvider } from "core"; import { useEffect, useRef, useState } from "react"; import { Combobox, ComboboxButton, ComboboxInput, ComboboxOption, ComboboxOptions, } from "../../components/ui/Combobox"; import { useSubmenuContextProviders } from "../../context/SubmenuContextProviders"; import { useAppSelector } from "../../redux/hooks"; import FileIcon from "../FileIcon"; export interface AddFileComboboxProps { onSelect: (filepaths: string[]) => void | Promise; onEscape: () => void | Promise; } export default function AddFileCombobox({ onSelect, onEscape, }: AddFileComboboxProps) { const [query, setQuery] = useState(""); const [selectedFiles, setSelectedFiles] = useState< ContextSubmenuItemWithProvider[] >([]); const inputRef = useRef(null); const buttonRef = useRef(null); const { getSubmenuContextItems } = useSubmenuContextProviders(); const allFiles = getSubmenuContextItems("file", ""); const codeToEdit = useAppSelector((state) => state.session.codeToEdit); const remainingFiles = allFiles.filter( (file) => !codeToEdit.find((code) => code.filepath === file.id), ); useEffect(() => { if (inputRef.current) { inputRef.current.focus(); } }, []); const filteredFiles = query === "" ? remainingFiles : remainingFiles.filter((file) => file.title.toLowerCase().includes(query.toLowerCase()), ); return (
{ setSelectedFiles(files); void onSelect(files.map((file) => file.id)); buttonRef.current?.click(); }} > {({ open }) => (
{ if (!open) { buttonRef.current?.click(); } }} onFocus={() => { buttonRef.current?.click(); }} className="bg-vsc-background border-lightgray text-vsc-foreground box-border w-full rounded border border-solid py-0.5 pl-2 focus:outline-none" onChange={(event) => setQuery(event.target.value)} placeholder="Type to search files..." onKeyDown={(e) => { if (e.key === "Escape" && !open) { void onEscape(); } }} /> {filteredFiles.length > 0 ? ( filteredFiles.map((file) => ( `relative flex w-full cursor-pointer px-2 py-1 text-left text-xs ${ active ? "bg-list-active text-list-active-foreground" : "" }` } > {({ active, selected }) => (
{file.title}
{file.description}
)}
)) ) : (
No results
)}
)}
); }