Skip to content

armgit / ente

Public Imported from GitHub
0

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

chore(reducer):added-comments-for-new-function

Aswin Asok3395341be9df
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 8bb2b25a49..793a746afe 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -1059,14 +1059,16 @@ const Page: React.FC = () => {
op: Extract<FileOp, "favorite" | "unfavorite">,
selectedFiles: EnteFile[],
) => {
- const filesWithTargetState =
- op == "unfavorite"
- ? selectedFiles.filter((file) => favoriteFileIDs.has(file.id))
- : selectedFiles;
const filesToProcess: EnteFile[] = [];
let skippedUnsupportedSharedFile = false;
- for (const file of filesWithTargetState) {
+ /**
+ * We currently can only process files which either the currentUser is the owner
+ * or the file has a valid metadataHash with it, so checking if we have
+ * any unspported files and if so then setting the variable flag as true
+ * for showing the modal later on.
+ */
+ for (const file of selectedFiles) {
if (file.ownerID == user!.id || metadataHash(file.metadata)) {
filesToProcess.push(file);
} else {
@@ -1074,10 +1076,16 @@ const Page: React.FC = () => {
}
}
+ // if there are no files to process, like if every file
+ // we got to process was unsupported then returning the flag and state.
if (!filesToProcess.length) {
return { processed: false, skippedUnsupportedSharedFile };
}
+ // if we have files to process then, checking what the op is
+ // favorite and then creating a map of the current state before
+ // doing anything. So if the API call fails later, the code knows
+ // how to restore each file back to its old state.
const isFavorite = op == "favorite";
const previousFavoriteByFileID = new Map(
filesToProcess.map((file) => [
@@ -1086,6 +1094,14 @@ const Page: React.FC = () => {
]),
);
+ /**
+ * Looping through the filesToProcess to do two things
+ * - addPendingFavoriteUpdate: Let the UI know that this file currently have a request in progress.
+ * So that the user can't trigger simultaneous updates.
+ *
+ * - unsycnedFavoriteUpdate: This changes the visible favorite state immediately, for a faster user
+ * feedback.
+ */
for (const file of filesToProcess) {
dispatch({ type: "addPendingFavoriteUpdate", fileID: file.id });
dispatch({ type: "unsyncedFavoriteUpdate", file, isFavorite });
diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.ts
index 022c71aed0..824dced319 100644
--- a/web/packages/new/photos/components/gallery/reducer.ts
+++ b/web/packages/new/photos/components/gallery/reducer.ts
@@ -956,13 +956,18 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (
}
case "clearUnsyncedState": {
+ // Getting the last syncedCollectionFiles
const collectionFiles = state.lastSyncedCollectionFiles;
+
+ // Getting the FavoriteFileIDs from the syncedData
+ // without applying any optimistic chagnes.
const syncedFavoriteFileIDs = deriveFavoriteFileIDs(
state.user!,
state.collections,
collectionFiles,
new Map(),
);
+
const unsyncedFavoriteUpdates: GalleryState["unsyncedFavoriteUpdates"] =
preserveUnreflectedFavoriteUpdates(
state.user!,
@@ -1365,7 +1370,24 @@ const deriveFavoriteFileIDs = (
}
return favoriteFileIDs;
};
-
+/**
+ *
+ * @param user The currentLoggedIn User
+ *
+ * @param collectionFiles The collectionsFiles which the user currently has
+ * from state.lastSyncedCollectionFiles
+ *
+ * @param syncedFavoriteFileIDs: The list of FavoriteFileIDs derived from
+ * the collectionFiles without any optimistic updations.
+ *
+ * @param unsyncedFavoriteUpdates The list of FavoriteFileIDs derived from
+ * the collectionFiles including any optimistic updates from the
+ * state.unsyncedFavoriteUpdates,
+ *
+ * @returns the preservedUpdates, If there is atleast one affected file
+ * where the synced state still disagrees with, we store that file's update in
+ * this variable and return it else, it will be an empty map.
+ */
const preserveUnreflectedFavoriteUpdates = (
user: LocalUser,
collectionFiles: GalleryState["collectionFiles"],
@@ -1374,6 +1396,12 @@ const preserveUnreflectedFavoriteUpdates = (
) => {
const preservedUpdates: GalleryState["unsyncedFavoriteUpdates"] = new Map();
+ /**
+ * Iterating through the unsyncedFavoriteUpdates, if the file has
+ * fileHashAndTypeKey it's a shared album file, if so then, finding
+ * all non-owned/shared file sin the gallery whose hash/type matches
+ * this update and storing their IDs to the updatedFileIDs.
+ */
for (const [key, update] of unsyncedFavoriteUpdates.entries()) {
const updatedFileIDs = update.fileHashAndTypeKey
? collectionFiles
@@ -1386,6 +1414,8 @@ const preserveUnreflectedFavoriteUpdates = (
.map((file) => file.id)
: [update.fileID];
+ // So, If there at least one affected file where synced state still
+ // disagrees with the optimistic update, we preserve the update, else ignore it.
if (
updatedFileIDs.some(
(fileID) =>