Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

refactor(photos):extract-subscription-completion-hook

Aswin Asokc85a4be114d9
diff --git a/web/apps/photos/src/components/utils/use-subscription-completion-redirect.tsx b/web/apps/photos/src/components/utils/use-subscription-completion-redirect.tsx
new file mode 100644
index 0000000000..ecb905cdbe
--- /dev/null
+++ b/web/apps/photos/src/components/utils/use-subscription-completion-redirect.tsx
@@ -0,0 +1,85 @@
+import { errorDialogAttributes } from "ente-base/components/utils/dialog";
+import { useBaseContext } from "ente-base/context";
+import log from "ente-base/log";
+import { haveMasterKeyInSession } from "ente-base/session-storage";
+import {
+ redirectToCustomerPortal,
+ verifyStripeSubscription,
+} from "ente-new/photos/services/user-details";
+import { usePhotosAppContext } from "ente-new/photos/types/context";
+import { t } from "i18next";
+import { useRouter } from "next/router";
+import { useEffect, useEffectEvent } from "react";
+import { Trans } from "react-i18next";
+
+export const useSubscriptionCompletionRedirect = () => {
+ const router = useRouter();
+ const { showMiniDialog } = useBaseContext();
+ const { showLoadingBar } = usePhotosAppContext();
+
+ const handleCompletion = useEffectEvent(async () => {
+ const { session_id: sessionID, status, reason } = router.query;
+
+ if (status == "success") {
+ try {
+ const subscription = await verifyStripeSubscription(sessionID);
+ showMiniDialog({
+ title: t("thank_you"),
+ message: (
+ <Trans
+ i18nKey="subscription_purchase_success"
+ values={{ date: subscription.expiryTime }}
+ />
+ ),
+ continue: { text: t("ok") },
+ cancel: false,
+ });
+ } catch (e) {
+ log.error("Subscription verification failed", e);
+ showMiniDialog(
+ errorDialogAttributes(t("subscription_verification_error")),
+ );
+ }
+ } else if (status == "fail") {
+ log.error(`Subscription purchase failed`, reason);
+ switch (reason) {
+ case "canceled":
+ showMiniDialog({
+ message: t("subscription_purchase_cancelled"),
+ continue: { text: t("ok"), color: "primary" },
+ cancel: false,
+ });
+ break;
+ case "requires_payment_method":
+ case "authentication_failed":
+ showMiniDialog({
+ title: t("update_payment_method"),
+ message:
+ reason == "authentication_failed"
+ ? t("payment_method_authentication_failed")
+ : t("update_payment_method_message"),
+ continue: {
+ text: t("update_payment_method"),
+ action: () => {
+ showLoadingBar();
+ return redirectToCustomerPortal();
+ },
+ },
+ });
+ break;
+ default:
+ showMiniDialog(
+ errorDialogAttributes(
+ t("subscription_purchase_failed"),
+ ),
+ );
+ }
+ }
+ });
+
+ useEffect(() => {
+ if (router.isReady && haveMasterKeyInSession()) {
+ void handleCompletion();
+ }
+ }, [router.isReady]);
+};
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index 17dd4d3ca2..d41dcafaff 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -57,6 +57,7 @@ import {
notifyUnsupportedSharedFavoritesDialogAttributes,
trashFilesDialogAttributes,
} from "@/components/utils/dialog-attributes";
+import { useSubscriptionCompletionRedirect } from "@/components/utils/use-subscription-completion-redirect";
import exportService from "@/services/export";
import { processPendingAlbumJoin } from "@/services/join-album";
import {
@@ -78,7 +79,6 @@ import { stashRedirect } from "ente-accounts/services/redirect";
import { isSessionInvalid } from "ente-accounts/services/session";
import { ensureLocalUser } from "ente-accounts/services/user";
import { isDesktop } from "ente-base/app";
-import type { MiniDialogAttributes } from "ente-base/components/MiniDialog";
import { Notification } from "ente-base/components/Notification";
import { SingleInputDialog } from "ente-base/components/SingleInputDialog";
import { TranslucentLoadingOverlay } from "ente-base/components/loaders";
@@ -175,15 +175,11 @@ import {
initSettings,
updateMapEnabled,
} from "ente-new/photos/services/settings";
-import {
- redirectToCustomerPortal,
- savedUserDetailsOrTriggerPull,
- verifyStripeSubscription,
-} from "ente-new/photos/services/user-details";
+import { savedUserDetailsOrTriggerPull } from "ente-new/photos/services/user-details";
import { usePhotosAppContext } from "ente-new/photos/types/context";
import { PromiseQueue } from "ente-utils/promise";
import { t } from "i18next";
-import { useRouter, type NextRouter } from "next/router";
+import { useRouter } from "next/router";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import type { FileWithPath } from "react-dropzone";
import { Trans } from "react-i18next";
@@ -774,15 +770,7 @@ const Page: React.FC = () => {
if (!activeCollectionSummary) closeCollectionMap();
}, [activeCollectionSummary, closeCollectionMap]);
- useEffect(() => {
- if (router.isReady && haveMasterKeyInSession()) {
- void handleSubscriptionCompletionRedirectIfNeeded(
- showMiniDialog,
- showLoadingBar,
- router,
- );
- }
- }, [router.isReady]);
+ useSubscriptionCompletionRedirect();
useEffect(() => {
updateSearchCollectionsAndFiles(
@@ -2600,68 +2588,6 @@ const Page: React.FC = () => {
export default Page;
-const handleSubscriptionCompletionRedirectIfNeeded = async (
- showMiniDialog: (attributes: MiniDialogAttributes) => void,
- showLoadingBar: () => void,
- router: NextRouter,
-) => {
- const { session_id: sessionID, status, reason } = router.query;
-
- if (status == "success") {
- try {
- const subscription = await verifyStripeSubscription(sessionID);
- showMiniDialog({
- title: t("thank_you"),
- message: (
- <Trans
- i18nKey="subscription_purchase_success"
- values={{ date: subscription.expiryTime }}
- />
- ),
- continue: { text: t("ok") },
- cancel: false,
- });
- } catch (e) {
- log.error("Subscription verification failed", e);
- showMiniDialog(
- errorDialogAttributes(t("subscription_verification_error")),
- );
- }
- } else if (status == "fail") {
- log.error(`Subscription purchase failed`, reason);
- switch (reason) {
- case "canceled":
- showMiniDialog({
- message: t("subscription_purchase_cancelled"),
- continue: { text: t("ok"), color: "primary" },
- cancel: false,
- });
- break;
- case "requires_payment_method":
- case "authentication_failed":
- showMiniDialog({
- title: t("update_payment_method"),
- message:
- reason == "authentication_failed"
- ? t("payment_method_authentication_failed")
- : t("update_payment_method_message"),
- continue: {
- text: t("update_payment_method"),
- action: () => {
- showLoadingBar();
- return redirectToCustomerPortal();
- },
- },
- });
- break;
- default:
- showMiniDialog(
- errorDialogAttributes(t("subscription_purchase_failed")),
- );
- }
- }
-};
-
const createAppDownloadFooter = (): FileListHeaderOrFooter => ({
component: (
<Typography