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
@@ -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>
|
||||
|
||||
@@ -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>{' '}
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user