From 038c91e652a5598693358819299c8407c5378e80 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 21 Oct 2024 16:10:11 +0530 Subject: [PATCH] Move --- web/apps/photos/src/pages/gallery.tsx | 12 +-------- web/apps/photos/src/services/export/index.ts | 10 +++---- web/apps/photos/src/utils/collection.ts | 22 +-------------- .../new/photos/components/gallery/reducer.ts | 22 ++++++++++++++- .../new/photos/services/collection/index.ts | 27 +++++++++++++++++++ 5 files changed, 55 insertions(+), 38 deletions(-) diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx index fb1631019a..7f5742d69d 100644 --- a/web/apps/photos/src/pages/gallery.tsx +++ b/web/apps/photos/src/pages/gallery.tsx @@ -137,7 +137,6 @@ import { import { checkSubscriptionPurchase } from "utils/billing"; import { COLLECTION_OPS_TYPE, - constructCollectionNameMap, getSelectedCollection, handleCollectionOps, } from "utils/collection"; @@ -339,6 +338,7 @@ export default function Gallery() { const archivedCollectionIDs = state.archivedCollectionIDs; const defaultHiddenCollectionIDs = state.defaultHiddenCollectionIDs; const hiddenFileIDs = state.hiddenFileIDs; + const collectionNameMap = state.allCollectionNameByID; const collectionSummaries = state.collectionSummaries; const hiddenCollectionSummaries = state.hiddenCollectionSummaries; @@ -750,16 +750,6 @@ export default function Gallery() { return constructFileToCollectionMap(files); }, [files]); - const collectionNameMap = useMemo(() => { - if (!collections || !hiddenCollections) { - return new Map(); - } - return constructCollectionNameMap([ - ...collections, - ...hiddenCollections, - ]); - }, [collections, hiddenCollections]); - const showSessionExpiredMessage = () => { setDialogMessage(getSessionExpiredMessage(logout)); }; diff --git a/web/apps/photos/src/services/export/index.ts b/web/apps/photos/src/services/export/index.ts index ced1dd3cb4..e31e5d0494 100644 --- a/web/apps/photos/src/services/export/index.ts +++ b/web/apps/photos/src/services/export/index.ts @@ -9,6 +9,10 @@ import { } from "@/media/file-metadata"; import { FileType } from "@/media/file-type"; import { decodeLivePhoto } from "@/media/live-photo"; +import { + createCollectionNameByID, + getCollectionUserFacingName, +} from "@/new/photos/services/collection"; import downloadManager from "@/new/photos/services/download"; import { updateExifIfNeededAndPossible } from "@/new/photos/services/exif-update"; import { @@ -34,10 +38,6 @@ import { ExportUIUpdaters, FileExportNames, } from "types/export"; -import { - constructCollectionNameMap, - getCollectionUserFacingName, -} from "utils/collection"; import { getAllLocalCollections } from "../collectionService"; import { migrateExport } from "./migration"; @@ -330,7 +330,7 @@ class ExportService { convertCollectionIDExportNameObjectToMap( exportRecord.collectionExportNames, ); - const collectionIDNameMap = constructCollectionNameMap(collections); + const collectionIDNameMap = createCollectionNameByID(collections); const renamedCollections = getRenamedExportedCollections( collections, diff --git a/web/apps/photos/src/utils/collection.ts b/web/apps/photos/src/utils/collection.ts index 661ec371f0..92d49a2eae 100644 --- a/web/apps/photos/src/utils/collection.ts +++ b/web/apps/photos/src/utils/collection.ts @@ -11,8 +11,8 @@ import { import { EnteFile } from "@/media/file"; import { ItemVisibility } from "@/media/file-metadata"; import { + DEFAULT_HIDDEN_COLLECTION_USER_FACING_NAME, findDefaultHiddenCollectionIDs, - isDefaultHiddenCollection, isHiddenCollection, isIncomingShare, } from "@/new/photos/services/collection"; @@ -391,26 +391,6 @@ export function getHiddenCollections(collections: Collection[]): Collection[] { return collections.filter((collection) => isHiddenCollection(collection)); } -export function constructCollectionNameMap( - collections: Collection[], -): Map { - return new Map( - (collections ?? []).map((collection) => [ - collection.id, - getCollectionUserFacingName(collection), - ]), - ); -} - -const DEFAULT_HIDDEN_COLLECTION_USER_FACING_NAME = "Hidden"; - -export const getCollectionUserFacingName = (collection: Collection) => { - if (isDefaultHiddenCollection(collection)) { - return DEFAULT_HIDDEN_COLLECTION_USER_FACING_NAME; - } - return collection.name; -}; - export const getOrCreateAlbum = async ( albumName: string, existingCollections: Collection[], diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.ts index 4a0ccd3917..ffea09257d 100644 --- a/web/packages/new/photos/components/gallery/reducer.ts +++ b/web/packages/new/photos/components/gallery/reducer.ts @@ -5,7 +5,10 @@ import { } from "@/media/collection"; import type { EnteFile } from "@/media/file"; import { mergeMetadata } from "@/media/file"; -import { isHiddenCollection } from "@/new/photos/services/collection"; +import { + createCollectionNameByID, + isHiddenCollection, +} from "@/new/photos/services/collection"; import { splitByPredicate } from "@/utils/array"; import { ensure } from "@/utils/ensure"; import type { User } from "@ente/shared/user/types"; @@ -109,6 +112,13 @@ export interface GalleryState { * File IDs of all the files that the user has marked as a favorite. */ favoriteFileIDs: Set; + /** + * User visible collection names indexed by collection IDs for fast lookup. + * + * This map will contain entries for all (both normal and hidden) + * collections. + */ + allCollectionNameByID: Map; /*--< Derived UI state >--*/ @@ -183,6 +193,7 @@ const initialGalleryState: GalleryState = { defaultHiddenCollectionIDs: new Set(), hiddenFileIDs: new Set(), favoriteFileIDs: new Set(), + allCollectionNameByID: new Map(), collectionSummaries: new Map(), hiddenCollectionSummaries: new Map(), filteredData: [], @@ -222,6 +233,9 @@ const galleryReducer: React.Reducer = ( collections, action.files, ), + allCollectionNameByID: createCollectionNameByID( + action.allCollections, + ), collectionSummaries: deriveCollectionSummaries( action.user, collections, @@ -255,6 +269,9 @@ const galleryReducer: React.Reducer = ( action.collections, state.files, ), + allCollectionNameByID: createCollectionNameByID( + action.collections.concat(state.hiddenCollections), + ), collectionSummaries: deriveCollectionSummaries( ensure(state.user), action.collections, @@ -280,6 +297,9 @@ const galleryReducer: React.Reducer = ( action.collections, state.files, ), + allCollectionNameByID: createCollectionNameByID( + action.collections.concat(action.hiddenCollections), + ), collectionSummaries: deriveCollectionSummaries( ensure(state.user), action.collections, diff --git a/web/packages/new/photos/services/collection/index.ts b/web/packages/new/photos/services/collection/index.ts index 3df8bfdca8..2f87512f6f 100644 --- a/web/packages/new/photos/services/collection/index.ts +++ b/web/packages/new/photos/services/collection/index.ts @@ -41,3 +41,30 @@ export const isHiddenCollection = (collection: Collection) => // TODO: Need to audit the types // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition collection.magicMetadata?.data.visibility === ItemVisibility.hidden; + +// TODO: Does this need localizations? +export const DEFAULT_HIDDEN_COLLECTION_USER_FACING_NAME = "Hidden"; + +/** + * Return the "user facing" name of the given collection. + * + * Usually this is the same as the collection name, but it might be a different + * string for special collections like default hidden collections. + */ +export const getCollectionUserFacingName = (collection: Collection) => { + if (isDefaultHiddenCollection(collection)) { + return DEFAULT_HIDDEN_COLLECTION_USER_FACING_NAME; + } + return collection.name; +}; + +/** + * Return a map of the (user-facing) collection name, indexed by collection ID. + */ +export const createCollectionNameByID = (allCollections: Collection[]) => + new Map( + allCollections.map((collection) => [ + collection.id, + getCollectionUserFacingName(collection), + ]), + );