fix: search autocomplete
This commit is contained in:
1 parent
91f8b4fc1e
commit
a747461505
6 files changed
+43
-14
No files matched your search
@@ -22,5 +22,3 @@ export default async function Starring({ searchParams }: { searchParams: Record<
|
|||||||
</HydrationBoundary>
|
</HydrationBoundary>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const revalidate = 3600;
|
|
||||||
@@ -7,9 +7,7 @@ const useStarring = ({ actorId }: { actorId?: string }) =>
|
|||||||
useInfiniteList({
|
useInfiniteList({
|
||||||
queryKey: ['starring', actorId],
|
queryKey: ['starring', actorId],
|
||||||
enabled: Boolean(actorId),
|
enabled: Boolean(actorId),
|
||||||
queryFn: ({ page }) => {
|
queryFn: ({ page }) => topMovies({ page, starring: actorId })
|
||||||
return topMovies({ page, starring: actorId });
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default useStarring;
|
export default useStarring;
|
||||||
+1
-3
@@ -32,9 +32,7 @@ const RootLayout = ({ children }: { children: ReactNode }) => {
|
|||||||
</head>
|
</head>
|
||||||
<body style={font.style}>
|
<body style={font.style}>
|
||||||
<Providers theme={theme}>
|
<Providers theme={theme}>
|
||||||
<div className='sticky top-0 z-20'>
|
<Header className='sticky top-0 z-20' />
|
||||||
<Header />
|
|
||||||
</div>
|
|
||||||
<main className='container'>{children}</main>
|
<main className='container'>{children}</main>
|
||||||
</Providers>
|
</Providers>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ export const showSearchModal = create(({ onResolve }) => {
|
|||||||
const [title, setTitle] = useState('');
|
const [title, setTitle] = useState('');
|
||||||
const scrollRef = useRef<HTMLDivElement>(null) as MutableRefObject<HTMLDivElement>;
|
const scrollRef = useRef<HTMLDivElement>(null) as MutableRefObject<HTMLDivElement>;
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
const isFetchedRef = useRef(false);
|
||||||
|
|
||||||
const query = useDistinctUntilChanged(title);
|
const query = useDistinctUntilChanged(title);
|
||||||
|
|
||||||
@@ -35,7 +36,13 @@ export const showSearchModal = create(({ onResolve }) => {
|
|||||||
{ title: 'Series', data: series, type: 'tv' as const }
|
{ title: 'Series', data: series, type: 'tv' as const }
|
||||||
].filter(({ data: { shows } }) => shows.length);
|
].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 (
|
return (
|
||||||
<Modal className='block max-w-[932px] p-0' onClose={onResolve} scrollRef={scrollRef}>
|
<Modal className='block max-w-[932px] p-0' onClose={onResolve} scrollRef={scrollRef}>
|
||||||
@@ -59,7 +66,7 @@ export const showSearchModal = create(({ onResolve }) => {
|
|||||||
<XCircle />
|
<XCircle />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
{isFetched && (
|
{isFetchedRef.current && (
|
||||||
<div className='px-2'>
|
<div className='px-2'>
|
||||||
{tabs.length > 1 ? (
|
{tabs.length > 1 ? (
|
||||||
<Tabs defaultValue='movie'>
|
<Tabs defaultValue='movie'>
|
||||||
|
|||||||
@@ -1,11 +1,29 @@
|
|||||||
|
import { useRef } from 'react';
|
||||||
|
|
||||||
import { search } from '@/api';
|
import { search } from '@/api';
|
||||||
import useInfiniteList from '@/hooks/useInfiniteList';
|
import useInfiniteList from '@/hooks/useInfiniteList';
|
||||||
|
|
||||||
const useSearch = ({ query, type }: { query: string; type: Show['type'] }) =>
|
const useSearch = ({ query, type }: { query: string; type: Show['type'] }) => {
|
||||||
useInfiniteList({
|
const previous = useRef<Show[]>([]);
|
||||||
|
|
||||||
|
const infiniteQuery = useInfiniteList({
|
||||||
queryKey: ['search', query, type],
|
queryKey: ['search', query, type],
|
||||||
queryFn: ({ page }) => search({ query, page, type }),
|
queryFn: ({ page }) => search({ query, page, type }),
|
||||||
enabled: Boolean(query)
|
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;
|
export default useSearch;
|
||||||
@@ -2,13 +2,23 @@ import { FC } from 'react';
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
|
|
||||||
import { Badge } from '@/components/ui/badge';
|
import { Badge } from '@/components/ui/badge';
|
||||||
|
import { cn } from '@/lib/utils';
|
||||||
|
|
||||||
import { DesktopNavigation, MobileNavigation } from './components/Navigation';
|
import { DesktopNavigation, MobileNavigation } from './components/Navigation';
|
||||||
import ToggleSearch from './components/Search';
|
import ToggleSearch from './components/Search';
|
||||||
import ToggleTheme from './components/ThemeToggle';
|
import ToggleTheme from './components/ThemeToggle';
|
||||||
|
|
||||||
const Header: FC = () => (
|
interface Props {
|
||||||
<header className='flex justify-center bg-background/95 py-2 backdrop-blur supports-[backdrop-filter]:bg-background/60'>
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Header: FC<Props> = ({ className }) => (
|
||||||
|
<header
|
||||||
|
className={cn(
|
||||||
|
'flex justify-center bg-background/95 py-2 backdrop-blur supports-[backdrop-filter]:bg-background/60',
|
||||||
|
className
|
||||||
|
)}
|
||||||
|
>
|
||||||
<div className='container'>
|
<div className='container'>
|
||||||
<div className='flex items-center justify-between gap-2 xs:flex-nowrap'>
|
<div className='flex items-center justify-between gap-2 xs:flex-nowrap'>
|
||||||
<Link shallow href='/'>
|
<Link shallow href='/'>
|
||||||
|
|||||||
Reference in new issue
Block a user