Cache category counts
This commit is contained in:
parent
69ec607e37
commit
6ed94c4857
@ -1,42 +1,6 @@
|
||||
'use server';
|
||||
|
||||
import { createLensKey } from '@/lens';
|
||||
import { getDataForCategories } from './data';
|
||||
import { getCountsForCategoriesCached } from './cache';
|
||||
|
||||
export const getCountsForCategoriesAction = async () => {
|
||||
const [
|
||||
cameras,
|
||||
lenses,
|
||||
tags,
|
||||
recipes,
|
||||
filmSimulations,
|
||||
focalLengths,
|
||||
] = await Promise.all(getDataForCategories());
|
||||
|
||||
return {
|
||||
cameras: cameras.reduce((acc, camera) => {
|
||||
acc[camera.cameraKey] = camera.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
lenses: lenses.reduce((acc, lens) => {
|
||||
acc[createLensKey(lens.lens)] = lens.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
tags: tags.reduce((acc, tag) => {
|
||||
acc[tag.tag] = tag.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
recipes: recipes.reduce((acc, recipe) => {
|
||||
acc[recipe.recipe] = recipe.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
filmSimulations: filmSimulations.reduce((acc, filmSimulation) => {
|
||||
acc[filmSimulation.simulation] = filmSimulation.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
focalLengths: focalLengths.reduce((acc, focalLength) => {
|
||||
acc[focalLength.focal] = focalLength.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
};
|
||||
};
|
||||
export const getCountsForCategoriesCachedAction = async () =>
|
||||
getCountsForCategoriesCached();
|
||||
|
||||
9
src/category/cache.ts
Normal file
9
src/category/cache.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { unstable_cache } from 'next/cache';
|
||||
import { getCountsForCategories } from './data';
|
||||
import { KEY_PHOTOS } from '@/photo/cache';
|
||||
|
||||
export const getCountsForCategoriesCached = () =>
|
||||
unstable_cache(
|
||||
getCountsForCategories,
|
||||
[KEY_PHOTOS],
|
||||
)();
|
||||
@ -14,6 +14,7 @@ import {
|
||||
SHOW_CAMERAS,
|
||||
SHOW_TAGS,
|
||||
} from '@/app/config';
|
||||
import { createLensKey } from '@/lens';
|
||||
import { sortTagsByCount } from '@/tag';
|
||||
import { sortCategoriesByCount } from '@/category';
|
||||
import { sortFocalLengths } from '@/focal';
|
||||
@ -50,3 +51,41 @@ export const getDataForCategories = () => [
|
||||
.catch(() => [])
|
||||
: [],
|
||||
] as const;
|
||||
|
||||
export const getCountsForCategories = async () => {
|
||||
const [
|
||||
cameras,
|
||||
lenses,
|
||||
tags,
|
||||
recipes,
|
||||
filmSimulations,
|
||||
focalLengths,
|
||||
] = await Promise.all(getDataForCategories());
|
||||
|
||||
return {
|
||||
cameras: cameras.reduce((acc, camera) => {
|
||||
acc[camera.cameraKey] = camera.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
lenses: lenses.reduce((acc, lens) => {
|
||||
acc[createLensKey(lens.lens)] = lens.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
tags: tags.reduce((acc, tag) => {
|
||||
acc[tag.tag] = tag.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
recipes: recipes.reduce((acc, recipe) => {
|
||||
acc[recipe.recipe] = recipe.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
filmSimulations: filmSimulations.reduce((acc, filmSimulation) => {
|
||||
acc[filmSimulation.simulation] = filmSimulation.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
focalLengths: focalLengths.reduce((acc, focalLength) => {
|
||||
acc[focalLength.focal] = focalLength.count;
|
||||
return acc;
|
||||
}, {} as Record<string, number>),
|
||||
};
|
||||
};
|
||||
|
||||
@ -39,7 +39,7 @@ import {
|
||||
import { createLensKey } from '@/lens';
|
||||
|
||||
// Table key
|
||||
const KEY_PHOTOS = 'photos';
|
||||
export const KEY_PHOTOS = 'photos';
|
||||
const KEY_PHOTO = 'photo';
|
||||
// Field keys
|
||||
const KEY_CAMERAS = 'cameras';
|
||||
|
||||
@ -11,7 +11,7 @@ import { InsightsIndicatorStatus } from '@/admin/insights';
|
||||
import { INITIAL_UPLOAD_STATE, UploadState } from '@/admin/upload';
|
||||
import { AdminData } from '@/admin/actions';
|
||||
import { RecipeProps } from '@/recipe';
|
||||
import { getCountsForCategoriesAction } from '@/category/actions';
|
||||
import { getCountsForCategoriesCachedAction } from '@/category/actions';
|
||||
export type AppStateContext = {
|
||||
// CORE
|
||||
previousPathname?: string
|
||||
@ -25,7 +25,7 @@ export type AppStateContext = {
|
||||
shouldRespondToKeyboardCommands?: boolean
|
||||
setShouldRespondToKeyboardCommands?: Dispatch<SetStateAction<boolean>>
|
||||
categoriesWithCounts?:
|
||||
Awaited<ReturnType<typeof getCountsForCategoriesAction>>
|
||||
Awaited<ReturnType<typeof getCountsForCategoriesCachedAction>>
|
||||
// MODAL
|
||||
isCommandKOpen?: boolean
|
||||
setIsCommandKOpen?: Dispatch<SetStateAction<boolean>>
|
||||
|
||||
@ -24,7 +24,7 @@ import { useRouter, usePathname } from 'next/navigation';
|
||||
import { isPathAdmin, PATH_SIGN_IN } from '@/app/paths';
|
||||
import { INITIAL_UPLOAD_STATE, UploadState } from '@/admin/upload';
|
||||
import { RecipeProps } from '@/recipe';
|
||||
import { getCountsForCategoriesAction } from '@/category/actions';
|
||||
import { getCountsForCategoriesCachedAction } from '@/category/actions';
|
||||
|
||||
export default function AppStateProvider({
|
||||
children,
|
||||
@ -93,7 +93,7 @@ export default function AppStateProvider({
|
||||
|
||||
const { data: categoriesWithCounts } = useSWR(
|
||||
'getDataForCategories',
|
||||
getCountsForCategoriesAction,
|
||||
getCountsForCategoriesCachedAction,
|
||||
);
|
||||
|
||||
const {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user