import { FC, useMemo, useRef } from 'react'; import { VirtuosoGrid, VirtuosoGridProps } from 'react-virtuoso'; import { motion } from 'framer-motion'; import Card from '@/components/Movie/Card'; import { showPreviewModal } from '@/components/Movie/Preview'; import filterUnknownMovies from '@/lib/filterUnknownMovies'; interface Props { pages: Array; hasNextPage?: boolean; fetchNextPage?: () => void; onPreviewClose?: () => void; type?: ShowType; customScrollParent?: HTMLElement; } const item = { hidden: { y: -20, opacity: 0 }, visible: { y: 0, opacity: 1 } }; const components: VirtuosoGridProps< Movie, { shows: Movie[]; initial: boolean; onClick: (movie: Movie) => void } >['components'] = { Item: ({ context, 'data-index': index }) => { const { shows, onClick } = context || {}; const movie = shows?.[index]; return ( onClick?.(movie!)} /> ); } }; const List: FC = ({ pages, hasNextPage, fetchNextPage, onPreviewClose, type = 'movie', customScrollParent }) => { const initial = useRef(true); const shows = useMemo( () => pages?.reduce((shows, page) => { shows.push(...filterUnknownMovies(page.results)); return shows; }, [] as Movie[]) || [], [pages] ); if (!shows.length) return null; const context = { shows, initial: initial.current, onClick: (movie: Movie) => showPreviewModal({ movie, onClose: onPreviewClose, type }) }; const handleFetchMore = () => { if (!hasNextPage) return; initial.current = false; fetchNextPage?.(); }; return ( <>
); }; export default List;