From 235067ecee1bb6134bc9840dee1749853304e317 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Sat, 19 Oct 2024 14:59:52 +0530 Subject: [PATCH] wip checkpoint --- web/apps/photos/src/pages/gallery.tsx | 16 +++++++++-- web/apps/photos/src/services/fileService.ts | 10 +++++-- .../new/photos/components/gallery/reducer.ts | 28 +++++++++++++++---- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx index 09a3f3ee8a..420202887f 100644 --- a/web/apps/photos/src/pages/gallery.tsx +++ b/web/apps/photos/src/pages/gallery.tsx @@ -843,10 +843,20 @@ export default function Gallery() { collections, hiddenCollections, }); - await syncFiles("normal", collections, (files) => - dispatch({ type: "fetchFiles", files }), + await syncFiles( + "normal", + collections, + (files) => dispatch({ type: "resetFiles", files }), + (files) => dispatch({ type: "fetchFiles", files }), + ); + await syncFiles( + "hidden", + hiddenCollections, + (hiddenFiles) => + dispatch({ type: "resetHiddenFiles", hiddenFiles }), + (hiddenFiles) => + dispatch({ type: "fetchHiddenFiles", hiddenFiles }), ); - await syncFiles("hidden", hiddenCollections, setHiddenFiles); await syncTrash(allCollections, setTrashedFiles); // syncWithRemote is called with the force flag set to true before // doing an upload. So it is possible, say when resuming a pending diff --git a/web/apps/photos/src/services/fileService.ts b/web/apps/photos/src/services/fileService.ts index 43187c6359..2f4f37077d 100644 --- a/web/apps/photos/src/services/fileService.ts +++ b/web/apps/photos/src/services/fileService.ts @@ -8,7 +8,6 @@ import { EnteFile, FileWithUpdatedMagicMetadata, FileWithUpdatedPublicMagicMetadata, - mergeMetadata, TrashRequest, } from "@/media/file"; import { getLatestVersionFiles } from "@/new/photos/services/file"; @@ -16,7 +15,6 @@ import { clearCachedThumbnailsIfChanged, getLocalFiles, setLocalFiles, - sortFiles, } from "@/new/photos/services/files"; import { batch } from "@/utils/array"; import HTTPService from "@ente/shared/network/HTTPService"; @@ -33,6 +31,11 @@ import { * Fetch all files of the given {@link type}, belonging to the given * {@link collections}, from remote and update our local database. * + * If this is the initial read, or if the count of files we have differs from + * the state of the local database (these two are expected to be the same case), + * then the {@link onResetFiles} callback is invoked to give the caller a chance + * to bring its state up to speed. + * * In addition to updating the local database, it also calls the provided * {@link onFetchFiles} callback with the latest decrypted files after each * batch the new and/or updated files are received from remote. @@ -40,6 +43,7 @@ import { export const syncFiles = async ( type: "normal" | "hidden", collections: Collection[], + onResetFiles: (fs: EnteFile[]) => void, onFetchFiles: (fs: EnteFile[]) => void, ) => { const localFiles = await getLocalFiles(type); @@ -47,7 +51,7 @@ export const syncFiles = async ( let didUpdateFiles = false; if (files.length !== localFiles.length) { await setLocalFiles(type, files); - setFiles(sortFiles(mergeMetadata(files))); + onResetFiles(files); didUpdateFiles = true; } for (const collection of collections) { diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.ts index b990586733..a785dcb6c0 100644 --- a/web/packages/new/photos/components/gallery/reducer.ts +++ b/web/packages/new/photos/components/gallery/reducer.ts @@ -126,10 +126,11 @@ export type GalleryAction = collections: Collection[]; hiddenCollections: Collection[]; } + | { type: "resetFiles"; files: EnteFile[] } | { type: "fetchFiles"; files: EnteFile[] } | { type: "uploadFile"; file: EnteFile } - | { type: "setFiles"; files: EnteFile[] } - | { type: "setHiddenFiles"; hiddenFiles: EnteFile[] } + | { type: "resetHiddenFiles"; hiddenFiles: EnteFile[] } + | { type: "fetchHiddenFiles"; hiddenFiles: EnteFile[] } | { type: "setTrashedFiles"; trashedFiles: EnteFile[] } | { type: "setFavorites"; favFileIDs: Set }; @@ -194,6 +195,8 @@ const galleryReducer: React.Reducer = ( collections: action.collections, hiddenCollections: action.hiddenCollections, }; + case "resetFiles": + return { ...state, files: sortFiles(mergeMetadata(action.files)) }; case "fetchFiles": return { ...state, @@ -211,10 +214,23 @@ const galleryReducer: React.Reducer = ( ...state, files: sortFiles([...state.files, action.file]), }; - case "setFiles": - return { ...state, files: action.files }; - case "setHiddenFiles": - return { ...state, hiddenFiles: action.hiddenFiles }; + case "resetHiddenFiles": + return { + ...state, + hiddenFiles: sortFiles(mergeMetadata(action.hiddenFiles)), + }; + case "fetchHiddenFiles": + return { + ...state, + hiddenFiles: sortFiles( + mergeMetadata( + getLatestVersionFiles([ + ...state.hiddenFiles, + ...action.hiddenFiles, + ]), + ), + ), + }; case "setTrashedFiles": return { ...state, trashedFiles: action.trashedFiles }; case "setFavorites":