Seed SWR photos

This commit is contained in:
Sam Becker 2024-04-25 17:18:54 -05:00
parent 11e87e5295
commit cc17cc9e45
2 changed files with 10 additions and 11 deletions

View File

@ -6,7 +6,6 @@ import {
import PhotosEmptyState from '@/photo/PhotosEmptyState'; import PhotosEmptyState from '@/photo/PhotosEmptyState';
import { Metadata } from 'next/types'; import { Metadata } from 'next/types';
import { MAX_PHOTOS_TO_SHOW_OG } from '@/image-response'; import { MAX_PHOTOS_TO_SHOW_OG } from '@/image-response';
import PhotosLarge from '@/photo/PhotosLarge';
import InfinitePhotoScroll from '../photo/InfinitePhotoScroll'; import InfinitePhotoScroll from '../photo/InfinitePhotoScroll';
export const dynamic = 'force-static'; export const dynamic = 'force-static';
@ -30,12 +29,8 @@ export default async function HomePage() {
return ( return (
photos.length > 0 photos.length > 0
? <div className="space-y-1"> ? <div className="space-y-1">
<PhotosLarge
photos={photos}
prefetchFirstPhotoLinks={true}
/>
<InfinitePhotoScroll <InfinitePhotoScroll
initialOffset={INFINITE_SCROLL_MULTIPLE_HOME} initialPhotos={photos}
itemsPerPage={INFINITE_SCROLL_MULTIPLE_HOME} itemsPerPage={INFINITE_SCROLL_MULTIPLE_HOME}
/> />
</div> </div>

View File

@ -8,8 +8,10 @@ import SiteGrid from '@/components/SiteGrid';
import Spinner from '@/components/Spinner'; import Spinner from '@/components/Spinner';
import { getPhotosAction } from '@/photo/actions'; import { getPhotosAction } from '@/photo/actions';
import { useAppState } from '@/state/AppState'; import { useAppState } from '@/state/AppState';
import { Photo } from '.';
export default function InfinitePhotoScroll({ export default function InfinitePhotoScroll({
initialPhotos,
key = 'PHOTOS', key = 'PHOTOS',
initialOffset = 0, initialOffset = 0,
itemsPerPage = 12, itemsPerPage = 12,
@ -17,6 +19,7 @@ export default function InfinitePhotoScroll({
triggerOnView = true, triggerOnView = true,
debug = true, debug = true,
}: { }: {
initialPhotos?: Photo[]
key?: string key?: string
initialOffset?: number initialOffset?: number
itemsPerPage?: number itemsPerPage?: number
@ -29,8 +32,8 @@ export default function InfinitePhotoScroll({
const buttonRef = useRef<HTMLButtonElement>(null); const buttonRef = useRef<HTMLButtonElement>(null);
const fetcher = useCallback((key: string) => { const fetcher = useCallback((key: string) => {
if (debug) { console.log('Fetching', key); }
const offset = parseInt(key.split('-')[1]); const offset = parseInt(key.split('-')[1]);
if (debug) { console.log('Fetching', offset); }
return getPhotosAction( return getPhotosAction(
initialOffset + offset * itemsPerPage, initialOffset + offset * itemsPerPage,
itemsPerPage, itemsPerPage,
@ -39,15 +42,16 @@ export default function InfinitePhotoScroll({
}, [initialOffset, itemsPerPage, debug]); }, [initialOffset, itemsPerPage, debug]);
const { data, isLoading, error, mutate, size, setSize } = useSwrInfinite( const { data, isLoading, error, mutate, size, setSize } = useSwrInfinite(
(size, prev: []) => prev && prev.length === 0 (size: number, prev: []) => prev && prev.length === 0
? null ? null
:`${key}-${size}`, :`${key}-${size}`,
fetcher, fetcher,
{ {
revalidateOnFocus: isUserSignedIn, revalidateOnFocus: isUserSignedIn,
revalidateOnReconnect: isUserSignedIn, revalidateOnReconnect: isUserSignedIn,
revalidateFirstPage: isUserSignedIn, revalidateFirstPage: isUserSignedIn,
} ...initialPhotos && { fallbackData: [initialPhotos] },
},
); );
const isFinished = useMemo(() => const isFinished = useMemo(() =>
@ -56,7 +60,7 @@ export default function InfinitePhotoScroll({
useEffect(() => { useEffect(() => {
if (prefetch) { if (prefetch) {
preload(`${key}-${size}`, fetcher); preload(`${key}-${size ?? 0 + 1}`, fetcher);
} }
}, [prefetch, key, size, fetcher]); }, [prefetch, key, size, fetcher]);