Skip to content

Commit

Permalink
Switch
Browse files Browse the repository at this point in the history
  • Loading branch information
mnvr committed Nov 5, 2024
1 parent b730d41 commit 448a878
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 39 deletions.
24 changes: 11 additions & 13 deletions web/apps/photos/src/components/Sidebar/Preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<NestedSidebarDrawerVisibilityProps> = ({
open,
Expand Down Expand Up @@ -234,16 +230,18 @@ export const AdvancedSettings: React.FC<NestedSidebarDrawerVisibilityProps> = ({
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 (
<NestedSidebarDrawer
Expand All @@ -262,8 +260,8 @@ export const AdvancedSettings: React.FC<NestedSidebarDrawerVisibilityProps> = ({
<MenuItemGroup>
<EnteMenuItem
variant="toggle"
checked={!appContext.isCFProxyDisabled}
onClick={toggleCFProxy}
checked={!cfUploadProxyDisabled}
onClick={toggle}
label={t("faster_upload")}
/>
</MenuItemGroup>
Expand Down
2 changes: 0 additions & 2 deletions web/apps/photos/src/components/Upload/Uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ export default function Uploader({
},
onUploadFile,
publicCollectionGalleryContext,
appContext.isCFProxyDisabled,
);

if (uploadManager.isUploadRunning()) {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions web/apps/photos/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -230,8 +226,6 @@ export default function App({ Component, pageProps }: AppProps) {
showMiniDialog,
somethingWentWrong,
onGenericError,
isCFProxyDisabled,
setIsCFProxyDisabled,
logout,
};

Expand Down
7 changes: 7 additions & 0 deletions web/apps/photos/src/services/logout.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -42,6 +43,12 @@ export const photosLogout = async () => {
ignoreError("settings", e);
}

try {
resetUploadState();
} catch (e) {
ignoreError("upload", e);
}

try {
DownloadManager.logout();
} catch (e) {
Expand Down
12 changes: 2 additions & 10 deletions web/apps/photos/src/services/upload/uploadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -331,7 +331,6 @@ class UploadManager {
private publicUploadProps: PublicUploadProps;
private uploaderName: string;
private uiService: UIService;
private isCFUploadProxyDisabled: boolean = false;

constructor() {
this.uiService = new UIService();
Expand All @@ -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;
Expand Down Expand Up @@ -543,7 +535,7 @@ class UploadManager {
this.existingFiles,
this.parsedMetadataJSONMap,
worker,
this.isCFUploadProxyDisabled,
shouldDisableCFUploadProxy(),
() => {
this.abortIfCancelled();
},
Expand Down
10 changes: 10 additions & 0 deletions web/packages/media/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 2 additions & 4 deletions web/packages/new/photos/services/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
// };
3 changes: 2 additions & 1 deletion web/packages/new/photos/services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions web/packages/new/photos/types/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
1 change: 0 additions & 1 deletion web/packages/shared/storage/localStorage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export enum LS_KEYS {
// LOCALE = 'locale',
SRP_SETUP_ATTRIBUTES = "srpSetupAttributes",
SRP_ATTRIBUTES = "srpAttributes",
CF_PROXY_DISABLED = "cfProxyDisabled",
REFERRAL_SOURCE = "referralSource",
}

Expand Down

0 comments on commit 448a878

Please sign in to comment.