mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
fix unnecessary autocomplete request
This commit is contained in:
parent
297056151c
commit
4aea8cfe3b
1 changed files with 9 additions and 4 deletions
|
@ -5,7 +5,10 @@ import useSWR from 'swr';
|
||||||
import { apiGetTagSuggestions } from '~/api/tags';
|
import { apiGetTagSuggestions } from '~/api/tags';
|
||||||
import { API } from '~/constants/api';
|
import { API } from '~/constants/api';
|
||||||
|
|
||||||
export const useTagAutocomplete = (input: string, exclude: string[]): string[] => {
|
export const useTagAutocomplete = (
|
||||||
|
input: string,
|
||||||
|
exclude: string[],
|
||||||
|
): string[] => {
|
||||||
const [search, setSearch] = useState('');
|
const [search, setSearch] = useState('');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -14,12 +17,14 @@ export const useTagAutocomplete = (input: string, exclude: string[]): string[] =
|
||||||
}, [input]);
|
}, [input]);
|
||||||
|
|
||||||
const { data } = useSWR(
|
const { data } = useSWR(
|
||||||
`${API.TAG.AUTOCOMPLETE}?tag=${search}&exclude=${exclude.join(',')}`,
|
search
|
||||||
|
? `${API.TAG.AUTOCOMPLETE}?tag=${search}&exclude=${exclude.join(',')}`
|
||||||
|
: null,
|
||||||
async () => {
|
async () => {
|
||||||
const result = await apiGetTagSuggestions({ search, exclude });
|
const result = await apiGetTagSuggestions({ search, exclude });
|
||||||
return result.tags || [];
|
return result.tags || [];
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
return useMemo(() => data || [], [data]);
|
return useMemo(() => (search ? data ?? [] : []), [data, search]);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue