feat(show-type): global movie/series toggle, type-aware pages and search, ?type=tv in URL

This commit is contained in:
ner committed 2026-09-24 15:40:01 +03:00
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>
)}
+12 -41
View File
@@ -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>
+2 -6
View File
@@ -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 />
+110
View File
@@ -0,0 +1,110 @@
'use client';
import { FC, useEffect } from 'react';
import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
import { atom, useAtom } from 'jotai';
import { Check, ChevronDown, Clapperboard, LucideIcon, Tv } from 'lucide-react';
import Link from 'next/link';
import analytics from '@/lib/analytics';
import { parseShowType } from '@/lib/showType';
import { cn } from '@/lib/utils';
export const showTypeAtom = atom<Show['type']>('movie');
interface Option {
value: Show['type'];
label: string;
icon: LucideIcon;
}
const options: Option[] = [
{ value: 'movie', label: 'Movie', icon: Clapperboard },
{ value: 'tv', label: 'Series', icon: Tv }
];
interface Props {
className?: string;
}
const ShowTypeToggle: FC<Props> = ({ className }) => {
const [value, setValue] = useAtom(showTypeAtom);
useEffect(() => {
const urlType = parseShowType(new URLSearchParams(window.location.search).get('type'));
if (urlType !== value) setValue(urlType);
}, [value, setValue]);
const current = options.find(({ value: optionValue }) => optionValue === value) ?? options[0];
const handleChange = (next: Show['type']) => {
analytics.randomTypeChanged({ type: next });
setValue(next);
const url = new URL(window.location.href);
if (next === 'tv') url.searchParams.set('type', 'tv');
else url.searchParams.delete('type');
window.history.replaceState(null, '', url);
};
return (
<DropdownMenuPrimitive.Root>
<DropdownMenuPrimitive.Trigger asChild>
<span
aria-label={`Watch Next: ${current.label}`}
className={cn(
'inline-flex h-6 cursor-pointer items-center whitespace-nowrap rounded-full bg-primary text-primary-foreground transition-opacity hover:opacity-90',
className
)}
>
<Link
href={value === 'tv' ? '/?type=tv' : '/'}
onPointerDown={(event) => event.stopPropagation()}
onClick={(event) => event.stopPropagation()}
className='pl-2.5 pr-1.5 text-[15px] font-normal leading-[18px]'
>
Watch Next
</Link>
<span aria-hidden className='h-4 w-px bg-primary-foreground/40' />
<span className='flex items-center gap-1.5 pl-1.5 pr-2.5 text-[15px] font-normal leading-[18px]'>
<span className='text-primary-foreground/70'>{current.label}</span>
<ChevronDown className='size-3.5 text-primary-foreground/70' />
</span>
</span>
</DropdownMenuPrimitive.Trigger>
<DropdownMenuPrimitive.Portal>
<DropdownMenuPrimitive.Content
align='start'
sideOffset={8}
className={cn(
'z-50 w-60 rounded-2xl border border-border/60 bg-popover p-2 text-popover-foreground shadow-xl',
'data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
'data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=top]:slide-in-from-bottom-2'
)}
>
<DropdownMenuPrimitive.RadioGroup value={value} onValueChange={(next) => handleChange(next as Show['type'])}>
{options.map(({ value: optionValue, label, icon: Icon }) => (
<DropdownMenuPrimitive.RadioItem
key={optionValue}
value={optionValue}
className={cn(
'flex w-full cursor-pointer items-center gap-3 rounded-xl px-3 py-2.5 text-lg outline-none transition-colors',
'focus:bg-secondary data-[state=checked]:bg-secondary'
)}
>
<Icon className='size-5 shrink-0' />
<span>{label}</span>
<DropdownMenuPrimitive.ItemIndicator className='ml-auto'>
<Check className='size-5' />
</DropdownMenuPrimitive.ItemIndicator>
</DropdownMenuPrimitive.RadioItem>
))}
</DropdownMenuPrimitive.RadioGroup>
</DropdownMenuPrimitive.Content>
</DropdownMenuPrimitive.Portal>
</DropdownMenuPrimitive.Root>
);
};
export default ShowTypeToggle;
+23
View File
@@ -20,3 +20,26 @@ export const tabs = [
type: 'recommendations'
}
];
export const seriesTabs = [
{
title: 'Apple',
type: 'apple'
},
{
title: 'Netflix',
type: 'netflix'
},
{
title: 'HBO',
type: 'hbo'
},
{
title: 'Similar',
type: 'similar'
},
{
title: 'Recommendations',
type: 'recommendations'
}
];
+21 -6
View File
@@ -1,13 +1,13 @@
'use client';
import { FC, useState } from 'react';
import { FC, useEffect, useMemo, useState } from 'react';
import useSimilar from '@/components/Trending/useSimilar';
import Nav from '@/components/ui/nav';
import analytics from '@/lib/analytics';
import Gallery from './components/Gallery';
import { tabs as initialTabs } from './constants';
import { seriesTabs, tabs as movieTabs } from './constants';
import useTrending from './useTrending';
interface Props {
@@ -17,13 +17,28 @@ interface Props {
}
const Trending: FC<Props> = ({ showId, showType, preview }) => {
const tabs = preview ? initialTabs.filter(({ type }) => ['similar', 'recommendations'].includes(type)) : initialTabs;
const isSeries = showType === 'tv';
const tabs = useMemo(
() =>
(isSeries ? seriesTabs : movieTabs).filter(({ type }) =>
preview ? ['similar', 'recommendations'].includes(type) : true
),
[isSeries, preview]
);
const [active, setActive] = useState(tabs[0]);
const streaming = useTrending({ type: 'streaming', enabled: !preview });
useEffect(() => {
setActive(tabs[0]);
}, [tabs]);
const streaming = useTrending({ type: 'streaming', enabled: !preview && !isSeries });
const trending = useTrending({ type: 'trending', enabled: active.type === 'trending' });
const theater = useTrending({ type: 'theater', enabled: active.type === 'theater' });
const apple = useTrending({ type: 'apple', enabled: isSeries && !preview });
const netflix = useTrending({ type: 'netflix', enabled: isSeries && active.type === 'netflix' });
const hbo = useTrending({ type: 'hbo', enabled: isSeries && active.type === 'hbo' });
const similar = useSimilar({
showId,
showType,
@@ -37,10 +52,10 @@ const Trending: FC<Props> = ({ showId, showType, preview }) => {
enabled: active.type === 'recommendations'
});
const tab = { trending, streaming, theater, similar, recommendations }[active.type]!;
const tab = { streaming, trending, theater, apple, netflix, hbo, similar, recommendations }[active.type]!;
return (
(streaming.isFetched || similar.isFetched) && (
(streaming.isFetched || apple.isFetched || similar.isFetched) && (
<div className='hidden md:block'>
<Nav
tabs={tabs}
+1 -1
View File
@@ -30,7 +30,7 @@ const Hint: FC<Props> = ({ lines, children, className }) => {
role='tooltip'
aria-hidden={!open}
className={cn(
'pointer-events-none absolute left-1/2 top-full z-10 mt-1.5 -translate-x-1/2 whitespace-nowrap rounded-md border bg-popover px-2 py-1 text-xs text-popover-foreground shadow-md transition-opacity',
'pointer-events-none absolute left-0 top-full z-10 mt-1.5 whitespace-nowrap rounded-md border bg-popover px-2 py-1 text-xs text-popover-foreground shadow-md transition-opacity md:left-1/2 md:-translate-x-1/2',
open ? 'opacity-100' : 'opacity-0'
)}
>