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