Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Keep fav file overrides in reducer

Manav Rathi1232b956c911
diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.ts
index e5ea779a32..03fb49fa75 100644
--- a/web/packages/new/photos/components/gallery/reducer.ts
+++ b/web/packages/new/photos/components/gallery/reducer.ts
@@ -211,6 +211,31 @@ export interface GalleryState {
* hidden.
*/
tempHiddenFileIDs: Set<number>;
+ /**
+ * Updates to the favorite status of files that have just been toggled by
+ * the user in the file viewer.
+ *
+ * Each entry is from a file ID to `true` (if that file should be considered
+ * as part of the favorites) or `false` (if that file should not be
+ * considered as part of the favorites).
+ *
+ * When the user marks a file as a favorite (or unmarks it as a favorite),
+ * we add the override to this map so that we can give them immediate
+ * feedback in the UI.
+ *
+ * The request to update the favorite status on remote proceeds in parallel.
+ * If that request fails, we remove the override from here.
+ *
+ * If the remote request succeeds, we still need to sync the files and
+ * collections in our local DB with the remote state, but that happens in a
+ * batch when the user exits the viewer. So until that point, these updates
+ * remain in this overrides map.
+ *
+ * Once the remote file + collection sync completes, we can clear this map
+ * since now just the {@link favoriteFileIDs} derived from our local files
+ * would reflect the correct state on remote too.
+ */
+ favoriteFileIDOverrides: Map<number, boolean>;
/*--< State that underlies transient UI state >--*/
@@ -335,6 +360,8 @@ export type GalleryAction =
| { type: "clearTempDeleted" }
| { type: "markTempHidden"; files: EnteFile[] }
| { type: "clearTempHidden" }
+ | { type: "addFavoriteOverride"; fileID: number; isFavorite: boolean }
+ | { type: "clearFavoriteOverrides" }
| { type: "showAll" }
| { type: "showHidden" }
| { type: "showAlbums" }
@@ -366,8 +393,9 @@ const initialGalleryState: GalleryState = {
fileCollectionIDs: new Map(),
collectionSummaries: new Map(),
hiddenCollectionSummaries: new Map(),
- tempDeletedFileIDs: new Set<number>(),
- tempHiddenFileIDs: new Set<number>(),
+ tempDeletedFileIDs: new Set(),
+ tempHiddenFileIDs: new Set(),
+ favoriteFileIDOverrides: new Map(),
selectedCollectionSummaryID: undefined,
selectedPersonID: undefined,
extraVisiblePerson: undefined,
@@ -415,6 +443,7 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
favoriteFileIDs: deriveFavoriteFileIDs(
collections,
action.files,
+ state.favoriteFileIDOverrides,
),
allCollectionNameByID: createCollectionNameByID(
action.allCollections,
@@ -466,6 +495,7 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
favoriteFileIDs: deriveFavoriteFileIDs(
collections,
state.files,
+ state.favoriteFileIDOverrides,
),
allCollectionNameByID: createCollectionNameByID(
collections.concat(state.hiddenCollections),
@@ -529,6 +559,7 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
favoriteFileIDs: deriveFavoriteFileIDs(
collections,
state.files,
+ state.favoriteFileIDOverrides,
),
allCollectionNameByID: createCollectionNameByID(
collections.concat(hiddenCollections),
@@ -554,6 +585,7 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
favoriteFileIDs: deriveFavoriteFileIDs(
state.collections,
files,
+ state.favoriteFileIDOverrides,
),
fileCollectionIDs: createFileCollectionIDs(action.files),
collectionSummaries: deriveCollectionSummaries(
@@ -584,6 +616,7 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
favoriteFileIDs: deriveFavoriteFileIDs(
state.collections,
files,
+ state.favoriteFileIDOverrides,
),
fileCollectionIDs: createFileCollectionIDs(action.files),
collectionSummaries: deriveCollectionSummaries(
@@ -610,6 +643,7 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
favoriteFileIDs: deriveFavoriteFileIDs(
state.collections,
files,
+ state.favoriteFileIDOverrides,
),
fileCollectionIDs: createFileCollectionIDs(files),
// TODO: Consider batching this instead of doing it per file
@@ -731,6 +765,39 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
tempHiddenFileIDs: new Set(),
});
+ case "addFavoriteOverride": {
+ const favoriteFileIDOverrides = new Map(
+ state.favoriteFileIDOverrides,
+ );
+ favoriteFileIDOverrides.set(action.fileID, action.isFavorite);
+ // stateByUpdatingFilteredFiles doesn't currently depend on
+ // favorite fileIDs or overrides, so skipping it.
+ return {
+ ...state,
+ favoriteFileIDs: deriveFavoriteFileIDs(
+ state.collections,
+ state.files,
+ favoriteFileIDOverrides,
+ ),
+ favoriteFileIDOverrides,
+ };
+ }
+
+ case "clearFavoriteOverrides": {
+ const favoriteFileIDOverrides = new Map<number, boolean>();
+ // stateByUpdatingFilteredFiles doesn't currently depend on
+ // favorite fileIDs or overrides, so skipping it.
+ return {
+ ...state,
+ favoriteFileIDs: deriveFavoriteFileIDs(
+ state.collections,
+ state.files,
+ favoriteFileIDOverrides,
+ ),
+ favoriteFileIDOverrides,
+ };
+ }
+
case "showAll":
return stateByUpdatingFilteredFiles({
...state,
@@ -926,17 +993,24 @@ const deriveHiddenFileIDs = (hiddenFiles: EnteFile[]) =>
const deriveFavoriteFileIDs = (
collections: Collection[],
files: EnteFile[],
+ favoriteFileIDOverrides: GalleryState["favoriteFileIDOverrides"],
): Set<number> => {
+ let favoriteFileIDs = new Set<number>();
for (const collection of collections) {
if (collection.type === CollectionType.favorites) {
- return new Set(
+ favoriteFileIDs = new Set(
files
.filter((file) => file.collectionID === collection.id)
- .map((file): number => file.id),
+ .map((file) => file.id),
);
+ break;
}
}
- return new Set();
+ for (const [fileID, isFavorite] of favoriteFileIDOverrides.entries()) {
+ if (isFavorite) favoriteFileIDs.add(fileID);
+ else favoriteFileIDs.delete(fileID);
+ }
+ return favoriteFileIDs;
};
/**