Vercel/app/focal/[focal]/[photoId]/page.tsx
2025-02-17 17:54:00 -06:00

89 lines
2.1 KiB
TypeScript

import {
RELATED_GRID_PHOTOS_TO_SHOW,
descriptionForPhoto,
titleForPhoto,
} from '@/photo';
import { Metadata } from 'next/types';
import { redirect } from 'next/navigation';
import {
PATH_ROOT,
absolutePathForPhoto,
absolutePathForPhotoImage,
} from '@/app/paths';
import PhotoDetailPage from '@/photo/PhotoDetailPage';
import { getPhotosNearIdCached } from '@/photo/cache';
import { cache } from 'react';
import { getPhotosMeta } from '@/photo/db/query';
import { getFocalLengthFromString } from '@/focal';
const getPhotosNearIdCachedCached = cache((photoId: string, focal: number) =>
getPhotosNearIdCached(
photoId,
{ focal, limit: RELATED_GRID_PHOTOS_TO_SHOW + 2 },
));
interface PhotoFocalLengthProps {
params: Promise<{ photoId: string, focal: string }>
}
export async function generateMetadata({
params,
}: PhotoFocalLengthProps): Promise<Metadata> {
const { photoId, focal: focalString } = await params;
const focal = getFocalLengthFromString(focalString);
const { photo } = await getPhotosNearIdCachedCached(photoId, focal);
if (!photo) { return {}; }
const title = titleForPhoto(photo);
const description = descriptionForPhoto(photo);
const images = absolutePathForPhotoImage(photo);
const url = absolutePathForPhoto({ photo, focal });
return {
title,
description,
openGraph: {
title,
images,
description,
url,
},
twitter: {
title,
description,
images,
card: 'summary_large_image',
},
};
}
export default async function PhotoFocalLengthPage({
params,
}: PhotoFocalLengthProps) {
const { photoId, focal: focalString } = await params;
const focal = getFocalLengthFromString(focalString);
const { photo, photos, photosGrid, indexNumber } =
await getPhotosNearIdCachedCached(photoId, focal);
if (!photo) { redirect(PATH_ROOT); }
const { count, dateRange } = await getPhotosMeta({ focal });
return (
<PhotoDetailPage {...{
photo,
photos,
photosGrid,
focal,
indexNumber,
count,
dateRange,
}} />
);
}