refactor: clean code and remove unnecessary dependencies
This commit is contained in:
1 parent
eceae6fec7
commit
ebf599eed6
92 files changed
+1152
-2842
No files matched your search
@@ -0,0 +1,6 @@
|
||||
export const paths = [
|
||||
{ path: '/', title: 'Random' },
|
||||
{ path: '/popular', title: 'Popular' },
|
||||
{ path: '/top', title: 'Top rated' },
|
||||
{ path: '/favorites', title: 'Favorites' }
|
||||
];
|
||||
@@ -1,6 +1,5 @@
|
||||
'use client';
|
||||
|
||||
import { FC, useEffect, useRef, useState } from 'react';
|
||||
import { DropdownMenuItem } from '@radix-ui/react-dropdown-menu';
|
||||
import { Menu } from 'lucide-react';
|
||||
import Link from 'next/link';
|
||||
@@ -10,14 +9,9 @@ import { Button } from '@/components/ui/button';
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
||||
import Nav from '@/components/ui/nav';
|
||||
|
||||
const paths = [
|
||||
{ path: '/', title: 'Random' },
|
||||
{ path: '/popular', title: 'Popular' },
|
||||
{ path: '/top', title: 'Top rated' },
|
||||
{ path: '/favorites', title: 'Favorites' }
|
||||
];
|
||||
import { paths } from './constants';
|
||||
|
||||
export function MobileNavigation() {
|
||||
export const MobileNavigation = () => {
|
||||
const currentPath = usePathname();
|
||||
|
||||
return (
|
||||
@@ -44,9 +38,9 @@ export function MobileNavigation() {
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export function DesktopNavigation() {
|
||||
export const DesktopNavigation = () => {
|
||||
const currentPath = usePathname();
|
||||
const active = paths.find(({ path }) => path === currentPath);
|
||||
|
||||
@@ -59,4 +53,4 @@ export function DesktopNavigation() {
|
||||
)}
|
||||
</Nav>
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -1,16 +0,0 @@
|
||||
import client from '@/network';
|
||||
|
||||
const API = {
|
||||
search: {
|
||||
byTitle({ query, page }: { query: string; page: number }): Promise<MovieDBResponse> {
|
||||
return client.get('/search', {
|
||||
params: {
|
||||
query,
|
||||
page
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default API;
|
||||
@@ -4,54 +4,40 @@ import { ChangeEvent, MutableRefObject, useRef, useState } from 'react';
|
||||
import { create } from 'react-modal-promise';
|
||||
import { Search as SearchIcon, XCircle } from 'lucide-react';
|
||||
|
||||
import List from '@/components/Movie/List';
|
||||
import List from '@/components/List';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Modal } from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
import useDistinctUntilChanged from '@/hooks/useDistinctUntilChanged';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
import useSearch from './useSearch';
|
||||
|
||||
export const showSearchModal = create(({ onResolve }) => {
|
||||
const [title, setTitle] = useState('');
|
||||
const [query, setQuery] = useState('');
|
||||
const scrollRef = useRef<HTMLDivElement>(null) as MutableRefObject<HTMLDivElement>;
|
||||
const timeout = useRef<NodeJS.Timeout>();
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const {
|
||||
data: movies,
|
||||
hasNextPage: hasNextMoviesPage,
|
||||
fetchNextPage: fetchNextMoviesPage
|
||||
} = useSearch({ query, type: 'movie' });
|
||||
const query = useDistinctUntilChanged(title);
|
||||
|
||||
const {
|
||||
data: series,
|
||||
hasNextPage: hasNextSeriesPage,
|
||||
fetchNextPage: fetchNextSeriesPage
|
||||
} = useSearch({ query, type: 'tv' });
|
||||
const movies = useSearch({ query, type: 'movie' });
|
||||
const series = useSearch({ query, type: 'tv' });
|
||||
|
||||
const handleChangeTitle = ({ target: { value } }: ChangeEvent<HTMLInputElement>) => {
|
||||
setTitle(value);
|
||||
|
||||
clearTimeout(timeout.current);
|
||||
timeout.current = setTimeout(() => setQuery(value.trim()), 500);
|
||||
};
|
||||
const handleChangeTitle = ({ target: { value } }: ChangeEvent<HTMLInputElement>) => setTitle(value);
|
||||
|
||||
const handleReset = () => {
|
||||
setQuery('');
|
||||
setTitle('');
|
||||
inputRef.current?.focus();
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
handleReset();
|
||||
return onResolve();
|
||||
};
|
||||
const tabs = [
|
||||
{ title: 'Movies', data: movies, type: 'movie' as const },
|
||||
{ title: 'Series', data: series, type: 'tv' as const }
|
||||
].filter(({ data: { shows } }) => shows.length);
|
||||
|
||||
return (
|
||||
<Modal className='block p-0 max-w-[932px]' onClose={handleClose} scrollRef={scrollRef}>
|
||||
<Modal className='block p-0 max-w-[932px]' onClose={onResolve} scrollRef={scrollRef}>
|
||||
<div className='relative p-2'>
|
||||
<Input
|
||||
autoFocus
|
||||
@@ -66,53 +52,51 @@ export const showSearchModal = create(({ onResolve }) => {
|
||||
type='button'
|
||||
size='icon'
|
||||
onClick={handleReset}
|
||||
disabled={!query.length}
|
||||
disabled={!title.length}
|
||||
className='absolute right-2 top-2'
|
||||
>
|
||||
<XCircle />
|
||||
</Button>
|
||||
</div>
|
||||
<Tabs
|
||||
defaultValue='movies'
|
||||
className={cn('w-full px-2', !(movies?.pages.length && series?.pages?.length) && 'hidden')}
|
||||
>
|
||||
<TabsList className='grid w-full grid-cols-2'>
|
||||
<TabsTrigger value='movies'>Movies</TabsTrigger>
|
||||
<TabsTrigger value='series'>Series</TabsTrigger>
|
||||
</TabsList>
|
||||
<TabsContent value='movies'>
|
||||
{Boolean(movies?.pages.length && movies?.pages?.[0]?.results?.length) && (
|
||||
<List
|
||||
customScrollParent={scrollRef.current}
|
||||
pages={movies!.pages as unknown as MovieDBResponse[]}
|
||||
fetchNextPage={fetchNextMoviesPage}
|
||||
hasNextPage={hasNextMoviesPage}
|
||||
onPreviewClose={handleClose}
|
||||
type='movie'
|
||||
/>
|
||||
)}
|
||||
</TabsContent>
|
||||
<TabsContent value='series'>
|
||||
{Boolean(series?.pages.length && series?.pages?.[0]?.results?.length) && (
|
||||
<List
|
||||
customScrollParent={scrollRef.current}
|
||||
pages={series!.pages as unknown as MovieDBResponse[]}
|
||||
fetchNextPage={fetchNextSeriesPage}
|
||||
hasNextPage={hasNextSeriesPage}
|
||||
onPreviewClose={handleClose}
|
||||
type='tv'
|
||||
/>
|
||||
)}
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
{tabs.length > 1 ? (
|
||||
<Tabs defaultValue='movie' className={cn('w-full px-2')}>
|
||||
<TabsList className='grid w-full grid-cols-2'>
|
||||
{tabs.map(({ type, title }) => (
|
||||
<TabsTrigger key={type} value={type}>
|
||||
{title}
|
||||
</TabsTrigger>
|
||||
))}
|
||||
</TabsList>
|
||||
{tabs.map(({ data: { shows, fetchNextPage }, type }) => (
|
||||
<TabsContent value={type} key={type}>
|
||||
<List
|
||||
shows={shows}
|
||||
customScrollParent={scrollRef.current}
|
||||
fetchNextPage={fetchNextPage}
|
||||
onPreviewClose={onResolve}
|
||||
/>
|
||||
</TabsContent>
|
||||
))}
|
||||
</Tabs>
|
||||
) : (
|
||||
tabs.map(({ data: { shows, fetchNextPage }, type }) => (
|
||||
<List
|
||||
key={type}
|
||||
shows={shows}
|
||||
customScrollParent={scrollRef.current}
|
||||
fetchNextPage={fetchNextPage}
|
||||
onPreviewClose={onResolve}
|
||||
/>
|
||||
))
|
||||
)}
|
||||
</Modal>
|
||||
);
|
||||
});
|
||||
|
||||
export default function ToggleSearch() {
|
||||
return (
|
||||
<Button variant='ghost' size='icon' onClick={() => showSearchModal()}>
|
||||
<SearchIcon size={19} />
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
const ToggleSearch = () => (
|
||||
<Button variant='ghost' size='icon' onClick={() => showSearchModal()}>
|
||||
<SearchIcon size={19} />
|
||||
</Button>
|
||||
);
|
||||
|
||||
export default ToggleSearch;
|
||||
@@ -1,20 +1,11 @@
|
||||
import { useInfiniteQuery } from '@tanstack/react-query';
|
||||
import { search } from '@/api';
|
||||
import useInfiniteList from '@/hooks/useInfiniteList';
|
||||
|
||||
import { search } from '@/lib/api';
|
||||
|
||||
const useSearch = ({ query, type }: { query: string; type: ShowType }) => {
|
||||
return useInfiniteQuery({
|
||||
const useSearch = ({ query, type }: { query: string; type: Show['type'] }) =>
|
||||
useInfiniteList({
|
||||
queryKey: ['search', query, type],
|
||||
queryFn: ({ pageParam: page }) => search({ query, page, type }),
|
||||
enabled: Boolean(query),
|
||||
getNextPageParam(lastPage) {
|
||||
const { page, total_pages } = lastPage as { page: number; total_pages: number };
|
||||
if (page === total_pages) return undefined;
|
||||
|
||||
return page + 1;
|
||||
},
|
||||
initialPageParam: 1
|
||||
queryFn: ({ page }) => search({ query, page, type }),
|
||||
enabled: Boolean(query)
|
||||
});
|
||||
};
|
||||
|
||||
export default useSearch;
|
||||
@@ -3,13 +3,16 @@
|
||||
import { Moon, Sun } from 'lucide-react';
|
||||
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { useTheme } from '@/hooks/useTheme';
|
||||
import useTheme from '@/hooks/useTheme';
|
||||
|
||||
export default function ThemeToggle() {
|
||||
const ThemeToggle = () => {
|
||||
const [theme, setTheme] = useTheme();
|
||||
|
||||
return (
|
||||
<Button variant='ghost' size='icon' onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>
|
||||
{theme === 'light' ? <Moon size={19} /> : <Sun size={19} />}
|
||||
</Button>
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default ThemeToggle;
|
||||
@@ -9,14 +9,13 @@ import ToggleTheme from './components/ThemeToggle';
|
||||
|
||||
const Header: FC = () => (
|
||||
<header className='flex justify-center bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 py-2'>
|
||||
<div className='max-w-[1240px] w-full px-2'>
|
||||
<div className='container'>
|
||||
<div className='flex items-center justify-between gap-2 xs:flex-nowrap'>
|
||||
<Link href='/' shallow legacyBehavior>
|
||||
<Link shallow href='/'>
|
||||
<Badge className='whitespace-nowrap cursor-pointer font-normal'>Movie advisor</Badge>
|
||||
</Link>
|
||||
<DesktopNavigation />
|
||||
<div className='flex items-center gap-2'>
|
||||
<div id='actions' className='empty:hidden' />
|
||||
<ToggleTheme />
|
||||
<ToggleSearch />
|
||||
<MobileNavigation />
|
||||
|
||||
Reference in new issue
Block a user