π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Move to reducer
Manav Rathid3491b80ee15
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsxindex 59942f4224..017d7d026e 100644--- a/web/apps/photos/src/pages/gallery.tsx+++ b/web/apps/photos/src/pages/gallery.tsx@@ -56,8 +56,6 @@ import {SearchResultsHeader,} from "ente-new/photos/components/gallery";import {- constructUserIDToEmailMap,- createShareeSuggestionEmails,findCollectionCreatingUncategorizedIfNeeded,validateKey,} from "ente-new/photos/components/gallery/helpers";@@ -199,10 +197,6 @@ const Page: React.FC = () => {while one was already in progress. */const resyncOpts = useRef<SyncWithRemoteOpts | undefined>(undefined);- const [userIDToEmailMap, setUserIDToEmailMap] =- useState<Map<number, string>>(null);- const [emailList, setEmailList] = useState<string[]>(null);-const [uploadTypeSelectorView, setUploadTypeSelectorView] = useState(false);const [uploadTypeSelectorIntent, setUploadTypeSelectorIntent] =useState<UploadTypeSelectorIntent>("upload");@@ -265,7 +259,6 @@ const Page: React.FC = () => {// Local aliases.const {user,- familyData,normalCollections,normalFiles,hiddenFiles,@@ -360,16 +353,6 @@ const Page: React.FC = () => {});}, [normalCollections, normalFiles]);- useEffect(() => {- if (!user || !normalCollections) {- return;- }- setUserIDToEmailMap(constructUserIDToEmailMap(user, normalCollections));- setEmailList(- createShareeSuggestionEmails(user, normalCollections, familyData),- );- }, [user, normalCollections, familyData]);-useEffect(() => {if (typeof activeCollectionID == "undefined" || !router.isReady) {return;@@ -910,9 +893,10 @@ const Page: React.FC = () => {syncWithRemote({ force, silent }),setBlockingLoad,photoListHeader,- userIDToEmailMap,user,- emailList,+ // TODO(RE): Rename+ userIDToEmailMap: state.emailByUserID,+ emailList: state.shareSuggestionEmails,openHiddenSection,isClipSearchResult,selectedFile: selected,diff --git a/web/packages/new/photos/components/gallery/helpers.ts b/web/packages/new/photos/components/gallery/helpers.tsindex ccc2b19848..3512b29d4e 100644--- a/web/packages/new/photos/components/gallery/helpers.ts+++ b/web/packages/new/photos/components/gallery/helpers.ts@@ -11,10 +11,8 @@*/import { getUserRecoveryKey } from "ente-accounts/services/recovery-key";-import type { User } from "ente-accounts/services/user";import log from "ente-base/log";import type { Collection } from "ente-media/collection";-import type { FamilyData } from "ente-new/photos/services/user-details";import { createUncategorizedCollection } from "../../services/collection";import { PseudoCollectionID } from "../../services/collection-summary";@@ -51,48 +49,3 @@ export const findCollectionCreatingUncategorizedIfNeeded = async (collectionSummaryID == PseudoCollectionID.uncategorizedPlaceholder? createUncategorizedCollection(): collections.find(({ id }) => id == collectionSummaryID);--export const constructUserIDToEmailMap = (- user: User,- collections: Collection[],-): Map<number, string> => {- const userIDToEmail = new Map<number, string>();- collections.forEach((item) => {- const { owner, sharees } = item;- if (user.id !== owner.id && owner.email) {- userIDToEmail.set(owner.id, owner.email);- }- sharees.forEach((item) => {- if (item.id !== user.id && item.email)- userIDToEmail.set(item.id, item.email);- });- });- return userIDToEmail;-};--/**- * Create a list of emails that are shown as suggestions to the user when they- * are trying to share albums with specific users.- */-export const createShareeSuggestionEmails = (- user: User,- collections: Collection[],- familyData: FamilyData | undefined,-): string[] => {- const emails = collections- .map(({ owner, sharees }) =>- owner.email && owner.id != user.id- ? [owner.email]- : sharees.map(({ email }) => email),- )- .flat()- .filter((e) => e !== undefined);-- // Add family members.- if (familyData) {- const family = familyData.members.map((member) => member.email);- emails.push(...family);- }-- return [...new Set(emails.filter((email) => email != user.email))];-};diff --git a/web/packages/new/photos/components/gallery/reducer.ts b/web/packages/new/photos/components/gallery/reducer.tsindex 1661e062b6..6fefb96bb0 100644--- a/web/packages/new/photos/components/gallery/reducer.ts+++ b/web/packages/new/photos/components/gallery/reducer.ts@@ -224,6 +224,22 @@ export interface GalleryState {* that they're a part of.*/fileNormalCollectionIDs: Map<number, number[]>;+ /**+ * A map from Ente user IDs to their emails (except for the user in user+ * themselves).+ *+ * This is used to perform a fast lookup of the email of the Ente user that+ * shared a file or collection.+ */+ emailByUserID: Map<number, string>;+ /**+ * A list of emails that can be served as suggestions when the user is+ * trying to share a collection with another Ente user.+ *+ * These are derived from the emails of the Ente users with whom the user+ * has already shared collections, plus the emails of their family members.+ */+ shareSuggestionEmails: string[];/*--< Derived UI state >--*/@@ -459,6 +475,8 @@ const initialGalleryState: GalleryState = {favoriteFileIDs: new Set(),collectionNameByID: new Map(),fileNormalCollectionIDs: new Map(),+ emailByUserID: new Map(),+ shareSuggestionEmails: [],normalCollectionSummaries: new Map(),hiddenCollectionSummaries: new Map(),uncategorizedCollectionSummaryID:@@ -488,6 +506,8 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (if (process.env.NEXT_PUBLIC_ENTE_TRACE) console.log("dispatch", action);switch (action.type) {case "mount": {+ const { user, familyData } = action;+const lastSyncedCollectionFiles = sortFiles(action.collectionFiles);const trashItems = sortTrashItems(action.trashItems);@@ -519,8 +539,8 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (return stateByUpdatingFilteredFiles({...state,- user: action.user,- familyData: action.familyData,+ user,+ familyData,collections,lastSyncedCollectionFiles,trashItems,@@ -543,6 +563,12 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (collectionFiles,hiddenCollectionIDs,),+ emailByUserID: constructUserIDToEmailMap(user, collections),+ shareSuggestionEmails: createShareSuggestionEmails(+ user,+ familyData,+ collections,+ ),normalCollectionSummaries: deriveNormalCollectionSummaries(action.user,normalCollections,@@ -633,6 +659,15 @@ const galleryReducer: React.Reducer<GalleryState, GalleryAction> = (state.collectionFiles,hiddenCollectionIDs,),+ emailByUserID: constructUserIDToEmailMap(+ state.user!,+ collections,+ ),+ shareSuggestionEmails: createShareSuggestionEmails(+ state.user!,+ state.familyData!,+ collections,+ ),normalCollectionSummaries,hiddenCollectionSummaries,uncategorizedCollectionSummaryID:@@ -1694,7 +1729,10 @@ const stateByUpdatingFilteredFiles = (state: GalleryState) => {if (state.isInSearchMode) {const filteredFiles = state.searchResults ?? state.filteredFiles;return { ...state, filteredFiles };- } else if (state.view?.type == "albums" || state.view?.type == "hidden-albums") {+ } else if (+ state.view?.type == "albums" ||+ state.view?.type == "hidden-albums"+ ) {const filteredFiles = deriveAlbumsOrHiddenAlbumsFilteredFiles(state.trashItems,state.collectionFiles,@@ -1859,3 +1897,48 @@ const enqueuePendingSearchSuggestionsIfNeeded = (searchSuggestion && isInSearchMode? [...pendingSearchSuggestions, searchSuggestion]: pendingSearchSuggestions;++/**+ * Create a map from the user IDs to their emails, with entries for all Ente+ * users who have shared a collection with the user.+ */+const constructUserIDToEmailMap = (+ user: User,+ collections: GalleryState["collections"],+): Map<number, string> => {+ const userIDToEmail = new Map<number, string>();+ for (const { owner, sharees } of collections) {+ if (user.id != owner.id && owner.email) {+ userIDToEmail.set(owner.id, owner.email);+ }+ for (const sharee of sharees) {+ if (sharee.id != user.id && sharee.email) {+ userIDToEmail.set(sharee.id, sharee.email);+ }+ }+ }+ return userIDToEmail;+};++/**+ * Create a list of emails that are shown as suggestions to the user when they+ * are trying to share albums with specific users.+ */+const createShareSuggestionEmails = (+ user: User,+ familyData: FamilyData,+ collections: Collection[],+): string[] => [+ ...new Set(+ collections+ .map(({ owner, sharees }) =>+ owner.email && owner.id != user.id+ ? [owner.email]+ : sharees.map(({ email }) => email),+ )+ .flat()+ .filter((e) => e !== undefined)+ .concat(familyData.members.map((member) => member.email))+ .filter((email) => email != user.email),+ ),+];