Skip to content

Commit

Permalink
[web] Misc tweaks (#3161)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnvr authored Sep 7, 2024
2 parents 88684c9 + d2deea9 commit e140b2f
Show file tree
Hide file tree
Showing 22 changed files with 112 additions and 171 deletions.
4 changes: 2 additions & 2 deletions web/apps/auth/src/pages/auth.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stashRedirect } from "@/accounts/services/redirect";
import { NavbarBase } from "@/base/components/Navbar";
import { ensure } from "@/utils/ensure";
import {
Expand All @@ -11,7 +12,6 @@ import OverflowMenu from "@ente/shared/components/OverflowMenu/menu";
import { OverflowMenuOption } from "@ente/shared/components/OverflowMenu/option";
import { AUTH_PAGES as PAGES } from "@ente/shared/constants/pages";
import { ApiError, CustomError } from "@ente/shared/error";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
import LogoutOutlined from "@mui/icons-material/LogoutOutlined";
import MoreHoriz from "@mui/icons-material/MoreHoriz";
import { Button, ButtonBase, Snackbar, TextField, styled } from "@mui/material";
Expand Down Expand Up @@ -43,7 +43,7 @@ const Page: React.FC = () => {
e instanceof Error &&
e.message == CustomError.KEY_MISSING
) {
InMemoryStore.set(MS_KEYS.REDIRECT_URL, PAGES.AUTH);
stashRedirect(PAGES.AUTH);
router.push("/");
} else if (e instanceof ApiError && e.httpStatusCode == 401) {
// We get back a 401 Unauthorized if the token is not valid.
Expand Down
30 changes: 9 additions & 21 deletions web/apps/photos/src/components/Upload/Uploader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { UPLOAD_STAGES } from "@/new/photos/services/upload/types";
import { firstNonEmpty } from "@/utils/array";
import { ensure } from "@/utils/ensure";
import { CustomError } from "@ente/shared/error";
import { isPromise } from "@ente/shared/utils";
import DiscFullIcon from "@mui/icons-material/DiscFull";
import UserNameInputDialog from "components/UserNameInputDialog";
import { t } from "i18next";
Expand Down Expand Up @@ -199,7 +198,7 @@ export default function Uploader({
*/
const pickedUploadType = useRef<PICKED_UPLOAD_TYPE>(null);

const currentUploadPromise = useRef<Promise<void>>(null);
const currentUploadPromise = useRef<Promise<void> | undefined>();
const uploadRunning = useRef(false);
const uploaderNameRef = useRef<string>(null);
const isDragAndDrop = useRef(false);
Expand Down Expand Up @@ -555,15 +554,14 @@ export default function Uploader({
uploaderName?: string,
) => {
const currentPromise = currentUploadPromise.current;
currentUploadPromise.current = waitAndRun(
currentPromise,
async () =>
await uploadFiles(
uploadItemsWithCollection,
collections,
uploaderName,
),
);
currentUploadPromise.current = (async () => {
if (currentPromise) await currentPromise;
return uploadFiles(
uploadItemsWithCollection,
collections,
uploaderName,
);
})();
await currentUploadPromise.current;
};

Expand Down Expand Up @@ -802,16 +800,6 @@ export default function Uploader({
);
}

async function waitAndRun(
waitPromise: Promise<void>,
task: () => Promise<void>,
) {
if (waitPromise && isPromise(waitPromise)) {
await waitPromise;
}
await task();
}

const desktopFilesAndZipItems = async (electron: Electron, files: File[]) => {
const fileAndPaths: FileAndPath[] = [];
let zipItems: ZipItem[] = [];
Expand Down
21 changes: 10 additions & 11 deletions web/apps/photos/src/pages/deduplicate/index.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
import { stashRedirect } from "@/accounts/services/redirect";
import { getLocalFiles } from "@/new/photos/services/files";
import PhotoFrame from "components/PhotoFrame";
import { ALL_SECTION } from "constants/collection";
import { t } from "i18next";
import { AppContext } from "pages/_app";
import { createContext, useContext, useEffect, useState } from "react";
import { Duplicate, getDuplicates } from "services/deduplicationService";
import { syncFiles, trashFiles } from "services/fileService";
import { SelectedState } from "types/gallery";

import { VerticallyCentered } from "@ente/shared/components/Container";
import EnteSpinner from "@ente/shared/components/EnteSpinner";
import { PHOTOS_PAGES as PAGES } from "@ente/shared/constants/pages";
import { ApiError } from "@ente/shared/error";
import useMemoSingleThreaded from "@ente/shared/hooks/useMemoSingleThreaded";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
import { SESSION_KEYS, getKey } from "@ente/shared/storage/sessionStorage";
import { styled } from "@mui/material";
import Typography from "@mui/material/Typography";
import { HttpStatusCode } from "axios";
import DeduplicateOptions from "components/pages/dedupe/SelectedFileOptions";
import PhotoFrame from "components/PhotoFrame";
import { ALL_SECTION } from "constants/collection";
import { t } from "i18next";
import { default as Router, default as router } from "next/router";
import { AppContext } from "pages/_app";
import { createContext, useContext, useEffect, useState } from "react";
import {
getAllLatestCollections,
getLocalCollections,
} from "services/collectionService";
import { Duplicate, getDuplicates } from "services/deduplicationService";
import { syncFiles, trashFiles } from "services/fileService";
import { syncTrash } from "services/trashService";
import {
DeduplicateContextType,
DefaultDeduplicateContext,
} from "types/deduplicate";
import { SelectedState } from "types/gallery";
import { constructFileToCollectionMap, getSelectedFiles } from "utils/file";

export const DeduplicateContext = createContext<DeduplicateContextType>(
Expand Down Expand Up @@ -57,7 +56,7 @@ export default function Deduplicate() {
useEffect(() => {
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
if (!key) {
InMemoryStore.set(MS_KEYS.REDIRECT_URL, PAGES.DEDUPLICATE);
stashRedirect(PAGES.DEDUPLICATE);
router.push("/");
return;
}
Expand Down
4 changes: 2 additions & 2 deletions web/apps/photos/src/pages/gallery/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stashRedirect } from "@/accounts/services/redirect";
import log from "@/base/log";
import { WhatsNew } from "@/new/photos/components/WhatsNew";
import { shouldShowWhatsNew } from "@/new/photos/services/changelog";
Expand All @@ -22,7 +23,6 @@ import { getRecoveryKey } from "@ente/shared/crypto/helpers";
import { CustomError } from "@ente/shared/error";
import { useFileInput } from "@ente/shared/hooks/useFileInput";
import useMemoSingleThreaded from "@ente/shared/hooks/useMemoSingleThreaded";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
import { LS_KEYS, getData } from "@ente/shared/storage/localStorage";
import {
getToken,
Expand Down Expand Up @@ -342,7 +342,7 @@ export default function Gallery() {
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
const token = getToken();
if (!key || !token) {
InMemoryStore.set(MS_KEYS.REDIRECT_URL, PAGES.GALLERY);
stashRedirect(PAGES.GALLERY);
router.push("/");
return;
}
Expand Down
41 changes: 8 additions & 33 deletions web/apps/photos/src/services/searchService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { isDesktop } from "@/base/app";
import log from "@/base/log";
import { FileType } from "@/media/file-type";
import {
clipMatches,
isMLEnabled,
isMLSupported,
mlStatusSnapshot,
wipSearchPersons,
Expand Down Expand Up @@ -41,19 +38,20 @@ export const getAutoCompleteSuggestions =
(files: EnteFile[], collections: Collection[]) =>
async (searchPhrase: string): Promise<SearchOption[]> => {
try {
searchPhrase = searchPhrase.trim().toLowerCase();
if (!searchPhrase?.length) {
const searchPhrase2 = searchPhrase.trim().toLowerCase();
if (!searchPhrase2?.length) {
return [];
}
const suggestions: Suggestion[] = [
await getClipSuggestion(searchPhrase),
...getFileTypeSuggestion(searchPhrase),
// The following functionality has moved to createSearchQuery
// - getClipSuggestion(searchPhrase)
// - getDateSuggestion(searchPhrase),
// - getLocationSuggestion(searchPhrase),
...(await createSearchQuery(searchPhrase)),
...getCollectionSuggestion(searchPhrase, collections),
getFileNameSuggestion(searchPhrase, files),
getFileCaptionSuggestion(searchPhrase, files),
...getFileTypeSuggestion(searchPhrase2),
...getCollectionSuggestion(searchPhrase2, collections),
getFileNameSuggestion(searchPhrase2, files),
getFileCaptionSuggestion(searchPhrase2, files),
].filter((suggestion) => !!suggestion);

return convertSuggestionsToOptions(suggestions);
Expand Down Expand Up @@ -198,20 +196,6 @@ function getFileCaptionSuggestion(
};
}

async function getClipSuggestion(
searchPhrase: string,
): Promise<Suggestion | undefined> {
if (!isDesktop) return undefined;

const clipResults = await searchClip(searchPhrase);
if (!clipResults) return undefined;
return {
type: SuggestionType.CLIP,
value: clipResults,
label: searchPhrase,
};
}

function searchCollection(
searchPhrase: string,
collections: Collection[],
Expand Down Expand Up @@ -239,15 +223,6 @@ function searchFilesByCaption(searchPhrase: string, files: EnteFile[]) {
);
}

const searchClip = async (
searchPhrase: string,
): Promise<ClipSearchScores | undefined> => {
if (!isMLEnabled()) return undefined;
const matches = await clipMatches(searchPhrase);
log.debug(() => ["clip/scores", matches]);
return matches;
};

function convertSuggestionToSearchQuery(option: Suggestion): SearchQuery {
switch (option.type) {
case SuggestionType.DATE:
Expand Down
5 changes: 2 additions & 3 deletions web/packages/accounts/pages/change-password.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ import {
generateLoginSubKey,
saveKeyInSessionStore,
} from "@ente/shared/crypto/helpers";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
import { LS_KEYS, getData, setData } from "@ente/shared/storage/localStorage";
import { SESSION_KEYS } from "@ente/shared/storage/sessionStorage";
import { getActualKey } from "@ente/shared/user";
import type { KEK, KeyAttributes, User } from "@ente/shared/user/types";
import { t } from "i18next";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { appHomeRoute } from "../services/redirect";
import { appHomeRoute, stashRedirect } from "../services/redirect";
import type { PageProps } from "../types/page";

const Page: React.FC<PageProps> = () => {
Expand All @@ -46,7 +45,7 @@ const Page: React.FC<PageProps> = () => {
const user = getData(LS_KEYS.USER);
setUser(user);
if (!user?.token) {
InMemoryStore.set(MS_KEYS.REDIRECT_URL, PAGES.CHANGE_PASSWORD);
stashRedirect(PAGES.CHANGE_PASSWORD);
router.push("/");
} else {
setToken(user.token);
Expand Down
13 changes: 7 additions & 6 deletions web/packages/accounts/pages/credentials.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
saveKeyInSessionStore,
} from "@ente/shared/crypto/helpers";
import { CustomError } from "@ente/shared/error";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
import {
LS_KEYS,
getData,
Expand Down Expand Up @@ -52,7 +51,11 @@ import {
openPasskeyVerificationURL,
passkeyVerificationRedirectURL,
} from "../services/passkey";
import { appHomeRoute } from "../services/redirect";
import {
appHomeRoute,
stashRedirect,
unstashRedirect,
} from "../services/redirect";
import { checkSessionValidity } from "../services/session";
import {
configureSRP,
Expand Down Expand Up @@ -231,7 +234,7 @@ const Page: React.FC<PageProps> = ({ appContext }) => {
isTwoFactorEnabled: true,
isTwoFactorPasskeysEnabled: true,
});
InMemoryStore.set(MS_KEYS.REDIRECT_URL, "/");
stashRedirect("/");
const url =
passkeyVerificationRedirectURL(passkeySessionID);
setPasskeyVerificationData({ passkeySessionID, url });
Expand Down Expand Up @@ -312,9 +315,7 @@ const Page: React.FC<PageProps> = ({ appContext }) => {
} catch (e) {
log.error("migrate to srp failed", e);
}
const redirectURL = InMemoryStore.get(MS_KEYS.REDIRECT_URL);
InMemoryStore.delete(MS_KEYS.REDIRECT_URL);
router.push(redirectURL ?? appHomeRoute);
router.push(unstashRedirect() ?? appHomeRoute);
} catch (e) {
log.error("useMasterPassword failed", e);
}
Expand Down
6 changes: 2 additions & 4 deletions web/packages/accounts/pages/passkeys/finish.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import log from "@/base/log";
import { nullToUndefined } from "@/utils/transform";
import { VerticallyCentered } from "@ente/shared/components/Container";
import EnteSpinner from "@ente/shared/components/EnteSpinner";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
import {
LS_KEYS,
getData,
Expand All @@ -13,6 +12,7 @@ import {
import { useRouter } from "next/router";
import React, { useEffect } from "react";
import { PAGES } from "../../constants/pages";
import { unstashRedirect } from "../../services/redirect";
import type { PageProps } from "../../types/page";

/**
Expand Down Expand Up @@ -112,7 +112,5 @@ const saveCredentialsAndNavigateTo = async (
});
setData(LS_KEYS.KEY_ATTRIBUTES, keyAttributes);

const redirectURL = InMemoryStore.get(MS_KEYS.REDIRECT_URL);
InMemoryStore.delete(MS_KEYS.REDIRECT_URL);
return redirectURL ?? PAGES.CREDENTIALS;
return unstashRedirect() ?? PAGES.CREDENTIALS;
};
5 changes: 2 additions & 3 deletions web/packages/accounts/pages/recover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import {
decryptAndStoreToken,
saveKeyInSessionStore,
} from "@ente/shared/crypto/helpers";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
import { LS_KEYS, getData, setData } from "@ente/shared/storage/localStorage";
import { SESSION_KEYS, getKey } from "@ente/shared/storage/sessionStorage";
import type { KeyAttributes, User } from "@ente/shared/user/types";
import { t } from "i18next";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { appHomeRoute } from "../services/redirect";
import { appHomeRoute, stashRedirect } from "../services/redirect";
import type { PageProps } from "../types/page";

const bip39 = require("bip39");
Expand All @@ -48,7 +47,7 @@ const Page: React.FC<PageProps> = ({ appContext }) => {
}
if (!user?.encryptedToken && !user?.token) {
sendOtt(user.email);
InMemoryStore.set(MS_KEYS.REDIRECT_URL, PAGES.RECOVER);
stashRedirect(PAGES.RECOVER);
router.push(PAGES.VERIFY);
return;
}
Expand Down
6 changes: 2 additions & 4 deletions web/packages/accounts/pages/two-factor/verify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import FormPaperFooter from "@ente/shared/components/Form/FormPaper/Footer";
import FormTitle from "@ente/shared/components/Form/FormPaper/Title";
import LinkButton from "@ente/shared/components/LinkButton";
import { ApiError } from "@ente/shared/error";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
import {
LS_KEYS,
getData,
Expand All @@ -22,6 +21,7 @@ import { HttpStatusCode } from "axios";
import { t } from "i18next";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { unstashRedirect } from "../../services/redirect";
import type { PageProps } from "../../types/page";

const Page: React.FC<PageProps> = ({ appContext }) => {
Expand Down Expand Up @@ -59,9 +59,7 @@ const Page: React.FC<PageProps> = ({ appContext }) => {
id,
});
setData(LS_KEYS.KEY_ATTRIBUTES, ensure(keyAttributes));
const redirectURL = InMemoryStore.get(MS_KEYS.REDIRECT_URL);
InMemoryStore.delete(MS_KEYS.REDIRECT_URL);
router.push(redirectURL ?? PAGES.CREDENTIALS);
router.push(unstashRedirect() ?? PAGES.CREDENTIALS);
} catch (e) {
if (
e instanceof ApiError &&
Expand Down
5 changes: 2 additions & 3 deletions web/packages/accounts/pages/verify.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import SingleInputForm, {
type SingleInputFormProps,
} from "@ente/shared/components/SingleInputForm";
import { ApiError } from "@ente/shared/error";
import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
import localForage from "@ente/shared/storage/localForage";
import {
LS_KEYS,
Expand All @@ -40,6 +39,7 @@ import {
openPasskeyVerificationURL,
passkeyVerificationRedirectURL,
} from "../services/passkey";
import { unstashRedirect } from "../services/redirect";
import { configureSRP } from "../services/srp";
import type { PageProps } from "../types/page";
import type { SRPSetupAttributes } from "../types/srp";
Expand Down Expand Up @@ -143,8 +143,7 @@ const Page: React.FC<PageProps> = ({ appContext }) => {
}
localForage.clear();
setIsFirstLogin(true);
const redirectURL = InMemoryStore.get(MS_KEYS.REDIRECT_URL);
InMemoryStore.delete(MS_KEYS.REDIRECT_URL);
const redirectURL = unstashRedirect();
if (keyAttributes?.encryptedKey) {
clearKeys();
router.push(redirectURL ?? PAGES.CREDENTIALS);
Expand Down
Loading

0 comments on commit e140b2f

Please sign in to comment.