diff --git a/src/app/film/[simulation]/[photoId]/layout.tsx b/src/app/film/[simulation]/[photoId]/layout.tsx index 663d68bb..30367832 100644 --- a/src/app/film/[simulation]/[photoId]/layout.tsx +++ b/src/app/film/[simulation]/[photoId]/layout.tsx @@ -59,8 +59,7 @@ export default async function PhotoFilmSimulationPage({ const [ photos, - count, - dateRange, + { count, dateRange }, ] = await getPhotosFilmSimulationDataCached({ simulation }); return <> diff --git a/src/app/film/[simulation]/page.tsx b/src/app/film/[simulation]/page.tsx index c3aaad19..28528515 100644 --- a/src/app/film/[simulation]/page.tsx +++ b/src/app/film/[simulation]/page.tsx @@ -17,8 +17,7 @@ export async function generateMetadata({ }: FilmSimulationProps): Promise { const [ photos, - count, - dateRange, + { count, dateRange }, ] = await getPhotosFilmSimulationDataCached({ simulation, limit: GRID_THUMBNAILS_TO_SHOW_MAX, diff --git a/src/app/film/[simulation]/share/page.tsx b/src/app/film/[simulation]/share/page.tsx index 7a91c33e..b03b30b3 100644 --- a/src/app/film/[simulation]/share/page.tsx +++ b/src/app/film/[simulation]/share/page.tsx @@ -18,8 +18,7 @@ export async function generateMetadata({ }: FilmSimulationProps): Promise { const [ photos, - count, - dateRange, + { count, dateRange }, ] = await getPhotosFilmSimulationDataCached({ simulation, limit: GRID_THUMBNAILS_TO_SHOW_MAX, diff --git a/src/app/shot-on/[camera]/[photoId]/layout.tsx b/src/app/shot-on/[camera]/[photoId]/layout.tsx index 8ddb9514..e102256d 100644 --- a/src/app/shot-on/[camera]/[photoId]/layout.tsx +++ b/src/app/shot-on/[camera]/[photoId]/layout.tsx @@ -65,8 +65,7 @@ export default async function PhotoCameraPage({ const [ photos, - count, - dateRange, + { count, dateRange }, ] = await getPhotosCameraDataCached({ camera }); return <> diff --git a/src/app/shot-on/[camera]/page.tsx b/src/app/shot-on/[camera]/page.tsx index 49f54711..e528a6ce 100644 --- a/src/app/shot-on/[camera]/page.tsx +++ b/src/app/shot-on/[camera]/page.tsx @@ -20,8 +20,7 @@ export async function generateMetadata({ const [ photos, - count, - dateRange, + { count, dateRange }, ] = await getPhotosCameraDataCached({ camera, limit: GRID_THUMBNAILS_TO_SHOW_MAX, diff --git a/src/app/shot-on/[camera]/share/page.tsx b/src/app/shot-on/[camera]/share/page.tsx index 5bff9080..f86568bb 100644 --- a/src/app/shot-on/[camera]/share/page.tsx +++ b/src/app/shot-on/[camera]/share/page.tsx @@ -21,8 +21,7 @@ export async function generateMetadata({ const [ photos, - count, - dateRange, + { count, dateRange }, ] = await getPhotosCameraDataCached({ camera, limit: GRID_THUMBNAILS_TO_SHOW_MAX, diff --git a/src/camera/data.ts b/src/camera/data.ts index 5e8ea18c..38ea8bca 100644 --- a/src/camera/data.ts +++ b/src/camera/data.ts @@ -5,8 +5,7 @@ import { import { Camera } from '.'; import { getPhotosCached, - getPhotosCameraCountCached, - getPhotosCameraDateRangeCached, + getPhotosCameraMetaCached, } from '@/photo/cache'; import { pathForCamera } from '@/site/paths'; @@ -19,8 +18,7 @@ export const getPhotosCameraDataCached = ({ }) => Promise.all([ getPhotosCached({ camera, limit }), - getPhotosCameraCountCached(camera), - getPhotosCameraDateRangeCached(camera), + getPhotosCameraMetaCached(camera), ]); export const getPhotosCameraDataCachedWithPagination = async ({ @@ -34,7 +32,7 @@ export const getPhotosCameraDataCachedWithPagination = async ({ }) => { const { offset, limit } = getPaginationFromSearchParams(searchParams); - const [photos, count, dateRange] = + const [photos, { count, dateRange }] = await getPhotosCameraDataCached({ camera, limit: limitProp ?? limit, diff --git a/src/photo/cache.ts b/src/photo/cache.ts index 6476c8d7..7271c2c1 100644 --- a/src/photo/cache.ts +++ b/src/photo/cache.ts @@ -9,16 +9,14 @@ import { getPhoto, getPhotos, getPhotosCount, - getPhotosCameraCount, getPhotosCountIncludingHidden, getUniqueCameras, getUniqueTags, getPhotosTagMeta, - getPhotosCameraDateRange, + getPhotosCameraMeta, getUniqueTagsHidden, getUniqueFilmSimulations, - getPhotosFilmSimulationDateRange, - getPhotosFilmSimulationCount, + getPhotosFilmSimulationMeta, getPhotosDateRange, getPhotosNearId, } from '@/services/vercel-postgres'; @@ -161,35 +159,21 @@ export const getPhotosCountIncludingHiddenCached = [KEY_PHOTOS, KEY_COUNT, KEY_HIDDEN], ); -export const getPhotosCameraCountCached = ( - ...args: Parameters -) => - unstable_cache( - getPhotosCameraCount, - [KEY_PHOTOS, KEY_COUNT, createCameraKey(...args)], - )(...args); - -export const getPhotosFilmSimulationCountCached = - unstable_cache( - getPhotosFilmSimulationCount, - [KEY_PHOTOS, KEY_FILM_SIMULATIONS, KEY_COUNT], - ); - export const getPhotosTagMetaCached = unstable_cache( getPhotosTagMeta, [KEY_PHOTOS, KEY_TAGS, KEY_DATE_RANGE], ); -export const getPhotosCameraDateRangeCached = +export const getPhotosCameraMetaCached = unstable_cache( - getPhotosCameraDateRange, + getPhotosCameraMeta, [KEY_PHOTOS, KEY_CAMERAS, KEY_DATE_RANGE], ); -export const getPhotosFilmSimulationDateRangeCached = +export const getPhotosFilmSimulationMetaCached = unstable_cache( - getPhotosFilmSimulationDateRange, + getPhotosFilmSimulationMeta, [KEY_PHOTOS, KEY_FILM_SIMULATIONS, KEY_DATE_RANGE], ); diff --git a/src/services/vercel-postgres.ts b/src/services/vercel-postgres.ts index 5f0c7e24..eeabd78a 100644 --- a/src/services/vercel-postgres.ts +++ b/src/services/vercel-postgres.ts @@ -165,22 +165,6 @@ const sqlGetPhotosCountIncludingHidden = async () => sql` SELECT COUNT(*) FROM photos `.then(({ rows }) => parseInt(rows[0].count, 10)); -const sqlGetPhotosCameraCount = async (camera: Camera) => sql` - SELECT COUNT(*) FROM photos - WHERE - LOWER(make)=${parameterize(camera.make, true)} AND - LOWER(REPLACE(model, ' ', '-'))=${parameterize(camera.model, true)} AND - hidden IS NOT TRUE -`.then(({ rows }) => parseInt(rows[0].count, 10)); - -const sqlGetPhotosFilmSimulationCount = async ( - simulation: FilmSimulation, -) => sql` - SELECT COUNT(*) FROM photos - WHERE film_simulation=${simulation} AND - hidden IS NOT TRUE -`.then(({ rows }) => parseInt(rows[0].count, 10)); - const sqlGetPhotosDateRange = async () => sql` SELECT MIN(taken_at_naive) as start, MAX(taken_at_naive) as end FROM photos @@ -201,27 +185,33 @@ const sqlGetPhotosTagMeta = async (tag: string) => sql` : undefined, })); -const sqlGetPhotosCameraDateRange = async (camera: Camera) => sql` - SELECT MIN(taken_at_naive) as start, MAX(taken_at_naive) as end +const sqlGetPhotosCameraMeta = async (camera: Camera) => sql` + SELECT COUNT(*), MIN(taken_at_naive) as start, MAX(taken_at_naive) as end FROM photos WHERE LOWER(make)=${parameterize(camera.make, true)} AND LOWER(REPLACE(model, ' ', '-'))=${parameterize(camera.model, true)} AND hidden IS NOT TRUE -`.then(({ rows }) => rows[0]?.start && rows[0]?.end - ? rows[0] as PhotoDateRange - : undefined); +`.then(({ rows }) => ({ + count: parseInt(rows[0].count, 10), + ...rows[0]?.start && rows[0]?.end + ? { dateRange: rows[0] as PhotoDateRange } + : undefined, + })); -const sqlGetPhotosFilmSimulationDateRange = async ( +const sqlGetPhotosFilmSimulationMeta = async ( simulation: FilmSimulation, ) => sql` - SELECT MIN(taken_at_naive) as start, MAX(taken_at_naive) as end + SELECT COUNT(*), MIN(taken_at_naive) as start, MAX(taken_at_naive) as end FROM photos WHERE film_simulation=${simulation} AND hidden IS NOT TRUE -`.then(({ rows }) => rows[0]?.start && rows[0]?.end - ? rows[0] as PhotoDateRange - : undefined); +`.then(({ rows }) => ({ + count: parseInt(rows[0].count, 10), + ...rows[0]?.start && rows[0]?.end + ? { dateRange: rows[0] as PhotoDateRange } + : undefined, + })); const sqlGetUniqueTags = async () => sql` SELECT DISTINCT unnest(tags) as tag, COUNT(*) @@ -467,27 +457,17 @@ export const getPhotosTagMeta = (tag: string) => // CAMERAS export const getUniqueCameras = () => safelyQueryPhotos(sqlGetUniqueCameras, 'getUniqueCameras'); -export const getPhotosCameraDateRange = (camera: Camera) => +export const getPhotosCameraMeta = (camera: Camera) => safelyQueryPhotos( - () => sqlGetPhotosCameraDateRange(camera), - 'getPhotosCameraDateRange', - ); -export const getPhotosCameraCount = (camera: Camera) => - safelyQueryPhotos( - () => sqlGetPhotosCameraCount(camera), - 'getPhotosCameraCount', + () => sqlGetPhotosCameraMeta(camera), + 'getPhotosCameraMeta', ); // FILM SIMULATIONS export const getUniqueFilmSimulations = () => safelyQueryPhotos(sqlGetUniqueFilmSimulations, 'getUniqueFilmSimulations'); -export const getPhotosFilmSimulationDateRange = +export const getPhotosFilmSimulationMeta = (simulation: FilmSimulation) => safelyQueryPhotos( - () => sqlGetPhotosFilmSimulationDateRange(simulation), - 'getPhotosFilmSimulationDateRange', - ); -export const getPhotosFilmSimulationCount = (simulation: FilmSimulation) => - safelyQueryPhotos( - () => sqlGetPhotosFilmSimulationCount(simulation), - 'getPhotosFilmSimulationCount', + () => sqlGetPhotosFilmSimulationMeta(simulation), + 'getPhotosFilmSimulationMeta', ); diff --git a/src/simulation/data.ts b/src/simulation/data.ts index a84ac22f..99fa1946 100644 --- a/src/simulation/data.ts +++ b/src/simulation/data.ts @@ -1,7 +1,6 @@ import { getPhotosCached, - getPhotosFilmSimulationCountCached, - getPhotosFilmSimulationDateRangeCached, + getPhotosFilmSimulationMetaCached, } from '@/photo/cache'; import { PaginationSearchParams, @@ -19,8 +18,7 @@ export const getPhotosFilmSimulationDataCached = ({ }) => Promise.all([ getPhotosCached({ simulation, limit }), - getPhotosFilmSimulationCountCached(simulation), - getPhotosFilmSimulationDateRangeCached(simulation), + getPhotosFilmSimulationMetaCached(simulation), ]); export const getPhotosFilmSimulationDataCachedWithPagination = async ({ @@ -34,7 +32,7 @@ export const getPhotosFilmSimulationDataCachedWithPagination = async ({ }) => { const { offset, limit } = getPaginationFromSearchParams(searchParams); - const [photos, count, dateRange] = + const [photos, { count, dateRange }] = await getPhotosFilmSimulationDataCached({ simulation, limit: limitProp ?? limit, diff --git a/src/tag/index.ts b/src/tag/index.ts index ea0b5d28..ec42f22c 100644 --- a/src/tag/index.ts +++ b/src/tag/index.ts @@ -13,13 +13,10 @@ import { capitalizeWords, convertStringToArray } from '@/utility/string'; export const TAG_FAVS = 'favs'; -export type TagWithMeta = { +export type TagsWithMeta = { tag: string count: number - dataRange?: PhotoDateRange -} - -export type TagsWithMeta = Omit[] +}[] export const formatTag = (tag?: string) => capitalizeWords(tag?.replaceAll('-', ' '));