feat: virtual list of credits

This commit is contained in:
ner committed 2024-10-20 13:54:05 +03:00
1 parent dc8dde4dbb
commit 29b22f88ae
7 files changed
+160 -93

No files matched your search

+87 -39
View File
@@ -1,56 +1,104 @@
'use client';
import { FC } from 'react';
import { User2 } from 'lucide-react';
import Image from 'next/image';
import { FC, ForwardedRef, forwardRef, useMemo } from 'react';
import { Virtuoso, VirtuosoProps } from 'react-virtuoso';
import Link from 'next/link';
import ScrollNavigation from '@/components/ScrollNavigation';
import { cn } from '@/lib/utils';
import useCredits from './useCredits';
interface Props {
showId: Show['id'];
showType?: Show['type'];
onPersonClick?: (id: string) => void;
interface Context {
onClick?: (id: string) => void;
width?: number;
height?: number;
gap?: number;
}
const Credits: FC<Props> = ({ showId, showType = 'movie', onPersonClick }) => {
interface Props extends Context {
showId: Show['id'];
showType?: Show['type'];
}
const Credits: FC<Props> = ({ showId, showType = 'movie', onClick, width = 120, height = 180, gap = 8 }) => {
const { data: credits } = useCredits({ showId, showType });
const context = useMemo(() => ({ width, height, gap, onClick }), [width, height, gap, onClick]);
if (!credits?.length) return null;
return (
Boolean(credits?.length) && (
<ScrollNavigation<HTMLUListElement>>
{(setRef) => (
<ul
ref={setRef}
className='no-scrollbar flex animate-fade-aside-slide-in snap-x snap-mandatory gap-2 overflow-auto rounded-lg opacity-0'
>
{credits?.map((actor) => (
<li
key={`${actor.id}_${actor.cast_id}`}
title={actor.name}
className='relative w-full max-w-[120px] shrink-0 cursor-pointer snap-start self-end bg-card text-card-foreground transition-shadow hover:shadow-lg'
>
<Link
href={`/starring?actorId=${actor.id}&title=${encodeURIComponent(actor.name)}`}
onClick={() => onPersonClick?.(actor.id.toString())}
>
<div className='overflow-hidden rounded-lg'>
<img height={180} width={120} src={actor.photoUrl} alt={actor.name} />
</div>
<div className='absolute left-0 top-0 flex size-full flex-col justify-between rounded-md bg-vignette p-2 py-1 text-sm text-white'>
<p className=' truncate'>{actor.name}</p>
{Boolean(actor.character) && <p className='truncate'>{actor.character}</p>}
</div>
</Link>
</li>
))}
</ul>
)}
</ScrollNavigation>
)
<Virtuoso
style={{ height }}
horizontalDirection
initialItemCount={10}
increaseViewportBy={width * 10}
data={credits}
context={context}
components={components}
/>
);
};
const components: VirtuosoProps<Actor, Context>['components'] = {
Item: ({ item: actor, children, context, ...props }) => {
if (!actor) return null;
return (
<div
{...props}
title={actor.name}
style={{ width: context?.width, height: context?.height }}
className='relative shrink-0 cursor-pointer snap-start self-end bg-card text-card-foreground transition-shadow hover:shadow-lg'
>
<Link
href={`/starring?actorId=${actor.id}&title=${encodeURIComponent(actor.name)}`}
onClick={() => context?.onClick?.(actor.id.toString())}
>
<div className='overflow-hidden rounded-lg'>
<img height={180} width={120} src={actor.photoUrl} alt={actor.name} />
</div>
<div className='absolute left-0 top-0 flex size-full flex-col justify-between rounded-md bg-vignette p-2 py-1 text-sm text-white'>
<p className=' truncate'>{actor.name}</p>
{Boolean(actor.character) && <p className='truncate'>{actor.character}</p>}
</div>
</Link>
</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 slideWidth = (context?.width || 0) + (context?.gap || 0);
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 (
<div className='animate-fade-aside-slide-in opacity-0'>
<ScrollNavigation<HTMLDivElement> slideWidth={slideWidth}>
{({ setScrollElement, isArrowHovered }) => (
<div
{...props}
ref={handleRef(ref, setScrollElement)}
className={cn('no-scrollbar h-full overflow-hidden rounded-lg', {
'snap-x snap-mandatory': !isArrowHovered
})}
/>
)}
</ScrollNavigation>
</div>
);
})
};
export default Credits;
@@ -2,7 +2,6 @@ import { useState } from 'react';
import { create, InstanceProps } from 'react-modal-promise';
import { Loader } from 'lucide-react';
import dynamic from 'next/dynamic';
import Image from 'next/image';
import { Modal } from '@/components/ui/dialog';
import { useToast } from '@/hooks/useToast';
@@ -48,7 +47,7 @@ const showPlayer = create(({ onResolve, magnet, backdrop, title }: Props) => {
{(!isReady || isLoading) && (
<div className='absolute left-0 top-0 size-full'>
<img src={backdrop} className='absolute left-0 top-0 size-full' alt={title} />
<div className='absolute left-0 top-0 flex size-full items-center justify-center bg-black/80'>
<div className='absolute left-0 top-0 flex size-full items-center justify-center bg-black/60'>
<div className='animate-spin'>
<Loader color='white' />
</div>
+1 -1
View File
@@ -56,7 +56,7 @@ const Preview: FC<Props> = ({ show: baseShow, className, onClose, poster }) => {
{show.overview}
</p>
<div className='order-5 mt-5 grid grid-cols-1 rounded-lg md:order-5 md:mt-auto'>
<Credits showType={show.type} showId={show.id} onPersonClick={onClose} />
<Credits showType={show.type} showId={show.id} onClick={onClose} />
</div>
</div>
</div>