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

@@ -45,7 +45,7 @@ export const DesktopNavigation = () => {
const active = paths.find(({ path }) => path === currentPath);
return (
<Nav tabs={paths} active={active || paths[0]}>
<Nav tabs={paths} active={active}>
{({ path, title }) => (
<Link href={path} prefetch={path !== '/'}>
<div>{title}</div>
+1 -1
View File
@@ -28,8 +28,8 @@ const Header: FC<Props> = ({ className }) => (
</Link>
<DesktopNavigation />
<div className='flex items-center gap-2'>
<ToggleTheme />
<ToggleSearch />
<ToggleTheme />
<User />
<MobileNavigation />
</div>
+24 -20
View File
@@ -1,36 +1,37 @@
'use client';
import { Bookmark, Copy, Heart, Play, Star } from 'lucide-react';
import { useRouter } from 'next/navigation';
import { Bookmark, Heart, Link, Play, Star } from 'lucide-react';
import { Button } from '@/components/ui/button';
import ButtonsGroup from '@/components/ui/buttons-group';
import useShowState, { useMutateShowState } from '@/hooks/useShowState';
import { useToast } from '@/hooks/useToast';
import { cn } from '@/lib/utils';
interface Props {
show: Show & Partial<Details>;
className: string;
onClose?: () => void;
}
const Actions = ({ onClose, show, className }: Props) => {
const router = useRouter();
const Actions = ({ show, className }: Props) => {
const { toast } = useToast();
const state = useShowState({ showId: show.id, showType: show.type });
const toggle = useMutateShowState(show);
return (
<div className={cn('flex gap-2 flex-wrap md:flex-grow-0 flex-grow', className)}>
<div className={cn('flex gap-2 flex-grow-0', className)}>
<ButtonsGroup className='pointer-events-none'>
<Button aria-label='Show rating' variant='outline'>
<Star size={14} />
</Button>
<Button variant='outline'>{show.rating.toFixed(1)}</Button>
<Button variant='outline' className='w-[43px]'>
{show.rating.toFixed(1)}
</Button>
</ButtonsGroup>
<ButtonsGroup>
<Button
aria-label='Toggle favorite'
title='Toggle favorite'
variant='outline'
disabled={toggle.list === 'favorite' && toggle.isPending}
onClick={() => toggle.mutate({ list: 'favorite', value: !state?.favorite })}
@@ -39,6 +40,7 @@ const Actions = ({ onClose, show, className }: Props) => {
</Button>
<Button
aria-label='Toggle watch list'
title='Toggle watch list'
variant='outline'
disabled={toggle.list === 'watchlist' && toggle.isPending}
onClick={() => toggle.mutate({ list: 'watchlist', value: !state?.watchlist })}
@@ -46,18 +48,20 @@ const Actions = ({ onClose, show, className }: Props) => {
<Bookmark size={14} className={cn({ 'fill-secondary-foreground': state?.watchlist })} />
</Button>
</ButtonsGroup>
<ButtonsGroup>
<Button
onClick={() => {
onClose?.();
router.push(`/similar?id=${show.id}&title=${encodeURIComponent(show.title)}&type=${show.type}`);
}}
aria-label='Similar show'
variant='outline'
>
<Copy size={14} />
Similar
</Button>
<Button
aria-label='Copy link'
title='Copy link'
variant='outline'
className='h-8 px-3'
onClick={() =>
navigator.clipboard
.writeText(`${location.origin}/${show.type}/${show.id}`)
.then(() => toast({ description: 'Link has been copied' }))
}
>
<Link size={14} />
</Button>
<ButtonsGroup className='ml-auto sm:ml-0'>
<Button
aria-label='Watch'
className='bg-red-600 transition-all duration-200 hover:bg-red-600 hover:shadow-lg hover:shadow-red-600/60 dark:bg-red-700'
+10 -7
View File
@@ -12,18 +12,21 @@ import { cn } from '@/lib/utils';
import Actions from './components/Actions';
interface Props {
show?: Show;
show?: Show & Partial<Details>;
showType?: Show['type'];
showId?: Show['id'];
className?: string;
posterClassName?: string;
onClose?: () => void;
poster?: ReactNode;
}
const Preview: FC<Props> = ({ show: baseShow, className, onClose, poster }) => {
const { data: detailedShow } = useDetails({ showId: baseShow?.id, showType: baseShow?.type });
const Preview: FC<Props> = ({ show: baseShow, showType, showId, className, posterClassName, onClose, poster }) => {
const { data: detailedShow } = useDetails({ showId: showId || baseShow?.id, showType: showType || baseShow?.type });
if (!baseShow) return null;
if (!baseShow && !detailedShow) return null;
const show: (Show & Partial<Details>) | undefined = detailedShow || baseShow;
const show: Show & Partial<Details> = (detailedShow || baseShow)!;
const isModal = Boolean(onClose);
@@ -35,7 +38,7 @@ const Preview: FC<Props> = ({ show: baseShow, className, onClose, poster }) => {
className
)}
>
{poster || <Poster className='mx-auto md:rounded-none md:rounded-l-xl' show={show} />}
{poster || <Poster className={cn('mx-auto md:rounded-none md:rounded-l-xl', posterClassName)} show={show} />}
<div className={cn('flex grow flex-col bg-background', { 'p-0 pt-1 md:p-2': isModal })}>
<span className='order-3 mb-4 text-3xl md:order-1 md:line-clamp-2'>{show.title}</span>
@@ -54,7 +57,7 @@ const Preview: FC<Props> = ({ show: baseShow, className, onClose, poster }) => {
)}
</div>
<Actions className='order-1 mb-4 mt-1 md:order-2 md:mt-0' show={show} onClose={onClose} />
<Actions className='order-1 mb-4 mt-1 md:order-2 md:mt-0' show={show} />
<p key={show.id} className='order-4 md:order-4 md:line-clamp-3' title={show.overview}>
{show.overview}
@@ -1,116 +0,0 @@
'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;
-26
View File
@@ -1,26 +0,0 @@
'use client';
import { FC } from 'react';
import Preview from '../Preview';
import Carousel from './components/Carousel';
import useRandomMovie from './useRandomMovie';
interface Props {
page: number;
}
const Random: FC<Props> = ({ page }) => {
const { movie, movies, onIndexChange, fetchNextPage } = useRandomMovie({ page });
return (
<Preview
show={movie}
className='rounded-md bg-background'
poster={<Carousel shows={movies} onIndexChange={onIndexChange} onEndReached={fetchNextPage} />}
/>
);
};
export default Random;
-39
View File
@@ -1,39 +0,0 @@
'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;
+8
View File
@@ -10,5 +10,13 @@ export const tabs = [
{
title: 'In theaters',
type: 'theater'
},
{
title: 'Similar',
type: 'similar'
},
{
title: 'Recommendations',
type: 'recommendations'
}
];
+29 -8
View File
@@ -2,26 +2,47 @@
import { FC, useState } from 'react';
import useSimilar from '@/components/Trending/useSimilar';
import Nav from '@/components/ui/nav';
import Gallery from './components/Gallery';
import { tabs } from './constants';
import { tabs as initialTabs } from './constants';
import useTrending from './useTrending';
const Trending: FC = () => {
interface Props {
showType: Show['type'];
showId: Show['id'];
preview?: boolean;
}
const Trending: FC<Props> = ({ showId, showType, preview }) => {
const tabs = preview ? initialTabs.filter(({ type }) => ['similar', 'recommendations'].includes(type)) : initialTabs;
const [active, setActive] = useState(tabs[0]);
const { data: streaming, isFetched } = useTrending({ type: 'streaming' });
const { data: trending } = useTrending({ type: 'trending' });
const { data: theater } = useTrending({ type: 'theater' });
const streaming = useTrending({ type: 'streaming', enabled: !preview });
const trending = useTrending({ type: 'trending', enabled: active.type === 'trending' });
const theater = useTrending({ type: 'theater', enabled: active.type === 'theater' });
const similar = useSimilar({
showId,
showType,
type: 'similar',
enabled: active.type === 'similar'
});
const recommendations = useSimilar({
showId,
showType,
type: 'recommendations',
enabled: active.type === 'recommendations'
});
const shows = { trending, streaming, theater }[active.type] || [];
const tab = { trending, streaming, theater, similar, recommendations }[active.type]!;
return (
isFetched && (
(streaming.isFetched || similar.isFetched) && (
<div className='hidden md:block'>
<Nav tabs={tabs} active={active} onChange={(active) => setActive(active)} className='mb-3' />
<Gallery key={active.type} shows={shows} />
{!tab.isPending && <Gallery key={active.type} shows={tab.data || []} />}
</div>
)
);
+17
View File
@@ -0,0 +1,17 @@
'use client';
import { useQuery } from '@tanstack/react-query';
import similarQuery from '@/data/queries/similar';
interface Props {
showId: Show['id'];
showType: Show['type'];
type?: 'similar' | 'recommendations';
enabled: boolean;
}
const useSimilar = ({ showId, showType = 'movie', type = 'similar', enabled }: Props) =>
useQuery({ ...similarQuery({ showId, showType, type }), enabled });
export default useSimilar;
+1 -1
View File
@@ -2,6 +2,6 @@ import { useQuery } from '@tanstack/react-query';
import trendingQuery, { TrendingQueryProps } from '@/data/queries/trending';
const useTrending = ({ type }: TrendingQueryProps) => useQuery(trendingQuery({ type }));
const useTrending = ({ type, enabled }: TrendingQueryProps) => useQuery(trendingQuery({ type, enabled }));
export default useTrending;
+2 -2
View File
@@ -8,13 +8,13 @@ interface Props {
}
const ButtonsGroup: FC<Props> = ({ children, className }) => (
<div className={cn('flex bg-input gap-[1px] rounded-md md:flex-grow-0 flex-grow group', className)}>
<div className={cn('flex bg-input gap-[1px] rounded-md group', className)}>
{Children.map(children, (child) => {
if (!isValidElement(child)) return null;
return cloneElement(child as ReactElement<{ className?: string }>, {
className: cn(
'h-8 gap-1 flex-grow md:px-3 px-2 [&:not(:first-child):not(:last-child)]:border-x-0 rounded-none first:border-r-0 first:rounded-l-md last:border-l-0 last:rounded-r-md',
'h-8 gap-1 flex-grow px-3 [&:not(:first-child):not(:last-child)]:border-x-0 rounded-none first:border-r-0 first:rounded-l-md last:border-l-0 last:rounded-r-md',
child.props.className
)
});
+3 -3
View File
@@ -17,7 +17,7 @@ const Cursor: FC<CursorProps> = ({ width, left, opacity, onTransitionEnd }) => (
export type NavProps<T extends Tab> = {
tabs: T[];
className?: string;
active: T;
active?: T;
onChange?: (tab: T) => void;
children?: (tab: T) => ReactNode;
};
@@ -39,7 +39,7 @@ function Nav<T extends Tab>({ tabs, active, className, children, onChange }: Nav
{tabs.map((tab) => {
const { title } = tab;
const isActive = title === active.title;
const isActive = title === active?.title;
return (
<li
@@ -48,7 +48,7 @@ function Nav<T extends Tab>({ tabs, active, className, children, onChange }: Nav
onClick={() => onChange?.(tab)}
className={cn('z-10 px-2.5 py-0.5 font-normal text-[15px] leading-[20px] cursor-pointer', {
'cursor-default text-secondary': isActive,
'mix-blend-difference text-white': !transitionEnded
'mix-blend-difference text-white': active && !transitionEnded
})}
>
{children?.(tab) || <div>{title}</div>}