Skip to content

armgit / ente

Public Imported from GitHub
0

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

Inline

Manav Rathi88996eddea8e
diff --git a/web/apps/photos/src/components/pages/gallery/PlanSelector/card.tsx b/web/apps/photos/src/components/pages/gallery/PlanSelector/card.tsx
deleted file mode 100644
index af2335db49..0000000000
--- a/web/apps/photos/src/components/pages/gallery/PlanSelector/card.tsx
+++ /dev/null
@@ -1,575 +0,0 @@
-import log from "@/base/log";
-import { bytesInGB, formattedStorageByteSize } from "@/new/photos/utils/units";
-import {
- FluidContainer,
- SpaceBetweenFlex,
-} from "@ente/shared/components/Container";
-import ChevronRight from "@mui/icons-material/ChevronRight";
-import Close from "@mui/icons-material/Close";
-import {
- Button,
- ButtonProps,
- IconButton,
- Link,
- Stack,
- styled,
- ToggleButton,
- ToggleButtonGroup,
-} from "@mui/material";
-import Box from "@mui/material/Box";
-import Typography from "@mui/material/Typography";
-import { PLAN_PERIOD } from "constants/gallery";
-import { t } from "i18next";
-import { AppContext } from "pages/_app";
-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 { Plan, Subscription } from "types/billing";
-import { SetLoading } from "types/gallery";
-import { BonusData } from "types/user";
-import {
- activateSubscription,
- cancelSubscription,
- getLocalUserSubscription,
- hasAddOnBonus,
- hasMobileSubscription,
- hasPaidSubscription,
- hasStripeSubscription,
- isOnFreePlan,
- isSubscriptionActive,
- isSubscriptionCancelled,
- isUserSubscribedPlan,
- manageFamilyMethod,
- planForSubscription,
- updatePaymentMethod,
- updateSubscription,
-} from "utils/billing";
-import { getLocalUserDetails } from "utils/user";
-import { getTotalFamilyUsage, isPartOfFamily } from "utils/user/family";
-import Plans from "./plans";
-
-interface PlanSelectorCardProps {
- closeModal: any;
- setLoading: SetLoading;
-}
-
-function PlanSelectorCard(props: PlanSelectorCardProps) {
- const subscription = useMemo(() => getLocalUserSubscription(), []);
- const [plansResponse, setPlansResponse] = useState<
- PlansResponse | undefined
- >();
-
- const [planPeriod, setPlanPeriod] = useState<PLAN_PERIOD>(
- subscription?.period || PLAN_PERIOD.MONTH,
- );
- const galleryContext = useContext(GalleryContext);
- const appContext = useContext(AppContext);
- const bonusData = useMemo(() => {
- const userDetails = getLocalUserDetails();
- if (!userDetails) {
- return null;
- }
- return userDetails.bonusData;
- }, []);
-
- const usage = useMemo(() => {
- const userDetails = getLocalUserDetails();
- if (!userDetails) {
- return 0;
- }
- return isPartOfFamily(userDetails.familyData)
- ? getTotalFamilyUsage(userDetails.familyData)
- : userDetails.usage;
- }, []);
-
- const togglePeriod = () => {
- setPlanPeriod((prevPeriod) =>
- prevPeriod === PLAN_PERIOD.MONTH
- ? PLAN_PERIOD.YEAR
- : PLAN_PERIOD.MONTH,
- );
- };
- function onReopenClick() {
- appContext.closeMessageDialog();
- galleryContext.showPlanSelectorModal();
- }
- useEffect(() => {
- const main = async () => {
- try {
- props.setLoading(true);
- const response = await billingService.getPlans();
- const { plans } = response;
- if (isSubscriptionActive(subscription)) {
- const planNotListed =
- plans.filter((plan) =>
- isUserSubscribedPlan(plan, subscription),
- ).length === 0;
- if (
- subscription &&
- !isOnFreePlan(subscription) &&
- planNotListed
- ) {
- plans.push(planForSubscription(subscription));
- }
- }
- setPlansResponse(response);
- } catch (e) {
- log.error("plan selector modal open failed", e);
- props.closeModal();
- appContext.setDialogMessage({
- title: t("OPEN_PLAN_SELECTOR_MODAL_FAILED"),
- content: t("UNKNOWN_ERROR"),
- close: { text: t("CLOSE"), variant: "secondary" },
- proceed: {
- text: t("REOPEN_PLAN_SELECTOR_MODAL"),
- variant: "accent",
- action: onReopenClick,
- },
- });
- } finally {
- props.setLoading(false);
- }
- };
- main();
- }, []);
-
- async function onPlanSelect(plan: Plan) {
- if (
- !hasPaidSubscription(subscription) &&
- !isSubscriptionCancelled(subscription)
- ) {
- try {
- props.setLoading(true);
- await billingService.buySubscription(plan.stripeID);
- } catch (e) {
- props.setLoading(false);
- appContext.setDialogMessage({
- title: t("ERROR"),
- content: t("SUBSCRIPTION_PURCHASE_FAILED"),
- close: { variant: "critical" },
- });
- }
- } else if (hasStripeSubscription(subscription)) {
- appContext.setDialogMessage({
- title: t("update_subscription_title"),
- content: t("UPDATE_SUBSCRIPTION_MESSAGE"),
- proceed: {
- text: t("UPDATE_SUBSCRIPTION"),
- action: updateSubscription.bind(
- null,
- plan,
- appContext.setDialogMessage,
- props.setLoading,
- props.closeModal,
- ),
- variant: "accent",
- },
- close: { text: t("cancel") },
- });
- } else if (hasMobileSubscription(subscription)) {
- appContext.setDialogMessage({
- title: t("CANCEL_SUBSCRIPTION_ON_MOBILE"),
- content: t("CANCEL_SUBSCRIPTION_ON_MOBILE_MESSAGE"),
- close: { variant: "secondary" },
- });
- } else {
- appContext.setDialogMessage({
- title: t("MANAGE_PLAN"),
- content: (
- <Trans
- i18nKey={"MAIL_TO_MANAGE_SUBSCRIPTION"}
- components={{
- a: <Link href="mailto:[email protected]" />,
- }}
- values={{ emailID: "[email protected]" }}
- />
- ),
- close: { variant: "secondary" },
- });
- }
- }
-
- const { closeModal, setLoading } = props;
-
- const commonCardData = {
- subscription,
- bonusData,
- closeModal,
- planPeriod,
- togglePeriod,
- setLoading,
- };
-
- const plansList = (
- <Plans
- plansResponse={plansResponse}
- planPeriod={planPeriod}
- onPlanSelect={onPlanSelect}
- subscription={subscription}
- bonusData={bonusData}
- closeModal={closeModal}
- />
- );
-
- return (
- <>
- <Stack spacing={3} p={1.5}>
- {hasPaidSubscription(subscription) ? (
- <PaidSubscriptionPlanSelectorCard
- {...commonCardData}
- usage={usage}
- >
- {plansList}
- </PaidSubscriptionPlanSelectorCard>
- ) : (
- <FreeSubscriptionPlanSelectorCard {...commonCardData}>
- {plansList}
- </FreeSubscriptionPlanSelectorCard>
- )}
- </Stack>
- </>
- );
-}
-
-export default PlanSelectorCard;
-
-function FreeSubscriptionPlanSelectorCard({
- children,
- subscription,
- bonusData,
- closeModal,
- setLoading,
- planPeriod,
- togglePeriod,
-}) {
- return (
- <>
- <Typography variant="h3" fontWeight={"bold"}>
- {t("CHOOSE_PLAN")}
- </Typography>
-
- <Box>
- <Stack spacing={3}>
- <Box>
- <PeriodToggler
- planPeriod={planPeriod}
- togglePeriod={togglePeriod}
- />
- <Typography variant="small" mt={0.5} color="text.muted">
- {t("TWO_MONTHS_FREE")}
- </Typography>
- </Box>
- {children}
- {hasAddOnBonus(bonusData) && (
- <BFAddOnRow
- bonusData={bonusData}
- closeModal={closeModal}
- />
- )}
- {hasAddOnBonus(bonusData) && (
- <ManageSubscription
- subscription={subscription}
- bonusData={bonusData}
- closeModal={closeModal}
- setLoading={setLoading}
- />
- )}
- </Stack>
- </Box>
- </>
- );
-}
-
-function PaidSubscriptionPlanSelectorCard({
- children,
- subscription,
- bonusData,
- closeModal,
- usage,
- planPeriod,
- togglePeriod,
- setLoading,
-}) {
- return (
- <>
- <Box pl={1.5} py={0.5}>
- <SpaceBetweenFlex>
- <Box>
- <Typography variant="h3" fontWeight={"bold"}>
- {t("SUBSCRIPTION")}
- </Typography>
- <Typography variant="small" color={"text.muted"}>
- {bytesInGB(subscription.storage, 2)}{" "}
- {t("storage_unit.gb")}
- </Typography>
- </Box>
- <IconButton onClick={closeModal} color="secondary">
- <Close />
- </IconButton>
- </SpaceBetweenFlex>
- </Box>
-
- <Box px={1.5}>
- <Typography color={"text.muted"} fontWeight={"bold"}>
- <Trans
- i18nKey="CURRENT_USAGE"
- values={{
- usage: `${bytesInGB(usage, 2)} ${t("storage_unit.gb")}`,
- }}
- />
- </Typography>
- </Box>
-
- <Box>
- <Stack
- spacing={3}
- border={(theme) => `1px solid ${theme.palette.divider}`}
- p={1.5}
- borderRadius={(theme) => `${theme.shape.borderRadius}px`}
- >
- <Box>
- <PeriodToggler
- planPeriod={planPeriod}
- togglePeriod={togglePeriod}
- />
- <Typography variant="small" mt={0.5} color="text.muted">
- {t("TWO_MONTHS_FREE")}
- </Typography>
- </Box>
- {children}
- </Stack>
-
- <Box py={1} px={1.5}>
- <Typography color={"text.muted"}>
- {!isSubscriptionCancelled(subscription)
- ? t("subscription_status_renewal_active", {
- date: subscription.expiryTime,
- })
- : t("subscription_status_renewal_cancelled", {
- date: subscription.expiryTime,
- })}
- </Typography>
- {hasAddOnBonus(bonusData) && (
- <BFAddOnRow
- bonusData={bonusData}
- closeModal={closeModal}
- />
- )}
- </Box>
- </Box>
-
- <ManageSubscription
- subscription={subscription}
- bonusData={bonusData}
- closeModal={closeModal}
- setLoading={setLoading}
- />
- </>
- );
-}
-
-function PeriodToggler({ planPeriod, togglePeriod }) {
- const handleChange = (_, newPlanPeriod: PLAN_PERIOD) => {
- if (newPlanPeriod !== null && newPlanPeriod !== planPeriod) {
- togglePeriod();
- }
- };
-
- return (
- <ToggleButtonGroup
- value={planPeriod}
- exclusive
- onChange={handleChange}
- color="primary"
- >
- <CustomToggleButton value={PLAN_PERIOD.MONTH}>
- {t("MONTHLY")}
- </CustomToggleButton>
- <CustomToggleButton value={PLAN_PERIOD.YEAR}>
- {t("YEARLY")}
- </CustomToggleButton>
- </ToggleButtonGroup>
- );
-}
-
-const CustomToggleButton = styled(ToggleButton)(({ theme }) => ({
- textTransform: "none",
- padding: "12px 16px",
- borderRadius: "4px",
- backgroundColor: theme.colors.fill.faint,
- border: `1px solid transparent`,
- color: theme.colors.text.faint,
- "&.Mui-selected": {
- backgroundColor: theme.colors.accent.A500,
- color: theme.colors.text.base,
- },
- "&.Mui-selected:hover": {
- backgroundColor: theme.colors.accent.A500,
- color: theme.colors.text.base,
- },
- width: "97.433px",
-}));
-
-function BFAddOnRow({ bonusData, closeModal }) {
- return (
- <>
- {bonusData.storageBonuses.map((bonus) => {
- if (bonus.type.startsWith("ADD_ON")) {
- return (
- <AddOnRowContainer key={bonus.id} onClick={closeModal}>
- <Box>
- <Typography color="text.muted">
- <Trans
- i18nKey={"add_on_valid_till"}
- values={{
- storage: formattedStorageByteSize(
- bonus.storage,
- ),
- date: bonus.validTill,
- }}
- />
- </Typography>
- </Box>
- </AddOnRowContainer>
- );
- }
- return null;
- })}
- </>
- );
-}
-
-const AddOnRowContainer = styled(SpaceBetweenFlex)(({ theme }) => ({
- // gap: theme.spacing(1.5),
- padding: theme.spacing(1, 0),
- cursor: "pointer",
- "&:hover .endIcon": {
- backgroundColor: "rgba(255,255,255,0.08)",
- },
-}));
-
-interface ManageSubscriptionProps {
- subscription: Subscription;
- bonusData?: BonusData;
- closeModal: () => void;
- setLoading: SetLoading;
-}
-
-function ManageSubscription({
- subscription,
- bonusData,
- closeModal,
- setLoading,
-}: ManageSubscriptionProps) {
- const appContext = useContext(AppContext);
- const openFamilyPortal = () =>
- manageFamilyMethod(appContext.setDialogMessage, setLoading);
-
- return (
- <Stack spacing={1}>
- {hasStripeSubscription(subscription) && (
- <StripeSubscriptionOptions
- subscription={subscription}
- bonusData={bonusData}
- closeModal={closeModal}
- setLoading={setLoading}
- />
- )}
- <ManageSubscriptionButton
- color="secondary"
- onClick={openFamilyPortal}
- >
- {t("MANAGE_FAMILY_PORTAL")}
- </ManageSubscriptionButton>
- </Stack>
- );
-}
-
-function StripeSubscriptionOptions({
- subscription,
- bonusData,
- setLoading,
- closeModal,
-}: ManageSubscriptionProps) {
- const appContext = useContext(AppContext);
-
- const confirmReactivation = () =>
- appContext.setDialogMessage({
- title: t("REACTIVATE_SUBSCRIPTION"),
- content: t("REACTIVATE_SUBSCRIPTION_MESSAGE", {
- date: subscription.expiryTime,
- }),
- proceed: {
- text: t("REACTIVATE_SUBSCRIPTION"),
- action: activateSubscription.bind(
- null,
- appContext.setDialogMessage,
- closeModal,
- setLoading,
- ),
- variant: "accent",
- },
- close: {
- text: t("cancel"),
- },
- });
- const confirmCancel = () =>
- appContext.setDialogMessage({
- title: t("CANCEL_SUBSCRIPTION"),
- content: hasAddOnBonus(bonusData) ? (
- <Trans i18nKey={"CANCEL_SUBSCRIPTION_WITH_ADDON_MESSAGE"} />
- ) : (
- <Trans i18nKey={"CANCEL_SUBSCRIPTION_MESSAGE"} />
- ),
- proceed: {
- text: t("CANCEL_SUBSCRIPTION"),
- action: cancelSubscription.bind(
- null,
- appContext.setDialogMessage,
- closeModal,
- setLoading,
- ),
- variant: "critical",
- },
- close: {
- text: t("NEVERMIND"),
- },
- });
- const openManagementPortal = updatePaymentMethod.bind(
- null,
- appContext.setDialogMessage,
- setLoading,
- );
- return (
- <>
- {isSubscriptionCancelled(subscription) ? (
- <ManageSubscriptionButton
- color="secondary"
- onClick={confirmReactivation}
- >
- {t("REACTIVATE_SUBSCRIPTION")}
- </ManageSubscriptionButton>
- ) : (
- <ManageSubscriptionButton
- color="secondary"
- onClick={confirmCancel}
- >
- {t("CANCEL_SUBSCRIPTION")}
- </ManageSubscriptionButton>
- )}
- <ManageSubscriptionButton
- color="secondary"
- onClick={openManagementPortal}
- >
- {t("MANAGEMENT_PORTAL")}
- </ManageSubscriptionButton>
- </>
- );
-}
-
-const ManageSubscriptionButton = ({ children, ...props }: ButtonProps) => (
- <Button size="large" endIcon={<ChevronRight />} {...props}>
- <FluidContainer>{children}</FluidContainer>
- </Button>
-);
diff --git a/web/apps/photos/src/components/pages/gallery/PlanSelector/index.tsx b/web/apps/photos/src/components/pages/gallery/PlanSelector/index.tsx
index d58c455158..ed3d34d3a0 100644
--- a/web/apps/photos/src/components/pages/gallery/PlanSelector/index.tsx
+++ b/web/apps/photos/src/components/pages/gallery/PlanSelector/index.tsx
@@ -1,6 +1,56 @@
-import { Dialog, useMediaQuery, useTheme } from "@mui/material";
+import log from "@/base/log";
+import { bytesInGB, formattedStorageByteSize } from "@/new/photos/utils/units";
+import {
+ FluidContainer,
+ SpaceBetweenFlex,
+} from "@ente/shared/components/Container";
+import ChevronRight from "@mui/icons-material/ChevronRight";
+import Close from "@mui/icons-material/Close";
+import {
+ Button,
+ ButtonProps,
+ Dialog,
+ IconButton,
+ Link,
+ Stack,
+ styled,
+ ToggleButton,
+ ToggleButtonGroup,
+ useMediaQuery,
+ useTheme,
+} from "@mui/material";
+import Box from "@mui/material/Box";
+import Typography from "@mui/material/Typography";
+import { PLAN_PERIOD } from "constants/gallery";
+import { t } from "i18next";
+import { AppContext } from "pages/_app";
+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 { Plan, Subscription } from "types/billing";
import { SetLoading } from "types/gallery";
-import PlanSelectorCard from "./card";
+import { BonusData } from "types/user";
+import {
+ activateSubscription,
+ cancelSubscription,
+ getLocalUserSubscription,
+ hasAddOnBonus,
+ hasMobileSubscription,
+ hasPaidSubscription,
+ hasStripeSubscription,
+ isOnFreePlan,
+ isSubscriptionActive,
+ isSubscriptionCancelled,
+ isUserSubscribedPlan,
+ manageFamilyMethod,
+ planForSubscription,
+ updatePaymentMethod,
+ updateSubscription,
+} from "utils/billing";
+import { getLocalUserDetails } from "utils/user";
+import { getTotalFamilyUsage, isPartOfFamily } from "utils/user/family";
+import Plans from "./plans";
interface PlanSelectorProps {
modalView: boolean;
@@ -37,3 +87,526 @@ function PlanSelector(props: PlanSelectorProps) {
}
export default PlanSelector;
+
+interface PlanSelectorCardProps {
+ closeModal: any;
+ setLoading: SetLoading;
+}
+
+function PlanSelectorCard(props: PlanSelectorCardProps) {
+ const subscription = useMemo(() => getLocalUserSubscription(), []);
+ const [plansResponse, setPlansResponse] = useState<
+ PlansResponse | undefined
+ >();
+
+ const [planPeriod, setPlanPeriod] = useState<PLAN_PERIOD>(
+ subscription?.period || PLAN_PERIOD.MONTH,
+ );
+ const galleryContext = useContext(GalleryContext);
+ const appContext = useContext(AppContext);
+ const bonusData = useMemo(() => {
+ const userDetails = getLocalUserDetails();
+ if (!userDetails) {
+ return null;
+ }
+ return userDetails.bonusData;
+ }, []);
+
+ const usage = useMemo(() => {
+ const userDetails = getLocalUserDetails();
+ if (!userDetails) {
+ return 0;
+ }
+ return isPartOfFamily(userDetails.familyData)
+ ? getTotalFamilyUsage(userDetails.familyData)
+ : userDetails.usage;
+ }, []);
+
+ const togglePeriod = () => {
+ setPlanPeriod((prevPeriod) =>
+ prevPeriod === PLAN_PERIOD.MONTH
+ ? PLAN_PERIOD.YEAR
+ : PLAN_PERIOD.MONTH,
+ );
+ };
+ function onReopenClick() {
+ appContext.closeMessageDialog();
+ galleryContext.showPlanSelectorModal();
+ }
+ useEffect(() => {
+ const main = async () => {
+ try {
+ props.setLoading(true);
+ const response = await billingService.getPlans();
+ const { plans } = response;
+ if (isSubscriptionActive(subscription)) {
+ const planNotListed =
+ plans.filter((plan) =>
+ isUserSubscribedPlan(plan, subscription),
+ ).length === 0;
+ if (
+ subscription &&
+ !isOnFreePlan(subscription) &&
+ planNotListed
+ ) {
+ plans.push(planForSubscription(subscription));
+ }
+ }
+ setPlansResponse(response);
+ } catch (e) {
+ log.error("plan selector modal open failed", e);
+ props.closeModal();
+ appContext.setDialogMessage({
+ title: t("OPEN_PLAN_SELECTOR_MODAL_FAILED"),
+ content: t("UNKNOWN_ERROR"),
+ close: { text: t("CLOSE"), variant: "secondary" },
+ proceed: {
+ text: t("REOPEN_PLAN_SELECTOR_MODAL"),
+ variant: "accent",
+ action: onReopenClick,
+ },
+ });
+ } finally {
+ props.setLoading(false);
+ }
+ };
+ main();
+ }, []);
+
+ async function onPlanSelect(plan: Plan) {
+ if (
+ !hasPaidSubscription(subscription) &&
+ !isSubscriptionCancelled(subscription)
+ ) {
+ try {
+ props.setLoading(true);
+ await billingService.buySubscription(plan.stripeID);
+ } catch (e) {
+ props.setLoading(false);
+ appContext.setDialogMessage({
+ title: t("ERROR"),
+ content: t("SUBSCRIPTION_PURCHASE_FAILED"),
+ close: { variant: "critical" },
+ });
+ }
+ } else if (hasStripeSubscription(subscription)) {
+ appContext.setDialogMessage({
+ title: t("update_subscription_title"),
+ content: t("UPDATE_SUBSCRIPTION_MESSAGE"),
+ proceed: {
+ text: t("UPDATE_SUBSCRIPTION"),
+ action: updateSubscription.bind(
+ null,
+ plan,
+ appContext.setDialogMessage,
+ props.setLoading,
+ props.closeModal,
+ ),
+ variant: "accent",
+ },
+ close: { text: t("cancel") },
+ });
+ } else if (hasMobileSubscription(subscription)) {
+ appContext.setDialogMessage({
+ title: t("CANCEL_SUBSCRIPTION_ON_MOBILE"),
+ content: t("CANCEL_SUBSCRIPTION_ON_MOBILE_MESSAGE"),
+ close: { variant: "secondary" },
+ });
+ } else {
+ appContext.setDialogMessage({
+ title: t("MANAGE_PLAN"),
+ content: (
+ <Trans
+ i18nKey={"MAIL_TO_MANAGE_SUBSCRIPTION"}
+ components={{
+ a: <Link href="mailto:[email protected]" />,
+ }}
+ values={{ emailID: "[email protected]" }}
+ />
+ ),
+ close: { variant: "secondary" },
+ });
+ }
+ }
+
+ const { closeModal, setLoading } = props;
+
+ const commonCardData = {
+ subscription,
+ bonusData,
+ closeModal,
+ planPeriod,
+ togglePeriod,
+ setLoading,
+ };
+
+ const plansList = (
+ <Plans
+ plansResponse={plansResponse}
+ planPeriod={planPeriod}
+ onPlanSelect={onPlanSelect}
+ subscription={subscription}
+ bonusData={bonusData}
+ closeModal={closeModal}
+ />
+ );
+
+ return (
+ <>
+ <Stack spacing={3} p={1.5}>
+ {hasPaidSubscription(subscription) ? (
+ <PaidSubscriptionPlanSelectorCard
+ {...commonCardData}
+ usage={usage}
+ >
+ {plansList}
+ </PaidSubscriptionPlanSelectorCard>
+ ) : (
+ <FreeSubscriptionPlanSelectorCard {...commonCardData}>
+ {plansList}
+ </FreeSubscriptionPlanSelectorCard>
+ )}
+ </Stack>
+ </>
+ );
+}
+
+function FreeSubscriptionPlanSelectorCard({
+ children,
+ subscription,
+ bonusData,
+ closeModal,
+ setLoading,
+ planPeriod,
+ togglePeriod,
+}) {
+ return (
+ <>
+ <Typography variant="h3" fontWeight={"bold"}>
+ {t("CHOOSE_PLAN")}
+ </Typography>
+
+ <Box>
+ <Stack spacing={3}>
+ <Box>
+ <PeriodToggler
+ planPeriod={planPeriod}
+ togglePeriod={togglePeriod}
+ />
+ <Typography variant="small" mt={0.5} color="text.muted">
+ {t("TWO_MONTHS_FREE")}
+ </Typography>
+ </Box>
+ {children}
+ {hasAddOnBonus(bonusData) && (
+ <BFAddOnRow
+ bonusData={bonusData}
+ closeModal={closeModal}
+ />
+ )}
+ {hasAddOnBonus(bonusData) && (
+ <ManageSubscription
+ subscription={subscription}
+ bonusData={bonusData}
+ closeModal={closeModal}
+ setLoading={setLoading}
+ />
+ )}
+ </Stack>
+ </Box>
+ </>
+ );
+}
+
+function PaidSubscriptionPlanSelectorCard({
+ children,
+ subscription,
+ bonusData,
+ closeModal,
+ usage,
+ planPeriod,
+ togglePeriod,
+ setLoading,
+}) {
+ return (
+ <>
+ <Box pl={1.5} py={0.5}>
+ <SpaceBetweenFlex>
+ <Box>
+ <Typography variant="h3" fontWeight={"bold"}>
+ {t("SUBSCRIPTION")}
+ </Typography>
+ <Typography variant="small" color={"text.muted"}>
+ {bytesInGB(subscription.storage, 2)}{" "}
+ {t("storage_unit.gb")}
+ </Typography>
+ </Box>
+ <IconButton onClick={closeModal} color="secondary">
+ <Close />
+ </IconButton>
+ </SpaceBetweenFlex>
+ </Box>
+
+ <Box px={1.5}>
+ <Typography color={"text.muted"} fontWeight={"bold"}>
+ <Trans
+ i18nKey="CURRENT_USAGE"
+ values={{
+ usage: `${bytesInGB(usage, 2)} ${t("storage_unit.gb")}`,
+ }}
+ />
+ </Typography>
+ </Box>
+
+ <Box>
+ <Stack
+ spacing={3}
+ border={(theme) => `1px solid ${theme.palette.divider}`}
+ p={1.5}
+ borderRadius={(theme) => `${theme.shape.borderRadius}px`}
+ >
+ <Box>
+ <PeriodToggler
+ planPeriod={planPeriod}
+ togglePeriod={togglePeriod}
+ />
+ <Typography variant="small" mt={0.5} color="text.muted">
+ {t("TWO_MONTHS_FREE")}
+ </Typography>
+ </Box>
+ {children}
+ </Stack>
+
+ <Box py={1} px={1.5}>
+ <Typography color={"text.muted"}>
+ {!isSubscriptionCancelled(subscription)
+ ? t("subscription_status_renewal_active", {
+ date: subscription.expiryTime,
+ })
+ : t("subscription_status_renewal_cancelled", {
+ date: subscription.expiryTime,
+ })}
+ </Typography>
+ {hasAddOnBonus(bonusData) && (
+ <BFAddOnRow
+ bonusData={bonusData}
+ closeModal={closeModal}
+ />
+ )}
+ </Box>
+ </Box>
+
+ <ManageSubscription
+ subscription={subscription}
+ bonusData={bonusData}
+ closeModal={closeModal}
+ setLoading={setLoading}
+ />
+ </>
+ );
+}
+
+function PeriodToggler({ planPeriod, togglePeriod }) {
+ const handleChange = (_, newPlanPeriod: PLAN_PERIOD) => {
+ if (newPlanPeriod !== null && newPlanPeriod !== planPeriod) {
+ togglePeriod();
+ }
+ };
+
+ return (
+ <ToggleButtonGroup
+ value={planPeriod}
+ exclusive
+ onChange={handleChange}
+ color="primary"
+ >
+ <CustomToggleButton value={PLAN_PERIOD.MONTH}>
+ {t("MONTHLY")}
+ </CustomToggleButton>
+ <CustomToggleButton value={PLAN_PERIOD.YEAR}>
+ {t("YEARLY")}
+ </CustomToggleButton>
+ </ToggleButtonGroup>
+ );
+}
+
+const CustomToggleButton = styled(ToggleButton)(({ theme }) => ({
+ textTransform: "none",
+ padding: "12px 16px",
+ borderRadius: "4px",
+ backgroundColor: theme.colors.fill.faint,
+ border: `1px solid transparent`,
+ color: theme.colors.text.faint,
+ "&.Mui-selected": {
+ backgroundColor: theme.colors.accent.A500,
+ color: theme.colors.text.base,
+ },
+ "&.Mui-selected:hover": {
+ backgroundColor: theme.colors.accent.A500,
+ color: theme.colors.text.base,
+ },
+ width: "97.433px",
+}));
+
+function BFAddOnRow({ bonusData, closeModal }) {
+ return (
+ <>
+ {bonusData.storageBonuses.map((bonus) => {
+ if (bonus.type.startsWith("ADD_ON")) {
+ return (
+ <AddOnRowContainer key={bonus.id} onClick={closeModal}>
+ <Box>
+ <Typography color="text.muted">
+ <Trans
+ i18nKey={"add_on_valid_till"}
+ values={{
+ storage: formattedStorageByteSize(
+ bonus.storage,
+ ),
+ date: bonus.validTill,
+ }}
+ />
+ </Typography>
+ </Box>
+ </AddOnRowContainer>
+ );
+ }
+ return null;
+ })}
+ </>
+ );
+}
+
+const AddOnRowContainer = styled(SpaceBetweenFlex)(({ theme }) => ({
+ // gap: theme.spacing(1.5),
+ padding: theme.spacing(1, 0),
+ cursor: "pointer",
+ "&:hover .endIcon": {
+ backgroundColor: "rgba(255,255,255,0.08)",
+ },
+}));
+
+interface ManageSubscriptionProps {
+ subscription: Subscription;
+ bonusData?: BonusData;
+ closeModal: () => void;
+ setLoading: SetLoading;
+}
+
+function ManageSubscription({
+ subscription,
+ bonusData,
+ closeModal,
+ setLoading,
+}: ManageSubscriptionProps) {
+ const appContext = useContext(AppContext);
+ const openFamilyPortal = () =>
+ manageFamilyMethod(appContext.setDialogMessage, setLoading);
+
+ return (
+ <Stack spacing={1}>
+ {hasStripeSubscription(subscription) && (
+ <StripeSubscriptionOptions
+ subscription={subscription}
+ bonusData={bonusData}
+ closeModal={closeModal}
+ setLoading={setLoading}
+ />
+ )}
+ <ManageSubscriptionButton
+ color="secondary"
+ onClick={openFamilyPortal}
+ >
+ {t("MANAGE_FAMILY_PORTAL")}
+ </ManageSubscriptionButton>
+ </Stack>
+ );
+}
+
+function StripeSubscriptionOptions({
+ subscription,
+ bonusData,
+ setLoading,
+ closeModal,
+}: ManageSubscriptionProps) {
+ const appContext = useContext(AppContext);
+
+ const confirmReactivation = () =>
+ appContext.setDialogMessage({
+ title: t("REACTIVATE_SUBSCRIPTION"),
+ content: t("REACTIVATE_SUBSCRIPTION_MESSAGE", {
+ date: subscription.expiryTime,
+ }),
+ proceed: {
+ text: t("REACTIVATE_SUBSCRIPTION"),
+ action: activateSubscription.bind(
+ null,
+ appContext.setDialogMessage,
+ closeModal,
+ setLoading,
+ ),
+ variant: "accent",
+ },
+ close: {
+ text: t("cancel"),
+ },
+ });
+ const confirmCancel = () =>
+ appContext.setDialogMessage({
+ title: t("CANCEL_SUBSCRIPTION"),
+ content: hasAddOnBonus(bonusData) ? (
+ <Trans i18nKey={"CANCEL_SUBSCRIPTION_WITH_ADDON_MESSAGE"} />
+ ) : (
+ <Trans i18nKey={"CANCEL_SUBSCRIPTION_MESSAGE"} />
+ ),
+ proceed: {
+ text: t("CANCEL_SUBSCRIPTION"),
+ action: cancelSubscription.bind(
+ null,
+ appContext.setDialogMessage,
+ closeModal,
+ setLoading,
+ ),
+ variant: "critical",
+ },
+ close: {
+ text: t("NEVERMIND"),
+ },
+ });
+ const openManagementPortal = updatePaymentMethod.bind(
+ null,
+ appContext.setDialogMessage,
+ setLoading,
+ );
+ return (
+ <>
+ {isSubscriptionCancelled(subscription) ? (
+ <ManageSubscriptionButton
+ color="secondary"
+ onClick={confirmReactivation}
+ >
+ {t("REACTIVATE_SUBSCRIPTION")}
+ </ManageSubscriptionButton>
+ ) : (
+ <ManageSubscriptionButton
+ color="secondary"
+ onClick={confirmCancel}
+ >
+ {t("CANCEL_SUBSCRIPTION")}
+ </ManageSubscriptionButton>
+ )}
+ <ManageSubscriptionButton
+ color="secondary"
+ onClick={openManagementPortal}
+ >
+ {t("MANAGEMENT_PORTAL")}
+ </ManageSubscriptionButton>
+ </>
+ );
+}
+
+const ManageSubscriptionButton = ({ children, ...props }: ButtonProps) => (
+ <Button size="large" endIcon={<ChevronRight />} {...props}>
+ <FluidContainer>{children}</FluidContainer>
+ </Button>
+);