From 19f18fe03239e18edfd383b4c4af4fc71a55d922 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 9 Sep 2024 14:02:14 +0530 Subject: [PATCH 1/5] Inline --- .../photos/src/components/pages/gallery/PlanSelector.tsx | 5 ++--- web/apps/photos/src/constants/redirects.ts | 9 --------- web/apps/photos/src/utils/billing/index.ts | 4 +--- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx index c016bbe0dd..859a2d3cc0 100644 --- a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx +++ b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx @@ -1,5 +1,6 @@ import log from "@/base/log"; import { bytesInGB, formattedStorageByteSize } from "@/new/photos/utils/units"; +import { openURL } from "@/new/photos/utils/web"; import { FlexWrapper, FluidContainer, @@ -46,7 +47,6 @@ import { isSubscriptionActive, isSubscriptionCancelled, isUserSubscribedPlan, - manageFamilyMethod, planForSubscription, planSelectionOutcome, updatePaymentMethod, @@ -686,9 +686,8 @@ function ManageSubscription({ closeModal, setLoading, }: ManageSubscriptionProps) { - const appContext = useContext(AppContext); const openFamilyPortal = () => - manageFamilyMethod(appContext.setDialogMessage, setLoading); + openURL("https://web.ente.io?redirect=families"); return ( diff --git a/web/apps/photos/src/constants/redirects.ts b/web/apps/photos/src/constants/redirects.ts index 92b5cd1b87..e69de29bb2 100644 --- a/web/apps/photos/src/constants/redirects.ts +++ b/web/apps/photos/src/constants/redirects.ts @@ -1,9 +0,0 @@ -export enum REDIRECTS { - FAMILIES = "families", -} - -export const getRedirectURL = (redirect: REDIRECTS) => { - const url = new URL("https://web.ente.io"); - url.searchParams.set("redirect", redirect); - return url.href; -}; diff --git a/web/apps/photos/src/utils/billing/index.ts b/web/apps/photos/src/utils/billing/index.ts index 50366e0407..7fd9937ab9 100644 --- a/web/apps/photos/src/utils/billing/index.ts +++ b/web/apps/photos/src/utils/billing/index.ts @@ -2,7 +2,6 @@ import log from "@/base/log"; import { openURL } from "@/new/photos/utils/web"; import { SetDialogBoxAttributes } from "@ente/shared/components/DialogBox/types"; import { LS_KEYS, getData } from "@ente/shared/storage/localStorage"; -import { REDIRECTS, getRedirectURL } from "constants/redirects"; import { t } from "i18next"; import type { NextRouter } from "next/router"; import billingService from "services/billingService"; @@ -253,8 +252,7 @@ export async function manageFamilyMethod( ) { try { setLoading(true); - const familyPortalRedirectURL = getRedirectURL(REDIRECTS.FAMILIES); - openURL(familyPortalRedirectURL); + ; } catch (e) { log.error("failed to redirect to family portal", e); setDialogMessage({ From 8aed23ca93efb3ac9766573cf41c3d760f30fd19 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 9 Sep 2024 14:07:01 +0530 Subject: [PATCH 2/5] Inline 2 --- web/apps/photos/src/pages/_app.tsx | 25 ++++++---------------- web/apps/photos/src/utils/billing/index.ts | 20 ----------------- 2 files changed, 7 insertions(+), 38 deletions(-) diff --git a/web/apps/photos/src/pages/_app.tsx b/web/apps/photos/src/pages/_app.tsx index 06b5a4d684..23fb88c679 100644 --- a/web/apps/photos/src/pages/_app.tsx +++ b/web/apps/photos/src/pages/_app.tsx @@ -43,7 +43,6 @@ import ArrowForward from "@mui/icons-material/ArrowForward"; import { CssBaseline } from "@mui/material"; import { ThemeProvider } from "@mui/material/styles"; import Notification from "components/Notification"; -import { REDIRECTS } from "constants/redirects"; import { t } from "i18next"; import isElectron from "is-electron"; import type { AppProps } from "next/app"; @@ -67,8 +66,6 @@ import { getUpdateReadyToInstallMessage, } from "utils/ui"; -const redirectMap = new Map([[REDIRECTS.FAMILIES, getFamilyPortalRedirectURL]]); - /** * Properties available via {@link AppContext} to the Photos app's React tree. */ @@ -202,25 +199,17 @@ export default function App({ Component, pageProps }: AppProps) { const setUserOnline = () => setOffline(false); const setUserOffline = () => setOffline(true); - useEffect(() => { - const redirectTo = async (redirect) => { - if ( - redirectMap.has(redirect) && - typeof redirectMap.get(redirect) === "function" - ) { - const redirectAction = redirectMap.get(redirect); - window.location.href = await redirectAction(); - } else { - log.error(`invalid redirection ${redirect}`); - } - }; + const redirectToFamilyPortal = void getFamilyPortalRedirectURL().then( + (url) => (window.location.href = url), + ); + useEffect(() => { const query = new URLSearchParams(window.location.search); const redirectName = query.get("redirect"); - if (redirectName) { + if (redirectName == "families") { const user = getData(LS_KEYS.USER); if (user?.token) { - redirectTo(redirectName); + redirectToFamilyPortal(); } else { setRedirectName(redirectName); } @@ -235,7 +224,7 @@ export default function App({ Component, pageProps }: AppProps) { if (redirectName) { const user = getData(LS_KEYS.USER); if (user?.token) { - redirectTo(redirectName); + redirectToFamilyPortal(); // https://github.com/vercel/next.js/issues/2476#issuecomment-573460710 // eslint-disable-next-line no-throw-literal diff --git a/web/apps/photos/src/utils/billing/index.ts b/web/apps/photos/src/utils/billing/index.ts index 7fd9937ab9..ede9b64333 100644 --- a/web/apps/photos/src/utils/billing/index.ts +++ b/web/apps/photos/src/utils/billing/index.ts @@ -1,5 +1,4 @@ import log from "@/base/log"; -import { openURL } from "@/new/photos/utils/web"; import { SetDialogBoxAttributes } from "@ente/shared/components/DialogBox/types"; import { LS_KEYS, getData } from "@ente/shared/storage/localStorage"; import { t } from "i18next"; @@ -246,25 +245,6 @@ export async function updatePaymentMethod( } } -export async function manageFamilyMethod( - setDialogMessage: SetDialogBoxAttributes, - setLoading: SetLoading, -) { - try { - setLoading(true); - ; - } catch (e) { - log.error("failed to redirect to family portal", e); - setDialogMessage({ - title: t("ERROR"), - content: t("UNKNOWN_ERROR"), - close: { variant: "critical" }, - }); - } finally { - setLoading(false); - } -} - export async function checkSubscriptionPurchase( setDialogMessage: SetDialogBoxAttributes, router: NextRouter, From 96f10bebd4dc9447bd85ac8a8a8c6cd00abecdb2 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 9 Sep 2024 14:16:36 +0530 Subject: [PATCH 3/5] Inline --- web/apps/photos/src/pages/_app.tsx | 40 +++++++++++------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/web/apps/photos/src/pages/_app.tsx b/web/apps/photos/src/pages/_app.tsx index 23fb88c679..47e2df76dd 100644 --- a/web/apps/photos/src/pages/_app.tsx +++ b/web/apps/photos/src/pages/_app.tsx @@ -23,7 +23,6 @@ import DialogBoxV2 from "@ente/shared/components/DialogBoxV2"; import type { DialogBoxAttributesV2 } from "@ente/shared/components/DialogBoxV2/types"; import EnteSpinner from "@ente/shared/components/EnteSpinner"; import { MessageContainer } from "@ente/shared/components/MessageContainer"; -import { PHOTOS_PAGES as PAGES } from "@ente/shared/constants/pages"; import { useLocalState } from "@ente/shared/hooks/useLocalState"; import HTTPService from "@ente/shared/network/HTTPService"; import { @@ -102,7 +101,6 @@ export default function App({ Component, pageProps }: AppProps) { typeof window !== "undefined" && !window.navigator.onLine, ); const [showNavbar, setShowNavBar] = useState(false); - const [redirectName, setRedirectName] = useState(null); const [mapEnabled, setMapEnabled] = useState(false); const isLoadingBarRunning = useRef(false); const loadingBar = useRef(null); @@ -199,37 +197,24 @@ export default function App({ Component, pageProps }: AppProps) { const setUserOnline = () => setOffline(false); const setUserOffline = () => setOffline(true); - const redirectToFamilyPortal = void getFamilyPortalRedirectURL().then( - (url) => (window.location.href = url), - ); - useEffect(() => { const query = new URLSearchParams(window.location.search); - const redirectName = query.get("redirect"); - if (redirectName == "families") { - const user = getData(LS_KEYS.USER); - if (user?.token) { - redirectToFamilyPortal(); - } else { - setRedirectName(redirectName); - } - } + const needsFamilyRedirect = query.get("redirect") == "families"; + if (needsFamilyRedirect && getData(LS_KEYS.USER)?.token) + redirectToFamilyPortal(); router.events.on("routeChangeStart", (url: string) => { - const newPathname = url.split("?")[0] as PAGES; + const newPathname = url.split("?")[0]; if (window.location.pathname !== newPathname) { setLoading(true); } - if (redirectName) { - const user = getData(LS_KEYS.USER); - if (user?.token) { - redirectToFamilyPortal(); + if (needsFamilyRedirect && getData(LS_KEYS.USER)?.token) { + redirectToFamilyPortal(); - // https://github.com/vercel/next.js/issues/2476#issuecomment-573460710 - // eslint-disable-next-line no-throw-literal - throw "Aborting route change, redirection in process...."; - } + // https://github.com/vercel/next.js/issues/2476#issuecomment-573460710 + // eslint-disable-next-line no-throw-literal + throw "Aborting route change, redirection in process...."; } }); @@ -244,7 +229,7 @@ export default function App({ Component, pageProps }: AppProps) { window.removeEventListener("online", setUserOnline); window.removeEventListener("offline", setUserOffline); }; - }, [redirectName]); + }, []); useEffect(() => { setMessageDialogView(true); @@ -373,3 +358,8 @@ export default function App({ Component, pageProps }: AppProps) { ); } + +const redirectToFamilyPortal = () => + void getFamilyPortalRedirectURL().then((url) => { + window.location.href = url; + }); From 672865c062acf59338124d73c0a106d052f298ee Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 9 Sep 2024 14:28:23 +0530 Subject: [PATCH 4/5] Remove double redirection --- .../src/components/pages/gallery/PlanSelector.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx index 859a2d3cc0..2f06953af9 100644 --- a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx +++ b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx @@ -32,6 +32,7 @@ import { GalleryContext } from "pages/gallery"; import { useContext, useEffect, useMemo, useState } from "react"; import { Trans } from "react-i18next"; import billingService, { type PlansResponse } from "services/billingService"; +import { getFamilyPortalRedirectURL } from "services/userService"; import { Plan, Subscription } from "types/billing"; import { SetLoading } from "types/gallery"; import { BonusData } from "types/user"; @@ -686,8 +687,15 @@ function ManageSubscription({ closeModal, setLoading, }: ManageSubscriptionProps) { - const openFamilyPortal = () => - openURL("https://web.ente.io?redirect=families"); + const openFamilyPortal = async () => { + setLoading(true); + try { + openURL(await getFamilyPortalRedirectURL()); + } catch (e) { + log.error("Could not redirect to family portal", e); + } + setLoading(false); + }; return ( From 9037973ced51ad75c9c9c694da85d0b98fde255a Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Mon, 9 Sep 2024 14:30:34 +0530 Subject: [PATCH 5/5] Retain the dialog --- .../photos/src/components/pages/gallery/PlanSelector.tsx | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx index 2f06953af9..525da89eb7 100644 --- a/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx +++ b/web/apps/photos/src/components/pages/gallery/PlanSelector.tsx @@ -687,12 +687,19 @@ function ManageSubscription({ closeModal, setLoading, }: ManageSubscriptionProps) { + const { setDialogMessage } = useContext(AppContext); + const openFamilyPortal = async () => { setLoading(true); try { openURL(await getFamilyPortalRedirectURL()); } catch (e) { log.error("Could not redirect to family portal", e); + setDialogMessage({ + title: t("ERROR"), + content: t("UNKNOWN_ERROR"), + close: { variant: "critical" }, + }); } setLoading(false); };