feat: show page

This commit is contained in:
ner committed 2024-11-06 22:12:19 +02:00
1 parent abe1c795a1
commit 183c7231a1
26 files changed
+210 -176

No files matched your search

@@ -0,0 +1,116 @@
'use client';
import { ForwardedRef, forwardRef, memo, useLayoutEffect, useMemo, useRef } from 'react';
import { Virtuoso, VirtuosoProps } from 'react-virtuoso';
import Poster from '@/components/Poster';
import ScrollNavigation from '@/components/ScrollNavigation';
import { cn } from '@/lib/utils';
import { useSilentIndex } from '../../useRandomMovie';
interface Context {
onIndexChange: (index: number) => void;
gap?: number;
}
interface Props extends Context {
shows?: Show[];
onEndReached?: () => void;
}
const Carousel = memo(
function Carousel({ shows, gap = 8, onIndexChange, onEndReached }: Props) {
const index = useSilentIndex();
const context = useMemo(() => ({ onIndexChange, gap }), [onIndexChange, gap]);
return (
<div className='card-aspect-ratio relative mx-auto'>
<Virtuoso
initialItemCount={1}
horizontalDirection
skipAnimationFrameInResizeObserver
data={shows}
context={context}
endReached={onEndReached}
initialTopMostItemIndex={index}
components={components}
increaseViewportBy={1000}
/>
</div>
);
},
(prev, next) => prev.shows === next.shows
);
const components: VirtuosoProps<Show, Context>['components'] = {
Item: ({ item, children, context, ...props }) => {
const ref = useRef<HTMLDivElement>(null);
const index = props['data-index'];
useLayoutEffect(() => {
const container = ref.current;
if (!container) return;
const observer = new IntersectionObserver(
(entries) =>
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
context?.onIndexChange?.(index);
}),
{
threshold: 0.7
}
);
observer.observe(container);
return () => observer.disconnect();
}, [context, index]);
return (
<div {...props} ref={ref} className='snap-start'>
<Poster lazy={false} show={item} />
</div>
);
},
List: forwardRef(function List({ context, ...props }, ref) {
return <div {...props} ref={ref} style={{ ...props.style, display: 'flex', gap: context?.gap }} />;
}),
Scroller: forwardRef(function Scroller({ context, ...props }, ref) {
const handleRef =
(ref: ForwardedRef<HTMLDivElement>, setRef: (element: HTMLDivElement) => void) =>
(element: HTMLDivElement | null) => {
if (!element || !ref) return;
setRef(element);
typeof ref === 'function' ? ref(element) : (ref.current = element);
};
return (
<ScrollNavigation<HTMLDivElement>
className='size-full'
arrowsClassName='h-20 bg-secondary'
backClassName='rounded-none rounded-r-full'
nextClassName='rounded-none rounded-l-full'
gap={context?.gap}
>
{({ setScrollElement, isArrowHovered }) => (
<div
{...props}
ref={handleRef(ref, setScrollElement)}
className={cn('no-scrollbar h-full overflow-hidden rounded-lg', {
'snap-x snap-mandatory': !isArrowHovered
})}
/>
)}
</ScrollNavigation>
);
})
};
export default Carousel;
+36
View File
@@ -0,0 +1,36 @@
'use client';
import { FC } from 'react';
import Preview from '@/components/Preview';
import Trending from '@/components/Trending';
import Carousel from './(components)/Carousel';
import useRandomMovie from './useRandomMovie';
interface Props {
page: number;
}
const Container: FC<Props> = ({ page }) => {
const { movie, movies, onIndexChange, fetchNextPage } = useRandomMovie({ page });
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} />}
/>
<div className='h-2 w-full' />
</div>
<div className='hidden w-full rounded-lg xs:block'>
<Trending showId={movie.id} showType={movie.type} />
<div className='h-2 w-full' />
</div>
</div>
);
};
export default Container;
File renamed without changes.
+39
View File
@@ -0,0 +1,39 @@
'use client';
import { startTransition, useCallback } from 'react';
import { atom, getDefaultStore, useAtom } from 'jotai';
import randomQuery from '@/data/queries/random';
import useInfiniteList from '@/hooks/useInfiniteList';
const indexAtom = atom(0);
export const useSilentIndex = () => getDefaultStore().get(indexAtom);
interface Props {
page: number;
}
const useRandomMovie = ({ page }: Props) => {
const [index, setIndex] = useAtom(indexAtom);
const { shows: movies, fetchNextPage } = useInfiniteList({ ...randomQuery({ page }), mode: 'default' });
if (movies.length && !movies[index]) {
setIndex(0);
}
return {
movie: movies[index],
movies,
fetchNextPage,
onIndexChange: useCallback(
(index: number) => {
startTransition(() => setIndex(index));
},
[setIndex]
)
};
};
export default useRandomMovie;
+29
View File
@@ -0,0 +1,29 @@
import { FC } from 'react';
import Preview from '@/components/Preview';
import Trending from '@/components/Trending';
interface Props {
showId: Show['id'];
showType: Show['type'];
}
const Container: FC<Props> = ({ showId, showType }) => (
<div className='flex flex-1 flex-col'>
<div className='xs:mb-4'>
<Preview
showId={showId}
showType={showType}
className='rounded-lg bg-background'
posterClassName='md:rounded-lg'
/>
<div className='h-2 w-full' />
</div>
<div className='hidden w-full rounded-lg xs:block'>
<Trending preview showId={showId} showType={showType} />
<div className='h-2 w-full' />
</div>
</div>
);
export default Container;
+34
View File
@@ -0,0 +1,34 @@
import { FC } from 'react';
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import { redirect } from 'next/navigation';
import detailsQuery from '@/data/queries/details';
import getQueryClient from '@/lib/queryClient';
import Container from './container';
interface Props {
params: { show: [Show['type'], Show['id']] };
}
const ShowPage: FC<Props> = async ({ params: { show } }) => {
const queryClient = getQueryClient();
const [showType, showId] = show as [Show['type'], Show['id']];
if (!showType || !showId) return redirect('/');
try {
await queryClient.fetchQuery(detailsQuery({ showId, showType }));
} catch (_) {
return redirect('/');
}
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<Container showId={showId} showType={showType} />
</HydrationBoundary>
);
};
export default ShowPage;
-23
View File
@@ -1,23 +0,0 @@
import { FC } from 'react';
import Random from '@/components/Random';
import Trending from '@/components/Trending';
interface Props {
page: number;
}
const Container: FC<Props> = ({ page }) => (
<div className='flex flex-1 flex-col'>
<div className='xs:mb-4'>
<Random page={page} />
<div className='h-2 w-full' />
</div>
<div className='hidden w-full rounded-lg xs:block'>
<Trending />
<div className='h-2 w-full' />
</div>
</div>
);
export default Container;
-32
View File
@@ -1,32 +0,0 @@
'use client';
import { useSearchParams } from 'next/navigation';
import List from '@/components/List';
import NoResults from '@/components/NoResults';
import useSimilar from './useSimilar';
const Container = () => {
const params = useSearchParams();
const title = params.get('title') as string;
const showId = params.get('id') as string;
const showType = params.get('type') as Show['type'];
const { shows, fetchNextPage, isFetched } = useSimilar({ showId, showType });
return (
<>
{Boolean(title) && (
<span className='mb-2 inline-flex h-10 w-full items-center justify-center rounded-md bg-muted p-1 text-muted-foreground'>
Similar: {decodeURIComponent(title)}
</span>
)}
<List shows={shows} fetchNextPage={fetchNextPage} />
{isFetched && !shows.length && <NoResults />}
</>
);
};
export default Container;
-27
View File
@@ -1,27 +0,0 @@
import { FC } from 'react';
import { dehydrate, HydrationBoundary } from '@tanstack/react-query';
import similarQuery from '@/data/queries/similar';
import getQueryClient from '@/lib/queryClient';
import Container from './container';
interface Props {
searchParams: { id: string; type: Show['type'] };
}
const Similar: FC<Props> = ({ searchParams }) => {
const queryClient = getQueryClient();
const { id, type } = searchParams || {};
queryClient.prefetchInfiniteQuery(similarQuery({ showId: id, showType: type }));
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<Container />
</HydrationBoundary>
);
};
export default Similar;
-13
View File
@@ -1,13 +0,0 @@
'use client';
import similarQuery from '@/data/queries/similar';
import useInfiniteList from '@/hooks/useInfiniteList';
interface Props {
showId: string;
showType: Show['type'];
}
const useSimilar = ({ showId, showType = 'movie' }: Props) => useInfiniteList(similarQuery({ showId, showType }));
export default useSimilar;