import { FC, forwardRef, useMemo } from 'react'; import { VirtuosoGrid, VirtuosoGridProps } from 'react-virtuoso'; 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; withBottomGap?: boolean; onPreviewClose?: () => void; type?: ShowType; } const container = { hidden: {}, visible: { transition: { staggerChildren: 0.15 } } }; const item = { hidden: { y: -20, opacity: 0 }, visible: { y: 0, opacity: 1 } }; const components: VirtuosoGridProps void }>['components'] = { List: forwardRef(function List(props, ref) { return
; }), Item: ({ context, 'data-index': index }) => { const { shows, onClick } = context || {}; const movie = shows?.[index]; return onClick?.(movie!)} />; } }; const List: FC = ({ pages, hasNextPage, fetchNextPage, withBottomGap = true, onPreviewClose, type = 'movie' }) => { 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, onClick: (movie: Movie) => showPreviewModal({ movie, onClose: onPreviewClose, type }) }; const handleFetchMore = () => { if (!hasNextPage) return; fetchNextPage?.(); }; return ( <> {withBottomGap &&
} ); }; export default List;