Limit grid thumbnails after photo detail

This commit is contained in:
Sam Becker 2023-09-10 12:02:49 -05:00
parent a1f01788ae
commit 6766bf6a7f
3 changed files with 40 additions and 5 deletions

View File

@ -3,6 +3,7 @@ import AnimateItems from '@/components/AnimateItems';
import PhotoLinks from '@/photo/PhotoLinks'; import PhotoLinks from '@/photo/PhotoLinks';
import SiteGrid from '@/components/SiteGrid'; import SiteGrid from '@/components/SiteGrid';
import { import {
PHOTOS_PER_REQUEST,
ogImageDescriptionForPhoto, ogImageDescriptionForPhoto,
ogImageUrlForPhoto, ogImageUrlForPhoto,
titleForPhoto, titleForPhoto,
@ -12,7 +13,11 @@ import PhotoLarge from '@/photo/PhotoLarge';
import { cc } from '@/utility/css'; import { cc } from '@/utility/css';
import { Metadata } from 'next'; import { Metadata } from 'next';
import { BASE_URL } from '@/site/config'; import { BASE_URL } from '@/site/config';
import { getPhoto, getPhotos } from '@/services/postgres'; import {
getPhoto,
getPhotosTakenAfterPhotoInclusive,
getPhotosTakenBeforePhoto,
} from '@/services/postgres';
import { redirect } from 'next/navigation'; import { redirect } from 'next/navigation';
export const runtime = 'edge'; export const runtime = 'edge';
@ -58,7 +63,12 @@ export default async function PhotoPage({
if (!photo) { redirect('/'); } if (!photo) { redirect('/'); }
const photos = await getPhotos(); const photosBefore = await getPhotosTakenBeforePhoto(photo, 1);
const photosAfter = await getPhotosTakenAfterPhotoInclusive(
photo,
PHOTOS_PER_REQUEST + 1,
);
const photos = photosBefore.concat(photosAfter);
return <> return <>
{children} {children}
@ -75,8 +85,7 @@ export default async function PhotoPage({
<SiteGrid <SiteGrid
sideFirstOnMobile sideFirstOnMobile
contentMain={<PhotoGrid contentMain={<PhotoGrid
photos={photos} photos={photosAfter.slice(1)}
selectedPhoto={photo}
animateOnFirstLoadOnly animateOnFirstLoadOnly
staggerOnFirstLoadOnly staggerOnFirstLoadOnly
/>} />}

View File

@ -11,6 +11,8 @@ import camelcaseKeys from 'camelcase-keys';
import { Metadata } from 'next'; import { Metadata } from 'next';
import short from 'short-uuid'; import short from 'short-uuid';
export const PHOTOS_PER_REQUEST = 12;
const translator = short(); const translator = short();
// Core EXIF data // Core EXIF data
@ -122,7 +124,7 @@ export const getNextPhoto = (photo: Photo, photos: Photo[]) => {
export const getPhotosLimitForQuery = ( export const getPhotosLimitForQuery = (
query?: string, query?: string,
photosPerRequest = 12, photosPerRequest = PHOTOS_PER_REQUEST,
) => { ) => {
const offsetInt = parseInt(query ?? '0'); const offsetInt = parseInt(query ?? '0');
const offset = (Number.isNaN(offsetInt) ? 0 : offsetInt); const offset = (Number.isNaN(offsetInt) ? 0 : offsetInt);

View File

@ -186,6 +186,30 @@ export const getPhotos = async (
return photos; return photos;
}; };
export const getPhotosTakenAfterPhotoInclusive = (
photo: Photo,
limit?: number,
) =>
sql<PhotoDb>`
SELECT * FROM photos
WHERE taken_at <= ${photo.takenAt.toISOString()}
ORDER BY taken_at DESC
LIMIT ${limit}
`
.then(({ rows }) => rows.map(parsePhotoFromDb));
export const getPhotosTakenBeforePhoto = (
photo: Photo,
limit?: number,
) =>
sql<PhotoDb>`
SELECT * FROM photos
WHERE taken_at > ${photo.takenAt.toISOString()}
ORDER BY taken_at ASC
LIMIT ${limit}
`
.then(({ rows }) => rows.map(parsePhotoFromDb));
export const getPhotosCount = async () => sql` export const getPhotosCount = async () => sql`
SELECT COUNT(*) FROM photos SELECT COUNT(*) FROM photos
`.then(({ rows }) => parseInt(rows[0].count, 10)); `.then(({ rows }) => parseInt(rows[0].count, 10));