From 4da2f32e71f8c075ed952bbf23e9638c1f2b38fc Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 21 Oct 2024 16:22:29 +0530 Subject: [PATCH] Move --- web/apps/photos/src/pages/deduplicate.tsx | 5 +++-- web/apps/photos/src/pages/gallery.tsx | 12 ++---------- web/apps/photos/src/utils/file/index.ts | 11 ----------- .../new/photos/components/gallery/reducer.ts | 10 ++++++++++ web/packages/new/photos/services/file.ts | 13 +++++++++++++ 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/web/apps/photos/src/pages/deduplicate.tsx b/web/apps/photos/src/pages/deduplicate.tsx index 242b41ceb9..67c594ccce 100644 --- a/web/apps/photos/src/pages/deduplicate.tsx +++ b/web/apps/photos/src/pages/deduplicate.tsx @@ -1,6 +1,7 @@ import { stashRedirect } from "@/accounts/services/redirect"; import { ActivityIndicator } from "@/base/components/mui/ActivityIndicator"; import { ALL_SECTION } from "@/new/photos/services/collection"; +import { createFileCollectionIDs } from "@/new/photos/services/file"; import { getLocalFiles } from "@/new/photos/services/files"; import { AppContext } from "@/new/photos/types/context"; import { VerticallyCentered } from "@ente/shared/components/Container"; @@ -28,7 +29,7 @@ import { DefaultDeduplicateContext, } from "types/deduplicate"; import { SelectedState } from "types/gallery"; -import { constructFileToCollectionMap, getSelectedFiles } from "utils/file"; +import { getSelectedFiles } from "utils/file"; export const DeduplicateContext = createContext( DefaultDeduplicateContext, @@ -114,7 +115,7 @@ export default function Deduplicate() { }, [duplicates]); const fileToCollectionsMap = useMemoSingleThreaded(() => { - return constructFileToCollectionMap(duplicateFiles); + return createFileCollectionIDs(duplicateFiles); }, [duplicateFiles]); const deleteFileHelper = async () => { diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx index 7f5742d69d..2970f7cbe7 100644 --- a/web/apps/photos/src/pages/gallery.tsx +++ b/web/apps/photos/src/pages/gallery.tsx @@ -140,12 +140,7 @@ import { getSelectedCollection, handleCollectionOps, } from "utils/collection"; -import { - FILE_OPS_TYPE, - constructFileToCollectionMap, - getSelectedFiles, - handleFileOps, -} from "utils/file"; +import { FILE_OPS_TYPE, getSelectedFiles, handleFileOps } from "utils/file"; import { getSessionExpiredMessage } from "utils/ui"; import { getLocalFamilyData } from "utils/user/family"; @@ -339,6 +334,7 @@ export default function Gallery() { const defaultHiddenCollectionIDs = state.defaultHiddenCollectionIDs; const hiddenFileIDs = state.hiddenFileIDs; const collectionNameMap = state.allCollectionNameByID; + const fileToCollectionsMap = state.fileCollectionIDs; const collectionSummaries = state.collectionSummaries; const hiddenCollectionSummaries = state.hiddenCollectionSummaries; @@ -746,10 +742,6 @@ export default function Gallery() { }; }, [selectAll, clearSelection]); - const fileToCollectionsMap = useMemoSingleThreaded(() => { - return constructFileToCollectionMap(files); - }, [files]); - const showSessionExpiredMessage = () => { setDialogMessage(getSessionExpiredMessage(logout)); }; diff --git a/web/apps/photos/src/utils/file/index.ts b/web/apps/photos/src/utils/file/index.ts index 4b7af55897..4adef118ea 100644 --- a/web/apps/photos/src/utils/file/index.ts +++ b/web/apps/photos/src/utils/file/index.ts @@ -517,17 +517,6 @@ export function getIDBasedSortedFiles(files: EnteFile[]) { return files.sort((a, b) => a.id - b.id); } -export function constructFileToCollectionMap(files: EnteFile[]) { - const fileToCollectionsMap = new Map(); - (files ?? []).forEach((file) => { - if (!fileToCollectionsMap.get(file.id)) { - fileToCollectionsMap.set(file.id, []); - } - fileToCollectionsMap.get(file.id).push(file.collectionID); - }); - return fileToCollectionsMap; -} - export const shouldShowAvatar = (file: EnteFile, user: User) => { if (!file || !user) { return false; diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.ts index ffea09257d..94aaee2875 100644 --- a/web/packages/new/photos/components/gallery/reducer.ts +++ b/web/packages/new/photos/components/gallery/reducer.ts @@ -29,6 +29,7 @@ import type { CollectionSummaryType, } from "../../services/collection/ui"; import { + createFileCollectionIDs, getLatestVersionFiles, groupFilesByCollectionID, } from "../../services/file"; @@ -119,6 +120,10 @@ export interface GalleryState { * collections. */ allCollectionNameByID: Map; + /** + * A list of collection IDs to which a file belongs, indexed by file ID. + */ + fileCollectionIDs: Map; /*--< Derived UI state >--*/ @@ -194,6 +199,7 @@ const initialGalleryState: GalleryState = { hiddenFileIDs: new Set(), favoriteFileIDs: new Set(), allCollectionNameByID: new Map(), + fileCollectionIDs: new Map(), collectionSummaries: new Map(), hiddenCollectionSummaries: new Map(), filteredData: [], @@ -236,6 +242,7 @@ const galleryReducer: React.Reducer = ( allCollectionNameByID: createCollectionNameByID( action.allCollections, ), + fileCollectionIDs: createFileCollectionIDs(action.files), collectionSummaries: deriveCollectionSummaries( action.user, collections, @@ -323,6 +330,7 @@ const galleryReducer: React.Reducer = ( state.collections, files, ), + fileCollectionIDs: createFileCollectionIDs(action.files), collectionSummaries: deriveCollectionSummaries( ensure(state.user), state.collections, @@ -345,6 +353,7 @@ const galleryReducer: React.Reducer = ( state.collections, files, ), + fileCollectionIDs: createFileCollectionIDs(action.files), collectionSummaries: deriveCollectionSummaries( ensure(state.user), state.collections, @@ -363,6 +372,7 @@ const galleryReducer: React.Reducer = ( state.collections, files, ), + fileCollectionIDs: createFileCollectionIDs(files), // TODO: Consider batching this instead of doing it per file // upload to speed up uploads. Perf test first though. collectionSummaries: deriveCollectionSummaries( diff --git a/web/packages/new/photos/services/file.ts b/web/packages/new/photos/services/file.ts index e522bb3d34..d887b30a1c 100644 --- a/web/packages/new/photos/services/file.ts +++ b/web/packages/new/photos/services/file.ts @@ -14,6 +14,19 @@ export const groupFilesByCollectionID = (files: EnteFile[]) => return result; }, new Map()); +/** + * Construct a map from file IDs to the list of collections (IDs) to which the + * file belongs. + */ +export const createFileCollectionIDs = (files: EnteFile[]) => + files.reduce((result, file) => { + const id = file.id; + let fs = result.get(id); + if (!fs) result.set(id, (fs = [])); + fs.push(file.collectionID); + return result; + }, new Map()); + export function getLatestVersionFiles(files: EnteFile[]) { const latestVersionFiles = new Map(); files.forEach((file) => {