fix: search autocomplete
This commit is contained in:
1 parent
91f8b4fc1e
commit
a747461505
6 files changed
+43
-14
No files matched your search
@@ -17,6 +17,7 @@ export const showSearchModal = create(({ onResolve }) => {
|
||||
const [title, setTitle] = useState('');
|
||||
const scrollRef = useRef<HTMLDivElement>(null) as MutableRefObject<HTMLDivElement>;
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const isFetchedRef = useRef(false);
|
||||
|
||||
const query = useDistinctUntilChanged(title);
|
||||
|
||||
@@ -35,7 +36,13 @@ export const showSearchModal = create(({ onResolve }) => {
|
||||
{ title: 'Series', data: series, type: 'tv' as const }
|
||||
].filter(({ data: { shows } }) => shows.length);
|
||||
|
||||
const isFetched = movies.isFetched && series.isFetched;
|
||||
if (!isFetchedRef.current && movies.isFetched && series.isFetched) {
|
||||
isFetchedRef.current = true;
|
||||
}
|
||||
|
||||
if (!query) {
|
||||
isFetchedRef.current = false;
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal className='block max-w-[932px] p-0' onClose={onResolve} scrollRef={scrollRef}>
|
||||
@@ -59,7 +66,7 @@ export const showSearchModal = create(({ onResolve }) => {
|
||||
<XCircle />
|
||||
</Button>
|
||||
</div>
|
||||
{isFetched && (
|
||||
{isFetchedRef.current && (
|
||||
<div className='px-2'>
|
||||
{tabs.length > 1 ? (
|
||||
<Tabs defaultValue='movie'>
|
||||
|
||||
@@ -1,11 +1,29 @@
|
||||
import { useRef } from 'react';
|
||||
|
||||
import { search } from '@/api';
|
||||
import useInfiniteList from '@/hooks/useInfiniteList';
|
||||
|
||||
const useSearch = ({ query, type }: { query: string; type: Show['type'] }) =>
|
||||
useInfiniteList({
|
||||
const useSearch = ({ query, type }: { query: string; type: Show['type'] }) => {
|
||||
const previous = useRef<Show[]>([]);
|
||||
|
||||
const infiniteQuery = useInfiniteList({
|
||||
queryKey: ['search', query, type],
|
||||
queryFn: ({ page }) => search({ query, page, type }),
|
||||
enabled: Boolean(query)
|
||||
});
|
||||
|
||||
if (infiniteQuery.isFetched) {
|
||||
previous.current = infiniteQuery.shows;
|
||||
}
|
||||
|
||||
if (!query) {
|
||||
previous.current = [];
|
||||
}
|
||||
|
||||
return {
|
||||
...infiniteQuery,
|
||||
shows: infiniteQuery.isLoading ? previous.current : infiniteQuery.shows
|
||||
};
|
||||
};
|
||||
|
||||
export default useSearch;
|
||||
Reference in new issue
Block a user