import { doesItemMatchSearchQuery } from '@/Utils/Items/Search/doesItemMatchSearchQuery' import { Combobox, ComboboxItem, ComboboxPopover, ComboboxStoreProps, useComboboxStore, VisuallyHidden, } from '@ariakit/react' import { classNames, ContentType, DecryptedItem, naturalSort } from '@standardnotes/snjs' import { observer } from 'mobx-react-lite' import { useDeferredValue, useEffect, useState } from 'react' import { useApplication } from '../ApplicationProvider' import LinkedItemMeta from '../LinkedItems/LinkedItemMeta' type Props = { contentTypes: ContentType[] placeholder: string onSelection: (item: DecryptedItem) => void className?: { input?: string popover?: string } comboboxProps?: ComboboxStoreProps } const ItemSelectionDropdown = ({ contentTypes, placeholder, onSelection, comboboxProps, className = {} }: Props) => { const application = useApplication() const combobox = useComboboxStore(comboboxProps) const value = combobox.useState('value') const open = combobox.useState('open') useEffect(() => { if (value.length < 1 && open) { combobox.setOpen(false) } }, [combobox, open, value.length]) const searchQuery = useDeferredValue(value) const [items, setItems] = useState([]) useEffect(() => { const searchableItems = naturalSort(application.items.getItems(contentTypes), 'title') const filteredItems = searchableItems.filter((item) => { return doesItemMatchSearchQuery(item, searchQuery, application) }) setItems(filteredItems) }, [searchQuery, application, contentTypes]) return (
Select an item {items.length > 0 ? ( items.map((item) => ( { combobox.setValue('') onSelection(item) }} > )) ) : (
No results found
)}
) } export default observer(ItemSelectionDropdown)