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

@@ -1,9 +1,10 @@
'use client';
import { FC } from 'react';
import { useAtomValue } from 'jotai';
import List from '@/components/List';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { showTypeAtom } from '@/components/ShowTypeToggle';
import useUsersShows from '@/hooks/useUsersShows';
interface Props {
@@ -13,36 +14,18 @@ interface Props {
}
const UsersList: FC<Props> = ({ list, label, session }) => {
const movies = useUsersShows({ list, showType: 'movie', session, mode: 'default' });
const series = useUsersShows({ list, showType: 'tv', session, mode: 'default' });
const showType = useAtomValue(showTypeAtom);
const { shows, fetchNextPage, isFetched } = useUsersShows({ list, showType, session, mode: 'default' });
const tabs = [
{ query: movies, title: 'Movies', key: 'movie' as const },
{ query: series, title: 'Series', key: 'tv' as const }
];
if (isFetched && !shows.length) {
return (
<div className='flex h-40 w-full items-center justify-center text-center text-xl text-muted-foreground'>
There are no {showType === 'tv' ? 'series' : 'movies'} in your {label}
</div>
);
}
return (
<Tabs defaultValue='movie' className='flex w-full flex-col items-center'>
<TabsList className='grid w-full grid-cols-2'>
{tabs.map(({ title, key }) => (
<TabsTrigger key={key} value={key}>
{title}
</TabsTrigger>
))}
</TabsList>
{tabs.map(({ query, key, title }) => (
<TabsContent key={key} value={key} className='w-full'>
{query.isFetched && !query.shows.length ? (
<div className='flex h-40 w-full items-center justify-center text-center text-xl text-muted-foreground'>
There are no {title.toLowerCase()} in your {label}
</div>
) : (
<List shows={query.shows} fetchNextPage={query.fetchNextPage} />
)}
</TabsContent>
))}
</Tabs>
);
return <List shows={shows} fetchNextPage={fetchNextPage} />;
};
export default UsersList;
+33 -9
View File
@@ -1,32 +1,56 @@
'use client';
import { FC } from 'react';
import { FC, useRef } from 'react';
import { useAtom } from 'jotai';
import Preview from '@/components/Preview';
import { showTypeAtom } from '@/components/ShowTypeToggle';
import Trending from '@/components/Trending';
import { Skeleton } from '@/components/ui/skeleton';
import { generatePage } from '@/data/queries/random';
import Carousel from './components/Carousel';
import useRandomMovie from './useRandomMovie';
interface Props {
page: number;
type: Show['type'];
}
const Container: FC<Props> = ({ page }) => {
const { movie, movies, onIndexChange, fetchNextPage } = useRandomMovie({ page });
const Container: FC<Props> = ({ page, type }) => {
const [showType] = useAtom(showTypeAtom);
const pagesRef = useRef<Record<Show['type'], number>>();
if (!pagesRef.current) {
pagesRef.current = {
movie: type === 'movie' ? page : generatePage('movie'),
tv: type === 'tv' ? page : generatePage('tv')
};
}
const { show, shows, onIndexChange, fetchNextPage, isLoading } = useRandomMovie({
page: pagesRef.current[showType],
type: showType
});
return (
<div className='flex flex-1 flex-col'>
<div className='xs:mb-4'>
<Preview
show={movie}
className='rounded-md bg-background'
poster={<Carousel shows={movies} onIndexChange={onIndexChange} onEndReached={fetchNextPage} />}
/>
{show ? (
<Preview
show={show}
className='rounded-md bg-background'
poster={
<Carousel key={showType} shows={shows} onIndexChange={onIndexChange} onEndReached={fetchNextPage} />
}
/>
) : (
<Skeleton className='card-aspect-ratio static-aspect-ratio mx-auto rounded-lg' />
)}
<div className='h-2 w-full' />
</div>
<div className='hidden w-full rounded-lg xs:block'>
<Trending showId={movie?.id} showType={movie?.type} />
<Trending showId={show?.id} showType={show?.type} />
<div className='h-2 w-full' />
</div>
</div>
+12 -6
View File
@@ -6,30 +6,36 @@ import JsonLd from '@/components/JsonLd';
import randomQuery, { generatePage } from '@/data/queries/random';
import { SITE_URL, TITLE } from '@/env';
import getQueryClient from '@/lib/queryClient';
import { parseShowType } from '@/lib/showType';
import Container from './container';
interface SearchParams {
type?: string;
}
export const metadata: Metadata = {
alternates: { canonical: '/' }
};
const Random = async () => {
const Random = async ({ searchParams }: { searchParams: SearchParams }) => {
const queryClient = getQueryClient();
const page = generatePage();
const type = parseShowType(searchParams.type);
const page = generatePage(type);
await queryClient.prefetchInfiniteQuery(randomQuery({ page }));
await queryClient.prefetchInfiniteQuery(randomQuery({ page, type }));
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<JsonLd data={{ '@context': 'https://schema.org', '@type': 'WebSite', name: TITLE, url: SITE_URL }} />
<h1 className='sr-only'>{TITLE} — Discover Your Next Favorite Movie</h1>
<Container page={page} />
<Container page={page} type={type} />
<section className='sr-only'>
<h2>Discover your next favorite movie</h2>
<p>
{TITLE} helps you decide what to watch: spin through random movie recommendations right on this page, browse
the{' '}
{TITLE} helps you decide what to watch: spin through random movie and series recommendations right on this
page, browse the{' '}
<Link className='underline underline-offset-2' href='/popular'>
most popular movies
</Link>{' '}
+16 -10
View File
@@ -1,6 +1,6 @@
'use client';
import { startTransition, useCallback, useRef } from 'react';
import { startTransition, useCallback, useEffect, useRef } from 'react';
import { atom, getDefaultStore, useAtom } from 'jotai';
import randomQuery from '@/data/queries/random';
@@ -13,32 +13,38 @@ export const useSilentIndex = () => getDefaultStore().get(indexAtom);
interface Props {
page: number;
type: Show['type'];
}
const useRandomMovie = ({ page }: Props) => {
const useRandomMovie = ({ page, type }: Props) => {
const [index, setIndex] = useAtom(indexAtom);
const { shows: movies, fetchNextPage } = useInfiniteList({
...randomQuery({ page }),
useEffect(() => {
setIndex(0);
}, [type, setIndex]);
const { shows, fetchNextPage, isLoading } = useInfiniteList({
...randomQuery({ page, type }),
mode: 'default',
list: 'random'
});
const moviesRef = useRef(movies);
const showsRef = useRef(shows);
moviesRef.current = movies;
showsRef.current = shows;
if (movies.length && !movies[index]) {
if (shows.length && !shows[index]) {
setIndex(0);
}
return {
movie: movies[index],
movies,
show: shows[index],
shows,
isLoading,
fetchNextPage,
onIndexChange: useCallback(
(index: number) => {
analytics.randomShuffled({ index, showTitle: moviesRef.current[index]?.title ?? '' });
analytics.randomShuffled({ index, showTitle: showsRef.current[index]?.title ?? '' });
startTransition(() => setIndex(index));
},
[setIndex]
+35 -20
View File
@@ -6,34 +6,49 @@ import popularQuery from '@/data/queries/popular';
import { TITLE } from '@/env';
import { itemList } from '@/lib/jsonLd';
import getQueryClient from '@/lib/queryClient';
import { parseShowType } from '@/lib/showType';
import Container from './container';
const HEADING = 'Popular Movies';
interface SearchParams {
type?: string;
}
const DESCRIPTION = `Check out the most popular movies right now on ${TITLE}. See what’s trending and don’t miss out on the latest hits.`;
const heading = (type: Show['type']) => (type === 'tv' ? 'Popular Series' : 'Popular Movies');
export const metadata: Metadata = {
title: `${HEADING} | ${TITLE}`,
description: DESCRIPTION,
alternates: { canonical: '/popular' },
openGraph: {
type: 'website',
siteName: TITLE,
title: HEADING,
description: DESCRIPTION,
url: '/popular'
},
twitter: {
title: HEADING,
description: DESCRIPTION
}
const description = (type: Show['type']) =>
type === 'tv'
? `Check out the most popular series right now on ${TITLE}. See what’s trending and don’t miss out on the latest hits.`
: `Check out the most popular movies right now on ${TITLE}. See what’s trending and don’t miss out on the latest hits.`;
export const generateMetadata = ({ searchParams }: { searchParams: SearchParams }): Metadata => {
const type = parseShowType(searchParams.type);
const title = heading(type);
const text = description(type);
return {
title: `${title} | ${TITLE}`,
description: text,
alternates: { canonical: '/popular' },
openGraph: {
type: 'website',
siteName: TITLE,
title,
description: text,
url: '/popular'
},
twitter: {
title,
description: text
}
};
};
const Popular = async () => {
const Popular = async ({ searchParams }: { searchParams: SearchParams }) => {
const queryClient = getQueryClient();
const query = popularQuery();
const type = parseShowType(searchParams.type);
const query = popularQuery({ type });
await queryClient.prefetchInfiniteQuery(query);
@@ -44,7 +59,7 @@ const Popular = async () => {
return (
<HydrationBoundary state={dehydrate(queryClient)}>
{Boolean(shows?.length) && <JsonLd data={itemList(shows!)} />}
<h1 className='sr-only'>{HEADING}</h1>
<h1 className='sr-only'>{heading(type)}</h1>
<Container />
</HydrationBoundary>
);
+8 -1
View File
@@ -1,8 +1,15 @@
'use client';
import { useAtomValue } from 'jotai';
import { showTypeAtom } from '@/components/ShowTypeToggle';
import popularQuery from '@/data/queries/popular';
import useInfiniteList from '@/hooks/useInfiniteList';
const usePopular = () => useInfiniteList({ ...popularQuery(), mode: 'default', list: 'popular' });
const usePopular = () => {
const showType = useAtomValue(showTypeAtom);
return useInfiniteList({ ...popularQuery({ type: showType }), mode: 'default', list: 'popular' });
};
export default usePopular;
+35 -20
View File
@@ -6,34 +6,49 @@ import popularQuery from '@/data/queries/popular';
import { TITLE } from '@/env';
import { itemList } from '@/lib/jsonLd';
import getQueryClient from '@/lib/queryClient';
import { parseShowType } from '@/lib/showType';
import Container from './container';
const HEADING = 'Top Rated Movies';
interface SearchParams {
type?: string;
}
const DESCRIPTION = `Discover the top-rated movies on ${TITLE}. Find the highest-rated films and make informed viewing choices.`;
const heading = (type: Show['type']) => (type === 'tv' ? 'Top Rated Series' : 'Top Rated Movies');
export const metadata: Metadata = {
title: `${HEADING} | ${TITLE}`,
description: DESCRIPTION,
alternates: { canonical: '/top' },
openGraph: {
type: 'website',
siteName: TITLE,
title: HEADING,
description: DESCRIPTION,
url: '/top'
},
twitter: {
title: HEADING,
description: DESCRIPTION
}
const description = (type: Show['type']) =>
type === 'tv'
? `Discover the top-rated series on ${TITLE}. Find the highest-rated shows and make informed viewing choices.`
: `Discover the top-rated movies on ${TITLE}. Find the highest-rated films and make informed viewing choices.`;
export const generateMetadata = ({ searchParams }: { searchParams: SearchParams }): Metadata => {
const type = parseShowType(searchParams.type);
const title = heading(type);
const text = description(type);
return {
title: `${title} | ${TITLE}`,
description: text,
alternates: { canonical: '/top' },
openGraph: {
type: 'website',
siteName: TITLE,
title,
description: text,
url: '/top'
},
twitter: {
title,
description: text
}
};
};
const Top = async () => {
const Top = async ({ searchParams }: { searchParams: SearchParams }) => {
const queryClient = getQueryClient();
const query = popularQuery({ sortBy: 'vote_average.desc' });
const type = parseShowType(searchParams.type);
const query = popularQuery({ sortBy: 'vote_average.desc', type });
await queryClient.prefetchInfiniteQuery(query);
@@ -44,7 +59,7 @@ const Top = async () => {
return (
<HydrationBoundary state={dehydrate(queryClient)}>
{Boolean(shows?.length) && <JsonLd data={itemList(shows!)} />}
<h1 className='sr-only'>{HEADING}</h1>
<h1 className='sr-only'>{heading(type)}</h1>
<Container />
</HydrationBoundary>
);
+12 -2
View File
@@ -1,9 +1,19 @@
'use client';
import { useAtomValue } from 'jotai';
import { showTypeAtom } from '@/components/ShowTypeToggle';
import popularQuery from '@/data/queries/popular';
import useInfiniteList from '@/hooks/useInfiniteList';
const useTop = () =>
useInfiniteList({ ...popularQuery({ sortBy: 'vote_average.desc' }), mode: 'default', list: 'top' });
const useTop = () => {
const showType = useAtomValue(showTypeAtom);
return useInfiniteList({
...popularQuery({ sortBy: 'vote_average.desc', type: showType }),
mode: 'default',
list: 'top'
});
};
export default useTop;