import React, { useRef } from 'react'; import { useSWRInfinite } from 'swr'; import type { MovieResult } from '../../../server/models/Search'; import TitleCard from '../TitleCard'; import useVerticalScroll from '../../hooks/useVerticalScroll'; interface SearchResult { page: number; totalResults: number; totalPages: number; results: MovieResult[]; } const getKey = (pageIndex: number, previousPageData: SearchResult | null) => { if (previousPageData && pageIndex + 1 > previousPageData.totalPages) { return null; } return `/api/v1/discover/movies?page=${pageIndex + 1}`; }; const Discover: React.FC = () => { const { data, error, size, setSize } = useSWRInfinite(getKey, { initialSize: 3, }); const isLoadingInitialData = !data && !error; const isLoadingMore = isLoadingInitialData || (size > 0 && data && typeof data[size - 1] === 'undefined'); useVerticalScroll(() => { setSize(size + 1); }, !isLoadingMore && !isLoadingInitialData); if (error) { return
{error}
; } const titles = data?.reduce( (a, v) => [...a, ...v.results], [] as MovieResult[] ); return ( <>

Discover

); }; export default Discover;