From 407eaeb53a3d2a0774cf0a694d936062a17a222b Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Sat, 19 Oct 2024 16:16:39 +0530 Subject: [PATCH] Tinker --- web/apps/photos/src/pages/gallery.tsx | 10 ++--- .../new/photos/components/gallery/reducer.ts | 39 ++++++++++++------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx index 7f24b97571..204d378e15 100644 --- a/web/apps/photos/src/pages/gallery.tsx +++ b/web/apps/photos/src/pages/gallery.tsx @@ -21,8 +21,8 @@ import { } from "@/new/photos/components/gallery"; import type { GalleryBarMode } from "@/new/photos/components/gallery/BarImpl"; import { - getUniqueFiles, setDerivativeState, + uniqueFilesByID, useGalleryReducer, } from "@/new/photos/components/gallery/reducer"; import { usePeopleStateSnapshot } from "@/new/photos/components/utils/ml"; @@ -436,7 +436,7 @@ export default function Gallery() { () => setSearchCollectionsAndFiles({ collections: collections ?? [], - files: getUniqueFiles(files ?? []), + files: uniqueFilesByID(files ?? []), }), [collections, files], ); @@ -612,7 +612,7 @@ export default function Gallery() { } } const pfSet = new Set(activePerson?.fileIDs ?? []); - filteredFiles = getUniqueFiles( + filteredFiles = uniqueFilesByID( files.filter(({ id }) => { if (!pfSet.has(id)) return false; return true; @@ -623,13 +623,13 @@ export default function Gallery() { people: filteredVisiblePeople, }; } else if (activeCollectionID === TRASH_SECTION) { - filteredFiles = getUniqueFiles([ + filteredFiles = uniqueFilesByID([ ...trashedFiles, ...files.filter((file) => tempDeletedFileIds?.has(file.id)), ]); } else { const baseFiles = barMode == "hidden-albums" ? hiddenFiles : files; - filteredFiles = getUniqueFiles( + filteredFiles = uniqueFilesByID( baseFiles.filter((item) => { if (tempDeletedFileIds?.has(item.id)) { return false; diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.ts index 280388e10e..b81b3d2616 100644 --- a/web/packages/new/photos/components/gallery/reducer.ts +++ b/web/packages/new/photos/components/gallery/reducer.ts @@ -326,19 +326,28 @@ export const setDerivativeState = ( }; }; -export function getUniqueFiles(files: EnteFile[]) { - const idSet = new Set(); - const uniqueFiles = files.filter((file) => { - if (!idSet.has(file.id)) { - idSet.add(file.id); - return true; - } else { - return false; - } +/** + * File IDs themselves are unique across all the files for the user (in fact, + * they're unique across all the files in an Ente instance). However, we still + * can have multiple entries for the same file ID in our local database because + * the unit of account is not actually a file, but a "Collection File": a + * collection and file pair. + * + * For example, if the same file is symlinked into two collections, then we will + * have two "Collection File" entries for it, both with the same file ID, but + * with different collection IDs. + * + * This function returns files such that only one of these entries (arbitrarily + * picked in case of dupes) is returned. + */ +export const uniqueFilesByID = (files: EnteFile[]) => { + const seen = new Set(); + return files.filter(({ id }) => { + if (seen.has(id)) return false; + seen.add(id); + return true; }); - - return uniqueFiles; -} +}; const getArchivedCollectionIDs = (collections: Collection[]) => new Set( @@ -541,7 +550,7 @@ function getHiddenItemsSummary( .filter((collection) => isDefaultHiddenCollection(collection)) .map((collection) => collection.id), ); - const hiddenItems = getUniqueFiles( + const hiddenItems = uniqueFilesByID( hiddenFiles.filter((file) => defaultHiddenCollectionIds.has(file.collectionID), ), @@ -589,7 +598,7 @@ function getSectionSummaries( } function getArchivedSectionSummary(files: EnteFile[]): CollectionSummary { - const archivedFiles = getUniqueFiles( + const archivedFiles = uniqueFilesByID( files.filter((file) => isArchivedFile(file)), ); return { @@ -662,7 +671,7 @@ function getAllSectionVisibleFiles( files: EnteFile[], archivedCollections: Set, ): EnteFile[] { - const allSectionVisibleFiles = getUniqueFiles( + const allSectionVisibleFiles = uniqueFilesByID( files.filter((file) => { if ( isArchivedFile(file) ||