diff --git a/app/grid/page.tsx b/app/grid/page.tsx index 34a8d025..f6d8aad3 100644 --- a/app/grid/page.tsx +++ b/app/grid/page.tsx @@ -4,7 +4,7 @@ import { } from '@/photo'; import PhotosEmptyState from '@/photo/PhotosEmptyState'; import { Metadata } from 'next/types'; -import { getPhotoSidebarData } from '@/photo/data'; +import { getDataForCategories } from '@/category/data'; import { getPhotos, getPhotosMeta } from '@/photo/db/query'; import { cache } from 'react'; import PhotoGridPage from '@/photo/PhotoGridPage'; @@ -28,8 +28,8 @@ export default async function GridPage() { cameras, lenses, tags, - simulations, recipes, + simulations, focalLengths, ] = await Promise.all([ getPhotosCached() @@ -37,7 +37,7 @@ export default async function GridPage() { getPhotosMeta() .then(({ count }) => count) .catch(() => 0), - ...getPhotoSidebarData(), + ...getDataForCategories(), ]); return ( diff --git a/app/page.tsx b/app/page.tsx index 0a91f53a..39e9797d 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -8,7 +8,7 @@ import { Metadata } from 'next/types'; import { cache } from 'react'; import { getPhotos, getPhotosMeta } from '@/photo/db/query'; import { GRID_HOMEPAGE_ENABLED } from '@/app/config'; -import { getPhotoSidebarData } from '@/photo/data'; +import { getDataForCategories } from '@/category/data'; import PhotoGridPage from '@/photo/PhotoGridPage'; import PhotoFeedPage from '@/photo/PhotoFeedPage'; @@ -34,8 +34,8 @@ export default async function HomePage() { cameras, lenses, tags, - simulations, recipes, + simulations, focalLengths, ] = await Promise.all([ getPhotosCached() @@ -44,7 +44,7 @@ export default async function HomePage() { .then(({ count }) => count) .catch(() => 0), ...(GRID_HOMEPAGE_ENABLED - ? getPhotoSidebarData() + ? getDataForCategories() : [[], [], [], [], [], [], []]), ]); @@ -58,8 +58,8 @@ export default async function HomePage() { cameras, lenses, tags, - simulations, recipes, + simulations, focalLengths, }} /> diff --git a/src/admin/AdminRecipeTable.tsx b/src/admin/AdminRecipeTable.tsx index 7840a8fd..37f5232b 100644 --- a/src/admin/AdminRecipeTable.tsx +++ b/src/admin/AdminRecipeTable.tsx @@ -7,7 +7,7 @@ import { photoQuantityText } from '@/photo'; import EditButton from '@/admin/EditButton'; import { pathForAdminRecipeEdit } from '@/app/paths'; import { clsx } from 'clsx/lite'; -import { formatRecipe, Recipes, sortRecipesWithCount } from '@/recipe'; +import { formatRecipe, Recipes, sortRecipes } from '@/recipe'; import AdminRecipeBadge from './AdminRecipeBadge'; export default function AdminRecipeTable({ @@ -17,7 +17,7 @@ export default function AdminRecipeTable({ }) { return ( - {sortRecipesWithCount(recipes).map(({ recipe, count }) => + {sortRecipes(recipes).map(({ recipe, count }) =>
diff --git a/src/admin/AdminTagTable.tsx b/src/admin/AdminTagTable.tsx index 8f961344..759dc495 100644 --- a/src/admin/AdminTagTable.tsx +++ b/src/admin/AdminTagTable.tsx @@ -4,7 +4,7 @@ import AdminTable from '@/admin/AdminTable'; import { Fragment } from 'react'; import DeleteFormButton from '@/admin/DeleteFormButton'; import { photoQuantityText } from '@/photo'; -import { Tags, formatTag, sortTagsObject } from '@/tag'; +import { Tags, formatTag, sortTags } from '@/tag'; import EditButton from '@/admin/EditButton'; import { pathForAdminTagEdit } from '@/app/paths'; import { clsx } from 'clsx/lite'; @@ -17,7 +17,7 @@ export default function AdminTagTable({ }) { return ( - {sortTagsObject(tags).map(({ tag, count }) => + {sortTags(tags).map(({ tag, count }) =>
diff --git a/src/app/CommandK.tsx b/src/app/CommandK.tsx index 36e12f14..af1d9413 100644 --- a/src/app/CommandK.tsx +++ b/src/app/CommandK.tsx @@ -1,20 +1,8 @@ import CommandKClient from '@/components/cmdk/CommandKClient'; -import { - getPhotosMetaCached, - getUniqueCamerasCached, - getUniqueFilmSimulationsCached, - getUniqueLensesCached, - getUniqueRecipesCached, - getUniqueTagsCached, -} from '@/photo/cache'; +import { getPhotosMetaCached } from '@/photo/cache'; import { photoQuantityText } from '@/photo'; -import { - ADMIN_DEBUG_TOOLS_ENABLED, - SHOW_FILM_SIMULATIONS, - SHOW_RECIPES, -} from './config'; -import { getUniqueFocalLengths } from '@/photo/db/query'; -import { sortCategoryByCount } from '@/category'; +import { ADMIN_DEBUG_TOOLS_ENABLED } from './config'; +import { getDataForCategories } from '@/category/data'; export default async function CommandK() { const [ @@ -29,25 +17,16 @@ export default async function CommandK() { getPhotosMetaCached() .then(({ count }) => count) .catch(() => 0), - getUniqueCamerasCached().catch(() => []), - getUniqueLensesCached().catch(() => []), - getUniqueTagsCached().catch(() => []), - SHOW_RECIPES - ? getUniqueRecipesCached().catch(() => []) - : [], - SHOW_FILM_SIMULATIONS - ? getUniqueFilmSimulationsCached().catch(() => []) - : [], - getUniqueFocalLengths().catch(() => []), + ...getDataForCategories(), ]); return ; diff --git a/src/category/data.ts b/src/category/data.ts new file mode 100644 index 00000000..1cb56b35 --- /dev/null +++ b/src/category/data.ts @@ -0,0 +1,45 @@ +import { + getUniqueCameras, + getUniqueFilmSimulations, + getUniqueFocalLengths, + getUniqueLenses, + getUniqueRecipes, + getUniqueTags, +} from '@/photo/db/query'; +import { + SHOW_FILM_SIMULATIONS, + SHOW_FOCAL_LENGTHS, + SHOW_LENSES, + SHOW_RECIPES, +} from '@/app/config'; +import { sortTagsByCount } from '@/tag'; +import { sortCategoriesByCount } from '@/category'; + +export const getDataForCategories = () => [ + getUniqueCameras() + .then(sortCategoriesByCount) + .catch(() => []), + SHOW_LENSES + ? getUniqueLenses() + .then(sortCategoriesByCount) + .catch(() => []) + : [], + getUniqueTags() + .then(sortTagsByCount) + .catch(() => []), + SHOW_RECIPES + ? getUniqueRecipes() + .then(sortCategoriesByCount) + .catch(() => []) + : [], + SHOW_FILM_SIMULATIONS + ? getUniqueFilmSimulations() + .then(sortCategoriesByCount) + .catch(() => []) + : [], + SHOW_FOCAL_LENGTHS + ? getUniqueFocalLengths() + .then(sortCategoriesByCount) + .catch(() => []) + : [], +] as const; diff --git a/src/category/index.ts b/src/category/index.ts index 713f7b79..f12ea34d 100644 --- a/src/category/index.ts +++ b/src/category/index.ts @@ -71,3 +71,7 @@ export const sortCategoryByCount = ( a: { count: number }, b: { count: number }, ) => b.count - a.count; + +export const sortCategoriesByCount = ( + categories: T[], +) => categories.sort(sortCategoryByCount); diff --git a/src/photo/PhotoGridSidebar.tsx b/src/photo/PhotoGridSidebar.tsx index d379a87c..3bae4eae 100644 --- a/src/photo/PhotoGridSidebar.tsx +++ b/src/photo/PhotoGridSidebar.tsx @@ -24,7 +24,7 @@ import IconFilmSimulation from '@/components/icons/IconFilmSimulation'; import IconLens from '@/components/icons/IconLens'; import PhotoLens from '@/lens/PhotoLens'; import IconFocalLength from '@/components/icons/IconFocalLength'; -import { PhotoSetCategories, sortCategoryByCount } from '@/category'; +import { PhotoSetCategories } from '@/category'; import PhotoFocalLength from '@/focal/PhotoFocalLength'; export default function PhotoGridSidebar({ @@ -57,7 +57,6 @@ export default function PhotoGridSidebar({ className="translate-x-[0.5px]" />} items={cameras - .sort(sortCategoryByCount) .map(({ cameraKey, camera, count }) => } items={lenses - .sort(sortCategoryByCount) .map(({ lensKey, lens, count }) => } items={recipes - .sort(sortCategoryByCount) .map(({ recipe, count }) => } items={simulations - .sort(sortCategoryByCount) .map(({ simulation, count }) => [ - getUniqueCameras().catch(() => []), - SHOW_LENSES ? getUniqueLenses().catch(() => []) : [], - getUniqueTags().then(sortTagsObject).catch(() => []), - SHOW_FILM_SIMULATIONS ? getUniqueFilmSimulations().catch(() => []) : [], - SHOW_RECIPES ? getUniqueRecipes().catch(() => []) : [], - SHOW_FOCAL_LENGTHS ? getUniqueFocalLengths().catch(() => []) : [], -] as const; - -export const getPhotoSidebarDataCached = () => [ - getUniqueCamerasCached(), - SHOW_LENSES ? getUniqueLensesCached() : [], - getUniqueTagsCached().then(sortTagsObject), - SHOW_FILM_SIMULATIONS ? getUniqueFilmSimulationsCached() : [], - SHOW_RECIPES ? getUniqueRecipesCached() : [], - SHOW_FOCAL_LENGTHS ? getUniqueFocalLengthsCached() : [], -] as const; diff --git a/src/recipe/index.ts b/src/recipe/index.ts index dc0fff5a..832ca371 100644 --- a/src/recipe/index.ts +++ b/src/recipe/index.ts @@ -143,11 +143,11 @@ export const getPhotoWithRecipeFromPhotos = ( ? preferredPhoto : photos.find(photoHasRecipe); -export const sortRecipesWithCount = (recipes: Recipes = []) => +export const sortRecipes = (recipes: Recipes = []) => recipes.sort((a, b) => a.recipe.localeCompare(b.recipe)); export const convertRecipesForForm = (recipes: Recipes = []) => - sortRecipesWithCount(recipes) + sortRecipes(recipes) .map(({ recipe, count }) => ({ value: recipe, annotation: formatCount(count), diff --git a/src/simulation/index.ts b/src/simulation/index.ts index 5b283887..a370083d 100644 --- a/src/simulation/index.ts +++ b/src/simulation/index.ts @@ -22,6 +22,10 @@ export type FilmSimulationWithCount = { export type FilmSimulations = FilmSimulationWithCount[] +export const sortFilmSimulations = ( + simulations: FilmSimulations, +) => simulations.sort(sortFilmSimulationsWithCount); + export const sortFilmSimulationsWithCount = ( a: FilmSimulationWithCount, b: FilmSimulationWithCount, diff --git a/src/tag/index.ts b/src/tag/index.ts index bbce06e8..2111bcfa 100644 --- a/src/tag/index.ts +++ b/src/tag/index.ts @@ -50,25 +50,33 @@ export const titleForTag = ( export const shareTextForTag = (tag: string) => isTagFavs(tag) ? 'Favorite photos' : `Photos tagged '${formatTag(tag)}'`; -export const sortTags = ( +export const sortTagsArray = ( tags: string[], tagToExclude?: string, ) => tags .filter(tag => tag !== tagToExclude) .sort((a, b) => isTagFavs(a) ? -1 : a.localeCompare(b)); -export const sortTagsObject = ( +export const sortTags = ( tags: Tags, - tagToHide?: string, + tagToExclude?: string, ) => tags - .filter(({ tag }) => tag!== tagToHide) + .filter(({ tag }) => tag!== tagToExclude) .sort(({ tag: a }, { tag: b }) => isTagFavs(a) ? -1 : a.localeCompare(b)); +export const sortTagsByCount = ( + tags: Tags, + tagToExclude?: string, +) => tags + .filter(({ tag }) => tag !== tagToExclude) + .sort(({ tag: tagA, count: a }, { count: b }) => + isTagFavs(tagA) ? -1 : b - a); + export const sortTagsWithoutFavs = (tags: string[]) => - sortTags(tags, TAG_FAVS); + sortTagsArray(tags, TAG_FAVS); export const sortTagsObjectWithoutFavs = (tags: Tags) => - sortTagsObject(tags, TAG_FAVS); + sortTags(tags, TAG_FAVS); export const descriptionForTaggedPhotos = ( photos: Photo[] = [],