Fix infinite scroll grid layout

This commit is contained in:
Sam Becker 2024-02-11 10:50:08 -06:00
parent 00b2b57b2a
commit 9810514c76
5 changed files with 36 additions and 29 deletions

View File

@ -1,7 +1,7 @@
import { getPhotosCached } from '@/photo/cache';
import SiteGrid from '@/components/SiteGrid';
import {
PHOTO_LOAD_MULTIPLE_GRID,
INFINITE_SCROLL_MULTIPLE_GRID,
generateOgImageMetaForPhotos,
} from '@/photo';
import PhotoGrid from '@/photo/PhotoGrid';
@ -28,7 +28,7 @@ export default async function GridPage() {
cameras,
simulations,
] = await Promise.all([
getPhotosCached({ limit: PHOTO_LOAD_MULTIPLE_GRID }),
getPhotosCached({ limit: INFINITE_SCROLL_MULTIPLE_GRID }),
...getPhotoSidebarDataCached(),
]);
@ -39,8 +39,8 @@ export default async function GridPage() {
<PhotoGrid {...{ photos }} />
<Suspense>
<MorePhotosGrid
initialOffset={PHOTO_LOAD_MULTIPLE_GRID}
itemsPerRequest={PHOTO_LOAD_MULTIPLE_GRID}
initialOffset={INFINITE_SCROLL_MULTIPLE_GRID}
itemsPerRequest={INFINITE_SCROLL_MULTIPLE_GRID}
totalPhotosCount={photosCount}
/>
</Suspense>

View File

@ -1,6 +1,6 @@
import { getPhotosCached, getPhotosCountCached } from '@/photo/cache';
import {
PHOTO_LOAD_MULTIPLE_ROOT,
INFINITE_SCROLL_MULTIPLE_ROOT,
generateOgImageMetaForPhotos,
} from '@/photo';
import PhotosEmptyState from '@/photo/PhotosEmptyState';
@ -25,7 +25,7 @@ export default async function HomePage() {
count,
] = await Promise.all([
// Make homepage queries resilient to error on first time setup
getPhotosCached({ limit: PHOTO_LOAD_MULTIPLE_ROOT }).catch(() => []),
getPhotosCached({ limit: INFINITE_SCROLL_MULTIPLE_ROOT }).catch(() => []),
getPhotosCountCached().catch(() => 0),
]);
@ -35,8 +35,8 @@ export default async function HomePage() {
<PhotosLarge photos={photos} />
<Suspense>
<MorePhotosRoot
initialOffset={PHOTO_LOAD_MULTIPLE_ROOT}
itemsPerRequest={PHOTO_LOAD_MULTIPLE_ROOT}
initialOffset={INFINITE_SCROLL_MULTIPLE_ROOT}
itemsPerRequest={INFINITE_SCROLL_MULTIPLE_ROOT}
totalPhotosCount={count}
/>
</Suspense>

View File

@ -21,6 +21,7 @@ export default function MoreComponents({
label = 'Load more',
triggerOnView = true,
prefetch = true,
wrapMoreButtonInSiteGrid,
}: {
stateKey: MoreComponentsKey
initialOffset: number
@ -33,6 +34,7 @@ export default function MoreComponents({
label?: string
triggerOnView?: boolean
prefetch?: boolean
wrapMoreButtonInSiteGrid?: boolean
}) {
const { state, setStateForKey } = useMoreComponentsState();
@ -162,23 +164,26 @@ export default function MoreComponents({
}
}, [triggerOnView, advance]);
const renderMoreButton = () =>
<button
ref={buttonRef}
className="block w-full subtle"
onClick={!triggerOnView ? advance : undefined}
disabled={triggerOnView || isLoading}
>
{isLoading || triggerOnView
? <span className="relative inline-block translate-y-[3px]">
<Spinner size={16} />
</span>
: label}
</button>;
return <>
{components.slice(0, indexToView + 1)}
{showMoreButton &&
<SiteGrid
contentMain={
<button
ref={buttonRef}
className="block w-full mt-4 subtle"
onClick={!triggerOnView ? advance : undefined}
disabled={triggerOnView || isLoading}
>
{isLoading || triggerOnView
? <span className="relative inline-block translate-y-[3px]">
<Spinner size={16} />
</span>
: label}
</button>}
/>}
<div className="space-y-4">
<div>{components.slice(0, indexToView + 1)}</div>
{(showMoreButton || true) && wrapMoreButtonInSiteGrid
? <SiteGrid contentMain={renderMoreButton()} />
: renderMoreButton()}
</div>
</>;
}

View File

@ -37,6 +37,7 @@ export function MorePhotosRoot({
};
}
}}
wrapMoreButtonInSiteGrid
/>
);
}

View File

@ -1,5 +1,5 @@
import { FilmSimulation } from '@/simulation';
import { SHOW_EXIF_DATA } from '@/site/config';
import { HIGH_DENSITY_GRID, SHOW_EXIF_DATA } from '@/site/config';
import { ABSOLUTE_PATH_FOR_HOME_IMAGE } from '@/site/paths';
import { formatDateFromPostgresString } from '@/utility/date';
import {
@ -14,10 +14,11 @@ import type { Metadata } from 'next';
export const GENERATE_STATIC_PARAMS_LIMIT = 1000;
export const PHOTO_LOAD_MULTIPLE_ROOT =
export const INFINITE_SCROLL_MULTIPLE_ROOT =
process.env.NODE_ENV === 'development' ? 2 : 12;
export const PHOTO_LOAD_MULTIPLE_GRID =
process.env.NODE_ENV === 'development' ? 4 : 36;
export const INFINITE_SCROLL_MULTIPLE_GRID = HIGH_DENSITY_GRID
? process.env.NODE_ENV === 'development' ? 5 : 20
: process.env.NODE_ENV === 'development' ? 4 : 24;
export const GRID_THUMBNAILS_TO_SHOW_MAX = 12;