From c69a965dcea43b4a4f859a4e5b25e0e12e76a81c Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Sun, 2 Feb 2025 22:19:31 -0600 Subject: [PATCH 1/5] Warm functions instead of calling db on first page load --- src/photo/InfinitePhotoScroll.tsx | 21 ++++++++++++++++---- src/photo/PhotoGridPage.tsx | 4 ++-- src/photo/PhotosLargeInfinite.tsx | 4 ++-- src/photo/actions.ts | 32 +++++++++++++++++++++++-------- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/photo/InfinitePhotoScroll.tsx b/src/photo/InfinitePhotoScroll.tsx index 23bd10c5..28d85b2d 100644 --- a/src/photo/InfinitePhotoScroll.tsx +++ b/src/photo/InfinitePhotoScroll.tsx @@ -4,6 +4,7 @@ import useSwrInfinite from 'swr/infinite'; import { ReactNode, useCallback, + useEffect, useMemo, useRef, } from 'react'; @@ -14,6 +15,7 @@ import { Photo, PhotoSetCategory } from '.'; import { clsx } from 'clsx/lite'; import { useAppState } from '@/state/AppState'; import { GetPhotosOptions } from './db'; +import useVisible from '@/utility/useVisible'; export type RevalidatePhoto = ( photoId: string, @@ -56,7 +58,10 @@ export default function InfinitePhotoScroll({ : [key, size] , [key]); - const fetcher = useCallback(([_key, size]: [string, number]) => + const fetcher = useCallback(( + [_key, size]: [string, number], + warmOnly?: boolean, + ) => (useCachedPhotos ? getPhotosCachedAction : getPhotosAction)({ offset: initialOffset + size * itemsPerPage, sortBy, @@ -65,7 +70,7 @@ export default function InfinitePhotoScroll({ tag, camera, simulation, - }) + }, warmOnly) , [ useCachedPhotos, sortBy, @@ -77,18 +82,22 @@ export default function InfinitePhotoScroll({ simulation, ]); - const { data, isLoading, isValidating, error, mutate, setSize } = + const { data, isLoading, isValidating, error, mutate, size, setSize } = useSwrInfinite( keyGenerator, fetcher, { - initialSize: 2, + initialSize: 0, revalidateFirstPage: false, revalidateOnFocus: Boolean(isUserSignedIn), revalidateOnReconnect: Boolean(isUserSignedIn), }, ); + useEffect(() => { + fetcher(['', 0], true); + }, [fetcher]); + const buttonContainerRef = useRef(null); const isLoadingOrValidating = isLoading || isValidating; @@ -116,6 +125,10 @@ export default function InfinitePhotoScroll({ }, } as any), [data, mutate]); + useVisible({ ref: buttonContainerRef, onVisible: () => { + if (size === 0) { advance(); } + }}); + const renderMoreButton = () =>