From 448a8789fa9715a3f8865eff966184c91f1b5276 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Tue, 5 Nov 2024 19:05:43 +0530 Subject: [PATCH] Switch --- .../src/components/Sidebar/Preferences.tsx | 24 +++++++++---------- .../photos/src/components/Upload/Uploader.tsx | 2 -- web/apps/photos/src/pages/_app.tsx | 6 ----- web/apps/photos/src/services/logout.ts | 7 ++++++ .../src/services/upload/uploadManager.ts | 12 ++-------- web/packages/media/upload.ts | 10 ++++++++ .../new/photos/services/migrations.ts | 6 ++--- web/packages/new/photos/services/settings.ts | 3 ++- web/packages/new/photos/types/context.ts | 2 -- .../shared/storage/localStorage/index.ts | 1 - 10 files changed, 34 insertions(+), 39 deletions(-) diff --git a/web/apps/photos/src/components/Sidebar/Preferences.tsx b/web/apps/photos/src/components/Sidebar/Preferences.tsx index 323c1266ee3..ba6800d4630 100644 --- a/web/apps/photos/src/components/Sidebar/Preferences.tsx +++ b/web/apps/photos/src/components/Sidebar/Preferences.tsx @@ -21,21 +21,17 @@ import { settingsSnapshot, settingsSubscribe, syncSettings, + updateCFProxyDisabledPreference, updateMapEnabled, } from "@/new/photos/services/settings"; -import { AppContext, useAppContext } from "@/new/photos/types/context"; +import { useAppContext } from "@/new/photos/types/context"; import { EnteMenuItem } from "@ente/shared/components/Menu/EnteMenuItem"; import ChevronRight from "@mui/icons-material/ChevronRight"; import ScienceIcon from "@mui/icons-material/Science"; import { Box, Stack } from "@mui/material"; import DropdownInput from "components/DropdownInput"; import { t } from "i18next"; -import React, { - useCallback, - useContext, - useEffect, - useSyncExternalStore, -} from "react"; +import React, { useCallback, useEffect, useSyncExternalStore } from "react"; export const Preferences: React.FC = ({ open, @@ -234,16 +230,18 @@ export const AdvancedSettings: React.FC = ({ onClose, onRootClose, }) => { - const appContext = useContext(AppContext); + const { cfUploadProxyDisabled } = useSyncExternalStore( + settingsSubscribe, + settingsSnapshot, + ); const handleRootClose = () => { onClose(); onRootClose(); }; - const toggleCFProxy = () => { - appContext.setIsCFProxyDisabled(!appContext.isCFProxyDisabled); - }; + const toggle = () => + void updateCFProxyDisabledPreference(!cfUploadProxyDisabled); return ( = ({ diff --git a/web/apps/photos/src/components/Upload/Uploader.tsx b/web/apps/photos/src/components/Upload/Uploader.tsx index bc5a08fa26f..163d72e6e8c 100644 --- a/web/apps/photos/src/components/Upload/Uploader.tsx +++ b/web/apps/photos/src/components/Upload/Uploader.tsx @@ -244,7 +244,6 @@ export default function Uploader({ }, onUploadFile, publicCollectionGalleryContext, - appContext.isCFProxyDisabled, ); if (uploadManager.isUploadRunning()) { @@ -287,7 +286,6 @@ export default function Uploader({ publicCollectionGalleryContext.accessedThroughSharedURL, publicCollectionGalleryContext.token, publicCollectionGalleryContext.passwordToken, - appContext.isCFProxyDisabled, ]); // Handle selected files when user selects files for upload through the open diff --git a/web/apps/photos/src/pages/_app.tsx b/web/apps/photos/src/pages/_app.tsx index 0c589b5b041..361a0be901c 100644 --- a/web/apps/photos/src/pages/_app.tsx +++ b/web/apps/photos/src/pages/_app.tsx @@ -79,10 +79,6 @@ export default function App({ Component, pageProps }: AppProps) { LS_KEYS.THEME, THEME_COLOR.DARK, ); - const [isCFProxyDisabled, setIsCFProxyDisabled] = useLocalState( - LS_KEYS.CF_PROXY_DISABLED, - false, - ); useEffect(() => { void setupI18n().finally(() => setIsI18nReady(true)); @@ -230,8 +226,6 @@ export default function App({ Component, pageProps }: AppProps) { showMiniDialog, somethingWentWrong, onGenericError, - isCFProxyDisabled, - setIsCFProxyDisabled, logout, }; diff --git a/web/apps/photos/src/services/logout.ts b/web/apps/photos/src/services/logout.ts index b4856ec5b32..ab4b91200a2 100644 --- a/web/apps/photos/src/services/logout.ts +++ b/web/apps/photos/src/services/logout.ts @@ -1,5 +1,6 @@ import { accountLogout } from "@/accounts/services/logout"; import log from "@/base/log"; +import { resetUploadState } from "@/media/upload"; import DownloadManager from "@/new/photos/services/download"; import { logoutML, terminateMLWorker } from "@/new/photos/services/ml"; import { logoutSearch } from "@/new/photos/services/search"; @@ -42,6 +43,12 @@ export const photosLogout = async () => { ignoreError("settings", e); } + try { + resetUploadState(); + } catch (e) { + ignoreError("upload", e); + } + try { DownloadManager.logout(); } catch (e) { diff --git a/web/apps/photos/src/services/upload/uploadManager.ts b/web/apps/photos/src/services/upload/uploadManager.ts index 3239e20055c..35bf5da1fbb 100644 --- a/web/apps/photos/src/services/upload/uploadManager.ts +++ b/web/apps/photos/src/services/upload/uploadManager.ts @@ -8,9 +8,9 @@ import type { Collection } from "@/media/collection"; import { EncryptedEnteFile, EnteFile } from "@/media/file"; import { FileType } from "@/media/file-type"; import { potentialFileTypeFromExtension } from "@/media/live-photo"; +import { shouldDisableCFUploadProxy } from "@/media/upload"; import { getLocalFiles } from "@/new/photos/services/files"; import { indexNewUpload } from "@/new/photos/services/ml"; -import { getDisableCFUploadProxyFlag } from "@/new/photos/services/settings"; import type { UploadItem } from "@/new/photos/services/upload/types"; import { RANDOM_PERCENTAGE_PROGRESS_FOR_PUT, @@ -331,7 +331,6 @@ class UploadManager { private publicUploadProps: PublicUploadProps; private uploaderName: string; private uiService: UIService; - private isCFUploadProxyDisabled: boolean = false; constructor() { this.uiService = new UIService(); @@ -341,15 +340,8 @@ class UploadManager { progressUpdater: ProgressUpdater, onUploadFile: (file: EnteFile) => void, publicCollectProps: PublicUploadProps, - isCFUploadProxyDisabled: boolean, ) { this.uiService.init(progressUpdater); - const remoteIsCFUploadProxyDisabled = - await getDisableCFUploadProxyFlag(); - if (remoteIsCFUploadProxyDisabled) { - isCFUploadProxyDisabled = remoteIsCFUploadProxyDisabled; - } - this.isCFUploadProxyDisabled = isCFUploadProxyDisabled; UploadService.init(publicCollectProps); this.onUploadFile = onUploadFile; this.publicUploadProps = publicCollectProps; @@ -543,7 +535,7 @@ class UploadManager { this.existingFiles, this.parsedMetadataJSONMap, worker, - this.isCFUploadProxyDisabled, + shouldDisableCFUploadProxy(), () => { this.abortIfCancelled(); }, diff --git a/web/packages/media/upload.ts b/web/packages/media/upload.ts index 45a26618e2a..21f0d9246a0 100644 --- a/web/packages/media/upload.ts +++ b/web/packages/media/upload.ts @@ -18,6 +18,16 @@ class UploadState { /** State shared by the functions in this module. See {@link UploadState}. */ let _state = new UploadState(); +/** + * Reset any internal state maintained by the module. + * + * This is primarily meant as a way for stateful apps (e.g. photos) to clear any + * user specific state on logout. + */ +export const resetUploadState = () => { + _state = new UploadState(); +}; + /** * Return true to disable the upload of files via Cloudflare Workers. * diff --git a/web/packages/new/photos/services/migrations.ts b/web/packages/new/photos/services/migrations.ts index 225eb375888..472a9c51963 100644 --- a/web/packages/new/photos/services/migrations.ts +++ b/web/packages/new/photos/services/migrations.ts @@ -101,11 +101,9 @@ const m3 = () => removeKV("latestUpdatedAt/location"), ]); -// TODO: Not enabled yet. +// TODO: Not enabled yet since it is not critical. Enable with next batch of changes. // // Added: Nov 2025 (v1.7.7-beta). Prunable. // const m4 = () => { -// // Delete legacy keys for storing individual settings -// // LS_KEYS.MAP_ENABLED = "mapEnabled", +// // Delete the legacy key that used to store the map preference. // localStorage.removeItem("mapEnabled"); -// localStorage.removeItem("cfProxyDisabled"); // }; diff --git a/web/packages/new/photos/services/settings.ts b/web/packages/new/photos/services/settings.ts index 51a6e7be4bd..3d79ca64aaa 100644 --- a/web/packages/new/photos/services/settings.ts +++ b/web/packages/new/photos/services/settings.ts @@ -235,7 +235,8 @@ const savedCFProxyDisabled = () => { }; /** - * Save the user preference for disabling uploads via Cloudflare Workers. + * Save the user preference for disabling uploads via Cloudflare Workers, also + * notifying the upload subsystem of the change. */ export const updateCFProxyDisabledPreference = async (value: boolean) => { saveCFProxyDisabled(value); diff --git a/web/packages/new/photos/types/context.ts b/web/packages/new/photos/types/context.ts index dc24bad5cfb..4fac591b5dd 100644 --- a/web/packages/new/photos/types/context.ts +++ b/web/packages/new/photos/types/context.ts @@ -37,8 +37,6 @@ export type AppContextT = AccountsContextT & { setWatchFolderFiles: (files: FileList) => void; themeColor: THEME_COLOR; setThemeColor: (themeColor: THEME_COLOR) => void; - isCFProxyDisabled: boolean; - setIsCFProxyDisabled: (disabled: boolean) => void; }; /** diff --git a/web/packages/shared/storage/localStorage/index.ts b/web/packages/shared/storage/localStorage/index.ts index 2ce68c535cb..17219a867ac 100644 --- a/web/packages/shared/storage/localStorage/index.ts +++ b/web/packages/shared/storage/localStorage/index.ts @@ -20,7 +20,6 @@ export enum LS_KEYS { // LOCALE = 'locale', SRP_SETUP_ATTRIBUTES = "srpSetupAttributes", SRP_ATTRIBUTES = "srpAttributes", - CF_PROXY_DISABLED = "cfProxyDisabled", REFERRAL_SOURCE = "referralSource", }