feat(show-type): global movie/series toggle, type-aware pages and search, ?type=tv in URL
This commit is contained in:
1 parent
5b41dbca10
commit
0824090c6d
21 files changed
+415
-174
No files matched your search
@@ -0,0 +1,9 @@
|
||||
'use client';
|
||||
|
||||
import { FC } from 'react';
|
||||
|
||||
import ShowTypeToggle from '@/components/ShowTypeToggle';
|
||||
|
||||
const Brand: FC = () => <ShowTypeToggle />;
|
||||
|
||||
export default Brand;
|
||||
@@ -1,10 +1,12 @@
|
||||
'use client';
|
||||
|
||||
import { DropdownMenuItem } from '@radix-ui/react-dropdown-menu';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { Menu } from 'lucide-react';
|
||||
import Link from 'next/link';
|
||||
import { usePathname } from 'next/navigation';
|
||||
|
||||
import { showTypeAtom } from '@/components/ShowTypeToggle';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
||||
import Nav from '@/components/ui/nav';
|
||||
@@ -12,8 +14,14 @@ import { cn } from '@/lib/utils';
|
||||
|
||||
import { paths } from './constants';
|
||||
|
||||
const TYPE_AWARE_PATHS = ['/', '/popular', '/top', '/favorites', '/watchlist'];
|
||||
|
||||
const withShowType = (path: string, type: Show['type']) =>
|
||||
type === 'tv' && TYPE_AWARE_PATHS.includes(path) ? `${path}?type=tv` : path;
|
||||
|
||||
export const MobileNavigation = () => {
|
||||
const currentPath = usePathname();
|
||||
const showType = useAtomValue(showTypeAtom);
|
||||
|
||||
return (
|
||||
<div className='flex md:hidden'>
|
||||
@@ -26,7 +34,7 @@ export const MobileNavigation = () => {
|
||||
<DropdownMenuContent className='flex w-44 flex-col gap-2 rounded-none rounded-bl-lg border-none p-2'>
|
||||
{paths.map(({ path, title }) => (
|
||||
<DropdownMenuItem key={path} asChild>
|
||||
<Link href={path} prefetch={path !== '/'}>
|
||||
<Link href={withShowType(path, showType)} prefetch={path !== '/'}>
|
||||
<div>
|
||||
<Button className='relative w-full' variant={currentPath === path ? 'default' : 'outline'}>
|
||||
{title}
|
||||
@@ -43,12 +51,13 @@ export const MobileNavigation = () => {
|
||||
|
||||
export const DesktopNavigation = () => {
|
||||
const currentPath = usePathname();
|
||||
const showType = useAtomValue(showTypeAtom);
|
||||
const active = paths.find(({ path }) => path === currentPath);
|
||||
|
||||
return (
|
||||
<Nav tabs={paths} active={active}>
|
||||
{({ path, title }) => (
|
||||
<Link href={path} prefetch={path !== '/'}>
|
||||
<Link href={withShowType(path, showType)} prefetch={path !== '/'}>
|
||||
<div>{title}</div>
|
||||
</Link>
|
||||
)}
|
||||
|
||||
@@ -2,14 +2,15 @@
|
||||
|
||||
import { ChangeEvent, MutableRefObject, useRef, useState } from 'react';
|
||||
import { create } from 'react-modal-promise';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { Search as SearchIcon, X } from 'lucide-react';
|
||||
|
||||
import List from '@/components/List';
|
||||
import { showTypeAtom } from '@/components/ShowTypeToggle';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import ButtonsGroup from '@/components/ui/buttons-group';
|
||||
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 useSearch from './useSearch';
|
||||
@@ -19,13 +20,13 @@ export const showSearchModal = create(({ onResolve }) => {
|
||||
const scrollRef = useRef<HTMLDivElement>(null) as MutableRefObject<HTMLDivElement>;
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const isFetchedRef = useRef(false);
|
||||
const showType = useAtomValue(showTypeAtom);
|
||||
|
||||
const query = useDistinctUntilChanged(title);
|
||||
const settledTitle = useDistinctUntilChanged(title, 1000);
|
||||
const settled = title === settledTitle;
|
||||
|
||||
const movies = useSearch({ query, type: 'movie', settled });
|
||||
const series = useSearch({ query, type: 'tv', settled });
|
||||
const search = useSearch({ query, type: showType, settled });
|
||||
|
||||
const handleChangeTitle = ({ target: { value } }: ChangeEvent<HTMLInputElement>) => setTitle(value);
|
||||
|
||||
@@ -34,12 +35,7 @@ export const showSearchModal = create(({ onResolve }) => {
|
||||
inputRef.current?.focus();
|
||||
};
|
||||
|
||||
const tabs = [
|
||||
{ title: 'Movies', data: movies, type: 'movie' as const },
|
||||
{ title: 'Series', data: series, type: 'tv' as const }
|
||||
].filter(({ data: { shows } }) => shows.length);
|
||||
|
||||
if (!isFetchedRef.current && movies.isFetched && series.isFetched) {
|
||||
if (!isFetchedRef.current && search.isFetched) {
|
||||
isFetchedRef.current = true;
|
||||
}
|
||||
|
||||
@@ -69,39 +65,14 @@ export const showSearchModal = create(({ onResolve }) => {
|
||||
</Button>
|
||||
</ButtonsGroup>
|
||||
</div>
|
||||
{isFetchedRef.current && (
|
||||
{isFetchedRef.current && search.shows.length > 0 && (
|
||||
<div className='px-2'>
|
||||
{tabs.length > 1 ? (
|
||||
<Tabs defaultValue='movie'>
|
||||
<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}
|
||||
/>
|
||||
))
|
||||
)}
|
||||
<List
|
||||
shows={search.shows}
|
||||
customScrollParent={scrollRef.current}
|
||||
fetchNextPage={search.fetchNextPage}
|
||||
onPreviewClose={onResolve}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import { FC } from 'react';
|
||||
import Link from 'next/link';
|
||||
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { TITLE } from '@/env';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
import Brand from './components/Brand';
|
||||
import { DesktopNavigation, MobileNavigation } from './components/Navigation';
|
||||
import ToggleSearch from './components/Search';
|
||||
import ToggleTheme from './components/ThemeToggle';
|
||||
@@ -23,9 +21,7 @@ const Header: FC<Props> = ({ className }) => (
|
||||
>
|
||||
<div className='container'>
|
||||
<div className='flex items-center justify-between gap-2 xs:flex-nowrap'>
|
||||
<Link shallow href='/'>
|
||||
<Badge className='cursor-pointer whitespace-nowrap text-[15px] font-normal leading-[18px]'>{TITLE}</Badge>
|
||||
</Link>
|
||||
<Brand />
|
||||
<DesktopNavigation />
|
||||
<div className='flex items-center gap-2'>
|
||||
<ToggleSearch />
|
||||
|
||||
Reference in new issue
Block a user