diff --git a/app/components/FilterOptions.tsx b/app/components/FilterOptions.tsx index c031fe6302..fa0de84417 100644 --- a/app/components/FilterOptions.tsx +++ b/app/components/FilterOptions.tsx @@ -22,7 +22,7 @@ type Props = { selectedPrefix?: string; className?: string; onSelect: (key: string | null | undefined) => void; - searchable?: boolean; + search?: (query: string) => Promise; paginateFetch?: ( options: PaginatedItem ) => Promise; @@ -35,7 +35,7 @@ const FilterOptions = ({ selectedPrefix = "", className, onSelect, - searchable, + search, paginateFetch, }: Props) => { const menu = useMenuState({ @@ -62,14 +62,22 @@ const FilterOptions = ({ // Simple case-insensitive filter to // check if text appears in any author's name. const handleFilter = React.useCallback( - (event) => { + async (event) => { const { value } = event.target; if (value) { - const search = options.filter((option) => + const res = options.filter((option) => option.label.toLowerCase().includes(value.toLowerCase()) ); - setFilteredOptions(search); + if (search) { + const more = await search(value); + const missing = more.filter( + (item) => !res.map((r) => r.key).includes(item.key) + ); + setFilteredOptions([...missing, ...res]); + } else { + setFilteredOptions(res); + } } else { // Clears filter options cache. // This part fires off when search term is "". @@ -79,7 +87,7 @@ const FilterOptions = ({ clearFilter(); } }, - [clearFilter, options] + [clearFilter, options, search] ); return ( @@ -92,8 +100,8 @@ const FilterOptions = ({ )} - {searchable && } - {searchable &&
} + {search && } + {search &&
} { + const res = await users.find(query); + return res.map((user) => ({ + key: user.id, + id: user.id, + label: user.name, + })); + }, + [users] + ); return ( ); diff --git a/app/stores/UsersStore.ts b/app/stores/UsersStore.ts index 90b4135919..e01556cff3 100644 --- a/app/stores/UsersStore.ts +++ b/app/stores/UsersStore.ts @@ -157,6 +157,14 @@ export default class UsersStore extends BaseStore { return res.data; }; + @action + async find(name: string): Promise { + const res = await client.post(`/users.list`, { + query: name, + }); + return res.data; + } + @action async delete(user: User, options: Record = {}) { super.delete(user, options);