Skip to content

armgit / ente

Public Imported from GitHub
0

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

[web][photos] Load plans from Manage subscription row

AswinAsokfd6596e23f4e
diff --git a/web/apps/photos/src/components/PlanSelector.tsx b/web/apps/photos/src/components/PlanSelector.tsx
index ba90af770f..4474e86f47 100644
--- a/web/apps/photos/src/components/PlanSelector.tsx
+++ b/web/apps/photos/src/components/PlanSelector.tsx
@@ -9,7 +9,6 @@ import {
Dialog,
IconButton,
Link,
- Skeleton,
Stack,
styled,
ToggleButton,
@@ -62,6 +61,7 @@ import { Trans } from "react-i18next";
type PlanSelectorProps = ModalVisibilityProps & {
setLoading: (loading: boolean) => void;
+ initialPlansData?: PlansData;
onManageFamily: () => void;
};
@@ -69,6 +69,7 @@ export const PlanSelector: React.FC<PlanSelectorProps> = ({
open,
onClose,
setLoading,
+ initialPlansData,
onManageFamily,
}) => {
const fullScreen = useMediaQuery(useTheme().breakpoints.down("sm"));
@@ -83,7 +84,12 @@ export const PlanSelector: React.FC<PlanSelectorProps> = ({
<SidebarDrawer {...{ open, onClose }} transitionDuration={0}>
<Box sx={{ maxWidth: 420, mx: "auto", py: 1 }}>
<PlanSelectorContents
- {...{ onClose, setLoading, onManageFamily }}
+ {...{
+ onClose,
+ setLoading,
+ onManageFamily,
+ initialPlansData,
+ }}
/>
</Box>
</SidebarDrawer>
@@ -105,7 +111,7 @@ export const PlanSelector: React.FC<PlanSelectorProps> = ({
}}
>
<PlanSelectorContents
- {...{ onClose, setLoading, onManageFamily }}
+ {...{ onClose, setLoading, onManageFamily, initialPlansData }}
/>
</Dialog>
);
@@ -113,12 +119,13 @@ export const PlanSelector: React.FC<PlanSelectorProps> = ({
export type PlanSelectorContentsProps = Pick<
PlanSelectorProps,
- "onClose" | "setLoading" | "onManageFamily"
+ "onClose" | "setLoading" | "onManageFamily" | "initialPlansData"
> & { onBeginCheckout?: () => void };
export const PlanSelectorContents: React.FC<PlanSelectorContentsProps> = ({
onClose,
setLoading,
+ initialPlansData,
onManageFamily,
onBeginCheckout,
}) => {
@@ -127,7 +134,9 @@ export const PlanSelectorContents: React.FC<PlanSelectorContentsProps> = ({
const userDetails = useUserDetailsSnapshot();
- const [plansData, setPlansData] = useState<PlansData | undefined>();
+ const [plansData, setPlansData] = useState<PlansData | undefined>(
+ initialPlansData,
+ );
const [planPeriod, setPlanPeriod] = useState<PlanPeriod>(
userDetails?.subscription.period ?? "month",
);
@@ -147,7 +156,7 @@ export const PlanSelectorContents: React.FC<PlanSelectorContentsProps> = ({
const request = { cancelled: false };
void (async () => {
try {
- const plansData = await getPlansData();
+ const plansData = initialPlansData ?? (await getPlansData());
if (request.cancelled) return;
const { plans } = plansData;
if (subscription && isSubscriptionActive(subscription)) {
@@ -517,17 +526,6 @@ const Plans: React.FC<PlansProps> = ({
onPlanSelect,
}) => (
<Stack sx={{ gap: 2 }}>
- {!plansData && (
- <Stack
- role="status"
- aria-label={t("loading")}
- aria-busy
- sx={{ gap: 2 }}
- >
- <Skeleton variant="rounded" height={56} />
- <Skeleton variant="rounded" height={56} />
- </Stack>
- )}
{plansData?.plans
.filter((plan) => plan.period === planPeriod)
.map((plan) => (
diff --git a/web/apps/photos/src/components/sidebar/Sidebar.tsx b/web/apps/photos/src/components/sidebar/Sidebar.tsx
index b0b4d6aab8..7ad4004a94 100644
--- a/web/apps/photos/src/components/sidebar/Sidebar.tsx
+++ b/web/apps/photos/src/components/sidebar/Sidebar.tsx
@@ -46,6 +46,7 @@ import {
pullUserDetails,
redirectToCustomerPortal,
userDetailsAddOnBonuses,
+ type PlansData,
type UserDetails,
} from "ente-new/photos/services/user-details";
import { usePhotosAppContext } from "ente-new/photos/types/context";
@@ -75,7 +76,7 @@ type SidebarProps = ModalVisibilityProps & {
onActionHandled?: (actionID: SidebarActionID) => void;
onShowLargeFiles: () => void;
onShowDuplicates: () => void;
- onShowPlanSelector: () => void;
+ onShowPlanSelector: (plansData?: PlansData) => void;
collectionNameByID: Map<number, string>;
onCloseOverlays: () => void;
children?: React.ReactNode;
@@ -592,7 +593,7 @@ const UserDetailsSection: React.FC<UserDetailsSectionProps> = ({
);
const handleSubscriptionCardClick = () =>
- openManageSubscription({
+ void openManageSubscription({
userDetails,
showManageMemberSubscription,
onShowPlanSelector,
diff --git a/web/apps/photos/src/components/sidebar/accounts/Account.tsx b/web/apps/photos/src/components/sidebar/accounts/Account.tsx
index 3f2831da44..f11bd6cbec 100644
--- a/web/apps/photos/src/components/sidebar/accounts/Account.tsx
+++ b/web/apps/photos/src/components/sidebar/accounts/Account.tsx
@@ -26,16 +26,24 @@ import type { SidebarActionID } from "ente-new/photos/services/search/types";
import { disable2FA, get2FAStatus } from "ente-new/photos/services/user";
import {
familyMemberStorageLimit,
+ getPlansData,
isFamilyAdmin,
isPartOfFamily,
isPartOfFamilyWithOtherMembers,
isSubscriptionFree,
pullUserDetails,
userDetailsSnapshot,
+ type PlansData,
} from "ente-new/photos/services/user-details";
import { t } from "i18next";
import { useRouter } from "next/router";
-import React, { useCallback, useEffect, useMemo, useState } from "react";
+import React, {
+ useCallback,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from "react";
import { DeleteAccount } from "../account/DeleteAccount";
import { SessionsSettings } from "../account/SessionsSettings";
import { ManageMemberSubscription } from "./ManageMemberSubscription";
@@ -56,7 +64,7 @@ export type AccountAction = Extract<
type AccountProps = NestedSidebarDrawerVisibilityProps & {
onAuthenticateUser: () => Promise<boolean>;
- onShowPlanSelector: () => void;
+ onShowPlanSelector: (plansData?: PlansData) => void;
pendingAction?: AccountAction;
onActionHandled?: (action?: AccountAction) => void;
};
@@ -70,11 +78,20 @@ export const Account: React.FC<AccountProps> = ({
pendingAction,
onActionHandled,
}) => {
- const { showMiniDialog } = useBaseContext();
+ const { showMiniDialog, onGenericError } = useBaseContext();
const userDetails = useUserDetailsSnapshot();
const [twoFactorEnabled, setTwoFactorEnabled] = useState<boolean>();
const [disablingTwoFactor, setDisablingTwoFactor] = useState(false);
const [sessionCount, setSessionCount] = useState<number>();
+ const [subscriptionLoading, setSubscriptionLoading] = useState(false);
+ const subscriptionRequest = useRef(0);
+
+ useEffect(() => {
+ if (!open) setSubscriptionLoading(false);
+ return () => {
+ subscriptionRequest.current += 1;
+ };
+ }, [open]);
let planSubtext: string | undefined;
if (userDetails) {
@@ -157,21 +174,40 @@ export const Account: React.FC<AccountProps> = ({
}, [router]);
const handleManageSubscription = useCallback(() => {
+ if (subscriptionLoading) return;
+ const requestID = ++subscriptionRequest.current;
+ setSubscriptionLoading(true);
void (async () => {
- if (!userDetails) {
- await pullUserDetails();
+ try {
+ if (!userDetails) await pullUserDetails();
+ if (subscriptionRequest.current != requestID) return;
+ const resolvedUserDetails =
+ userDetails ?? userDetailsSnapshot();
+ if (!resolvedUserDetails) return;
+ await openManageSubscription({
+ userDetails: resolvedUserDetails,
+ showManageMemberSubscription,
+ onShowPlanSelector: async () => {
+ const plansData = await getPlansData();
+ if (subscriptionRequest.current == requestID)
+ onShowPlanSelector(plansData);
+ },
+ });
+ } catch (error) {
+ if (subscriptionRequest.current == requestID)
+ onGenericError(error);
+ } finally {
+ if (subscriptionRequest.current == requestID)
+ setSubscriptionLoading(false);
}
-
- const resolvedUserDetails = userDetails ?? userDetailsSnapshot();
- if (!resolvedUserDetails) return;
-
- openManageSubscription({
- userDetails: resolvedUserDetails,
- showManageMemberSubscription,
- onShowPlanSelector,
- });
})();
- }, [onShowPlanSelector, showManageMemberSubscription, userDetails]);
+ }, [
+ subscriptionLoading,
+ userDetails,
+ showManageMemberSubscription,
+ onShowPlanSelector,
+ onGenericError,
+ ]);
const handleRecoveryKey = useCallback(async () => {
if (isDesktop) {
@@ -326,6 +362,7 @@ export const Account: React.FC<AccountProps> = ({
title={t("manage_plan")}
subtitle={planSubtext}
onClick={handleManageSubscription}
+ loading={subscriptionLoading}
/>
<RowCard
title={t("recovery_key")}
diff --git a/web/apps/photos/src/components/sidebar/accounts/subscription.ts b/web/apps/photos/src/components/sidebar/accounts/subscription.ts
index 9bf8b7606f..a7d4475cda 100644
--- a/web/apps/photos/src/components/sidebar/accounts/subscription.ts
+++ b/web/apps/photos/src/components/sidebar/accounts/subscription.ts
@@ -14,7 +14,7 @@ export const openManageSubscription = ({
}: {
userDetails: UserDetails | undefined;
showManageMemberSubscription: () => void;
- onShowPlanSelector: () => void;
+ onShowPlanSelector: () => void | Promise<void>;
}) => {
if (
userDetails &&
@@ -32,6 +32,6 @@ export const openManageSubscription = ({
// eslint-disable-next-line @typescript-eslint/no-floating-promises
redirectToCustomerPortal();
} else {
- onShowPlanSelector();
+ return onShowPlanSelector();
}
};
diff --git a/web/apps/photos/src/pages/gallery.tsx b/web/apps/photos/src/pages/gallery.tsx
index f1b0adeb9f..ad3948dffd 100644
--- a/web/apps/photos/src/pages/gallery.tsx
+++ b/web/apps/photos/src/pages/gallery.tsx
@@ -184,6 +184,7 @@ import {
redirectToCustomerPortal,
savedUserDetailsOrTriggerPull,
verifyStripeSubscription,
+ type PlansData,
} from "ente-new/photos/services/user-details";
import { usePhotosAppContext } from "ente-new/photos/types/context";
import { PromiseQueue } from "ente-utils/promise";
@@ -304,8 +305,16 @@ const Page: React.FC = () => {
const pendingPeopleSearchReset = useRef<(() => void) | undefined>(
undefined,
);
- const { show: showPlanSelector, props: planSelectorVisibilityProps } =
+ const { show: showPlanSelectorModal, props: planSelectorVisibilityProps } =
useModalVisibility();
+ const [initialPlansData, setInitialPlansData] = useState<PlansData>();
+ const showPlanSelector = useCallback(
+ (plansData?: PlansData) => {
+ setInitialPlansData(plansData);
+ showPlanSelectorModal();
+ },
+ [showPlanSelectorModal],
+ );
const {
show: showFamilyManagement,
props: familyManagementVisibilityProps,
@@ -2121,6 +2130,7 @@ const Page: React.FC = () => {
<>
<PlanSelector
{...planSelectorVisibilityProps}
+ initialPlansData={initialPlansData}
setLoading={(v) => setBlockingLoad(v)}
onManageFamily={showFamilyManagement}
/>
diff --git a/web/packages/base/components/v2/RowCard.tsx b/web/packages/base/components/v2/RowCard.tsx
index 16b0be0ebc..daa6001951 100644
--- a/web/packages/base/components/v2/RowCard.tsx
+++ b/web/packages/base/components/v2/RowCard.tsx
@@ -1,5 +1,6 @@
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
import { Box, Stack, Typography } from "@mui/material";
+import { ActivityIndicator } from "ente-base/components/mui/ActivityIndicator";
import { FocusVisibleButton } from "ente-base/components/mui/FocusVisibleButton";
import React from "react";
@@ -9,6 +10,7 @@ interface RowCardProps {
subtitle?: string;
onClick?: () => void;
endIcon?: React.ReactNode;
+ loading?: boolean;
}
export const RowCard: React.FC<RowCardProps> = ({
@@ -16,11 +18,14 @@ export const RowCard: React.FC<RowCardProps> = ({
title,
subtitle,
onClick,
+ loading = false,
endIcon = <ChevronRightIcon className="ente-chevron-right" />,
}) => (
<Box
component={onClick ? FocusVisibleButton : "div"}
onClick={onClick}
+ disabled={onClick ? loading : undefined}
+ aria-busy={loading || undefined}
sx={[
{
display: "flex",
@@ -72,6 +77,6 @@ export const RowCard: React.FC<RowCardProps> = ({
</Typography>
)}
</Stack>
- {endIcon}
+ {loading ? <ActivityIndicator size={20} /> : endIcon}
</Box>
);