ntfy/web/src/components/UpgradeDialog.jsx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

436 lines
15 KiB
React
Raw Normal View History

2023-01-10 09:40:46 +13:00
import * as React from "react";
2023-01-31 07:10:45 +13:00
import { useContext, useEffect, useState } from "react";
import {
Dialog,
DialogContent,
DialogTitle,
Alert,
CardActionArea,
CardContent,
Chip,
Link,
ListItem,
Switch,
useMediaQuery,
Button,
Card,
Typography,
List,
ListItemIcon,
ListItemText,
Box,
DialogContentText,
DialogActions,
} from "@mui/material";
2023-01-18 13:40:03 +13:00
import { Trans, useTranslation } from "react-i18next";
2023-02-22 16:44:30 +13:00
import { Check, Close } from "@mui/icons-material";
import { NavLink } from "react-router-dom";
2023-02-03 09:19:37 +13:00
import { UnauthorizedError } from "../app/errors";
2023-02-22 16:44:30 +13:00
import { formatBytes, formatNumber, formatPrice, formatShortDate } from "../app/utils";
2023-01-15 00:43:44 +13:00
import { AccountContext } from "./App";
import routes from "./routes";
import session from "../app/Session";
2023-02-22 16:44:30 +13:00
import accountApi, { SubscriptionInterval } from "../app/AccountApi";
2023-01-10 09:40:46 +13:00
import theme from "./theme";
const Feature = (props) => <FeatureItem feature>{props.children}</FeatureItem>;
const NoFeature = (props) => <FeatureItem feature={false}>{props.children}</FeatureItem>;
const FeatureItem = (props) => (
<ListItem disableGutters sx={{ m: 0, p: 0 }}>
<ListItemIcon sx={{ minWidth: "24px" }}>
{props.feature && <Check fontSize="small" sx={{ color: "#338574" }} />}
{!props.feature && <Close fontSize="small" sx={{ color: "gray" }} />}
</ListItemIcon>
<ListItemText sx={{ mt: "2px", mb: "2px" }} primary={<Typography variant="body1">{props.children}</Typography>} />
</ListItem>
);
const Action = {
REDIRECT_SIGNUP: 1,
CREATE_SUBSCRIPTION: 2,
UPDATE_SUBSCRIPTION: 3,
CANCEL_SUBSCRIPTION: 4,
};
const Banner = {
CANCEL_WARNING: 1,
PRORATION_INFO: 2,
RESERVATIONS_WARNING: 3,
};
2023-01-10 09:40:46 +13:00
const UpgradeDialog = (props) => {
const { t } = useTranslation();
const { account } = useContext(AccountContext); // May be undefined!
2023-02-03 09:19:37 +13:00
const [error, setError] = useState("");
2023-01-18 04:09:37 +13:00
const [tiers, setTiers] = useState(null);
2023-02-22 16:44:30 +13:00
const [interval, setInterval] = useState(account?.billing?.interval || SubscriptionInterval.YEAR);
const [newTierCode, setNewTierCode] = useState(account?.tier?.code); // May be undefined
2023-01-18 14:21:19 +13:00
const [loading, setLoading] = useState(false);
2023-02-03 09:19:37 +13:00
const fullScreen = useMediaQuery(theme.breakpoints.down("sm"));
2023-01-10 09:40:46 +13:00
2023-01-18 04:09:37 +13:00
useEffect(() => {
2023-02-12 14:38:13 +13:00
const fetchTiers = async () => {
setTiers(await accountApi.billingTiers());
};
fetchTiers(); // Dangle
2023-01-18 04:09:37 +13:00
}, []);
if (!tiers) {
return <></>;
}
const tiersMap = Object.assign(...tiers.map((tier) => ({ [tier.code]: tier })));
const newTier = tiersMap[newTierCode]; // May be undefined
const currentTier = account?.tier; // May be undefined
2023-02-22 16:44:30 +13:00
const currentInterval = account?.billing?.interval; // May be undefined
const currentTierCode = currentTier?.code; // May be undefined
// Figure out buttons, labels and the submit action
let submitAction;
let submitButtonLabel;
let banner;
if (!account) {
submitButtonLabel = t("account_upgrade_dialog_button_redirect_signup");
submitAction = Action.REDIRECT_SIGNUP;
banner = null;
} else if (currentTierCode === newTierCode && (currentInterval === undefined || currentInterval === interval)) {
2023-01-18 13:40:03 +13:00
submitButtonLabel = t("account_upgrade_dialog_button_update_subscription");
submitAction = null;
banner = currentTierCode ? Banner.PRORATION_INFO : null;
} else if (!currentTierCode) {
2023-01-18 13:40:03 +13:00
submitButtonLabel = t("account_upgrade_dialog_button_pay_now");
submitAction = Action.CREATE_SUBSCRIPTION;
banner = null;
} else if (!newTierCode) {
2023-01-18 13:40:03 +13:00
submitButtonLabel = t("account_upgrade_dialog_button_cancel_subscription");
submitAction = Action.CANCEL_SUBSCRIPTION;
banner = Banner.CANCEL_WARNING;
} else {
2023-01-18 13:40:03 +13:00
submitButtonLabel = t("account_upgrade_dialog_button_update_subscription");
submitAction = Action.UPDATE_SUBSCRIPTION;
banner = Banner.PRORATION_INFO;
}
// Exceptional conditions
2023-01-18 14:21:19 +13:00
if (loading) {
submitAction = null;
2023-01-22 02:55:31 +13:00
} else if (newTier?.code && account?.reservations?.length > newTier?.limits?.reservations) {
submitAction = null;
banner = Banner.RESERVATIONS_WARNING;
2023-01-18 14:21:19 +13:00
}
const handleSubmit = async () => {
if (submitAction === Action.REDIRECT_SIGNUP) {
window.location.href = routes.signup;
2023-01-15 00:43:44 +13:00
return;
2023-01-10 09:40:46 +13:00
}
2023-05-24 07:13:01 +12:00
try {
2023-01-18 14:21:19 +13:00
setLoading(true);
if (submitAction === Action.CREATE_SUBSCRIPTION) {
2023-02-22 16:44:30 +13:00
const response = await accountApi.createBillingSubscription(newTierCode, interval);
window.location.href = response.redirect_url;
} else if (submitAction === Action.UPDATE_SUBSCRIPTION) {
2023-02-22 16:44:30 +13:00
await accountApi.updateBillingSubscription(newTierCode, interval);
} else if (submitAction === Action.CANCEL_SUBSCRIPTION) {
2023-01-16 17:29:46 +13:00
await accountApi.deleteBillingSubscription();
2023-05-24 07:13:01 +12:00
}
props.onCancel();
2023-01-15 00:43:44 +13:00
} catch (e) {
console.log(`[UpgradeDialog] Error changing billing subscription`, e);
2023-02-03 09:19:37 +13:00
if (e instanceof UnauthorizedError) {
await session.resetAndRedirect(routes.login);
2023-05-24 07:13:01 +12:00
} else {
2023-02-03 09:19:37 +13:00
setError(e.message);
2023-05-24 07:13:01 +12:00
}
2023-01-18 14:21:19 +13:00
} finally {
setLoading(false);
2023-05-24 07:13:01 +12:00
}
};
2023-01-10 09:40:46 +13:00
2023-02-22 16:44:30 +13:00
// Figure out discount
2023-02-23 08:21:23 +13:00
let discount = 0;
let upto = false;
2023-02-22 16:44:30 +13:00
if (newTier?.prices) {
discount = Math.round(((newTier.prices.month * 12) / newTier.prices.year - 1) * 100);
} else {
2023-02-23 08:21:23 +13:00
let n = 0;
for (const tier of tiers) {
if (tier.prices) {
const tierDiscount = Math.round(((tier.prices.month * 12) / tier.prices.year - 1) * 100);
2023-02-23 08:21:23 +13:00
if (tierDiscount > discount) {
discount = tierDiscount;
n += 1;
2023-02-22 16:44:30 +13:00
}
2023-05-24 07:13:01 +12:00
}
2023-02-22 16:44:30 +13:00
}
2023-02-23 08:21:23 +13:00
upto = n > 1;
2023-05-24 07:13:01 +12:00
}
2023-02-22 16:44:30 +13:00
2023-01-10 09:40:46 +13:00
return (
2023-01-18 13:40:03 +13:00
<Dialog open={props.open} onClose={props.onCancel} maxWidth="lg" fullScreen={fullScreen}>
2023-02-22 16:44:30 +13:00
<DialogTitle>
<div style={{ display: "flex", flexDirection: "row" }}>
<div style={{ flexGrow: 1 }}>{t("account_upgrade_dialog_title")}</div>
<div
style={{
display: "flex",
flexDirection: "row",
alignItems: "center",
marginTop: "4px",
}}
>
<Typography component="span" variant="subtitle1">
2023-02-23 08:21:23 +13:00
{t("account_upgrade_dialog_interval_monthly")}
</Typography>
2023-05-24 07:13:01 +12:00
<Switch
2023-02-23 08:21:23 +13:00
checked={interval === SubscriptionInterval.YEAR}
onChange={(ev) => setInterval(ev.target.checked ? SubscriptionInterval.YEAR : SubscriptionInterval.MONTH)}
2023-01-18 04:09:37 +13:00
/>
2023-02-22 16:44:30 +13:00
<Typography component="span" variant="subtitle1">
{t("account_upgrade_dialog_interval_yearly")}
</Typography>
{discount > 0 && (
2023-05-24 07:13:01 +12:00
<Chip
2023-02-22 16:44:30 +13:00
label={
2023-05-24 07:13:01 +12:00
upto
2023-02-22 16:44:30 +13:00
? t("account_upgrade_dialog_interval_yearly_discount_save_up_to", { discount })
2023-01-18 13:40:03 +13:00
: t("account_upgrade_dialog_interval_yearly_discount_save", { discount })
2023-01-18 04:09:37 +13:00
}
2023-02-23 08:21:23 +13:00
color="primary"
size="small"
variant={interval === SubscriptionInterval.YEAR ? "filled" : "outlined"}
2023-02-23 08:21:23 +13:00
sx={{ marginLeft: "5px" }}
2023-05-24 07:13:01 +12:00
/>
)}
2023-01-10 09:40:46 +13:00
</div>
2023-05-24 07:13:01 +12:00
</div>
2023-02-22 16:44:30 +13:00
</DialogTitle>
2023-01-10 09:40:46 +13:00
<DialogContent>
2023-03-01 08:38:31 +13:00
<div
style={{
display: "flex",
flexDirection: "row",
marginBottom: "8px",
2023-01-18 04:09:37 +13:00
width: "100%",
2023-05-24 07:13:01 +12:00
}}
>
2023-01-18 04:09:37 +13:00
{tiers.map((tier) => (
<TierCard
2023-01-18 13:40:03 +13:00
key={`tierCard${tier.code || "_free"}`}
tier={tier}
current={currentTierCode === tier.code} // tier.code or currentTierCode may be undefined!
selected={newTierCode === tier.code} // tier.code may be undefined!
2023-02-22 16:44:30 +13:00
interval={interval}
onClick={() => setNewTierCode(tier.code)} // tier.code may be undefined!
2023-05-24 07:13:01 +12:00
/>
))}
2023-03-01 08:38:31 +13:00
</div>
{banner === Banner.CANCEL_WARNING && (
<Alert severity="warning" sx={{ fontSize: "1rem" }}>
2023-05-24 07:13:01 +12:00
<Trans
i18nKey="account_upgrade_dialog_cancel_warning"
2023-05-24 07:13:01 +12:00
values={{
date: formatShortDate(account?.billing?.paid_until || 0),
2023-05-24 07:13:01 +12:00
}}
/>
</Alert>
)}
{banner === Banner.PRORATION_INFO && (
2023-03-01 08:38:31 +13:00
<Alert severity="info" sx={{ fontSize: "1rem" }}>
2023-01-18 13:40:03 +13:00
<Trans i18nKey="account_upgrade_dialog_proration_info" />
2023-05-24 07:13:01 +12:00
</Alert>
)}
{banner === Banner.RESERVATIONS_WARNING && (
2023-02-22 16:44:30 +13:00
<Alert severity="warning" sx={{ fontSize: "1rem" }}>
2023-05-24 07:13:01 +12:00
<Trans
i18nKey="account_upgrade_dialog_reservations_warning"
count={(account?.reservations.length ?? 0) - (newTier?.limits.reservations ?? 0)}
components={{
2023-03-01 08:38:31 +13:00
Link: <NavLink to={routes.settings} />,
2023-05-24 07:13:01 +12:00
}}
/>
</Alert>
)}
2023-03-01 08:38:31 +13:00
</DialogContent>
2023-05-24 07:13:01 +12:00
<Box
sx={{
2023-03-01 08:38:31 +13:00
display: "flex",
2023-01-18 04:09:37 +13:00
flexDirection: "row",
2023-03-01 08:38:31 +13:00
justifyContent: "space-between",
paddingLeft: "24px",
2023-01-18 04:09:37 +13:00
paddingBottom: "8px",
2023-05-24 07:13:01 +12:00
}}
>
2023-03-01 08:38:31 +13:00
<DialogContentText
component="div"
aria-live="polite"
sx={{
margin: "0px",
paddingTop: "12px",
paddingBottom: "4px",
}}
>
{config.billing_contact.indexOf("@") !== -1 && (
<>
<Trans
i18nKey="account_upgrade_dialog_billing_contact_email"
components={{
Link: <Link href={`mailto:${config.billing_contact}`} />,
}}
/>{" "}
</>
)}
{config.billing_contact.match(`^http?s://`) && (
<>
<Trans
i18nKey="account_upgrade_dialog_billing_contact_website"
components={{
Link: <Link href={config.billing_contact} target="_blank" />,
}}
/>{" "}
</>
)}
{error}
</DialogContentText>
<DialogActions sx={{ paddingRight: 2 }}>
<Button onClick={props.onCancel}>{t("account_upgrade_dialog_button_cancel")}</Button>
<Button onClick={handleSubmit} disabled={!submitAction}>
{submitButtonLabel}
2023-05-24 07:13:01 +12:00
</Button>
2023-03-01 08:38:31 +13:00
</DialogActions>
</Box>
2023-01-10 09:40:46 +13:00
</Dialog>
);
};
2023-01-15 00:43:44 +13:00
const TierCard = (props) => {
2023-01-18 13:40:03 +13:00
const { t } = useTranslation();
const { tier } = props;
2023-02-22 16:44:30 +13:00
let cardStyle;
let labelStyle;
let labelText;
if (props.selected) {
2023-02-22 16:44:30 +13:00
cardStyle = { background: "#eee", border: "3px solid #338574" };
labelStyle = { background: "#338574", color: "white" };
labelText = t("account_upgrade_dialog_tier_selected_label");
} else if (props.current) {
2023-02-22 16:44:30 +13:00
cardStyle = { border: "3px solid #eee" };
labelStyle = { background: "#eee", color: "black" };
labelText = t("account_upgrade_dialog_tier_current_label");
} else {
2023-02-22 16:44:30 +13:00
cardStyle = { border: "3px solid transparent" };
}
let monthlyPrice;
if (!tier.prices) {
monthlyPrice = 0;
2023-01-18 14:21:19 +13:00
} else if (props.interval === SubscriptionInterval.YEAR) {
2023-02-22 16:44:30 +13:00
monthlyPrice = tier.prices.year / 12;
} else if (props.interval === SubscriptionInterval.MONTH) {
monthlyPrice = tier.prices.month;
2023-01-18 14:21:19 +13:00
}
2023-05-24 07:13:01 +12:00
return (
<Box
sx={{
2023-01-18 14:21:19 +13:00
m: "7px",
2023-02-22 16:44:30 +13:00
minWidth: "240px",
2023-01-18 13:40:03 +13:00
flexGrow: 1,
flexShrink: 1,
flexBasis: 0,
2023-02-22 16:44:30 +13:00
borderRadius: "5px",
"&:first-of-type": { ml: 0 },
"&:last-of-type": { mr: 0 },
2023-01-18 04:09:37 +13:00
...cardStyle,
2023-05-24 07:13:01 +12:00
}}
>
2023-01-18 14:21:19 +13:00
<Card sx={{ height: "100%" }}>
<CardActionArea sx={{ height: "100%" }}>
<CardContent onClick={props.onClick} sx={{ height: "100%" }}>
{labelStyle && (
2023-05-24 07:13:01 +12:00
<div
style={{
2023-01-18 14:21:19 +13:00
position: "absolute",
top: "0",
right: "15px",
padding: "2px 10px",
borderRadius: "3px",
...labelStyle,
2023-05-24 07:13:01 +12:00
}}
>
{labelText}
2023-05-24 07:13:01 +12:00
</div>
)}
2023-02-22 16:44:30 +13:00
<Typography variant="subtitle1" component="div">
2023-02-10 09:24:12 +13:00
{tier.name || t("account_basics_tier_free")}
2023-01-18 13:40:03 +13:00
</Typography>
2023-05-24 07:13:01 +12:00
<div>
2023-02-22 16:44:30 +13:00
<Typography component="span" variant="h4" sx={{ fontWeight: 500, marginRight: "3px" }}>
{formatPrice(monthlyPrice)}
2023-01-18 13:40:03 +13:00
</Typography>
2023-02-22 16:44:30 +13:00
{monthlyPrice > 0 && <>/ {t("account_upgrade_dialog_tier_price_per_month")}</>}
2023-05-24 07:13:01 +12:00
</div>
2023-01-18 14:21:19 +13:00
<List dense>
{tier.limits.reservations > 0 && (
2023-02-22 16:44:30 +13:00
<Feature>
{t("account_upgrade_dialog_tier_features_reservations", {
reservations: tier.limits.reservations,
count: tier.limits.reservations,
2023-05-24 07:13:01 +12:00
})}
2023-02-22 16:44:30 +13:00
</Feature>
2023-05-24 07:13:01 +12:00
)}
2023-02-22 16:44:30 +13:00
<Feature>
{t("account_upgrade_dialog_tier_features_messages", {
messages: formatNumber(tier.limits.messages),
count: tier.limits.messages,
2023-05-24 07:13:01 +12:00
})}
2023-02-22 16:44:30 +13:00
</Feature>
<Feature>
{t("account_upgrade_dialog_tier_features_emails", {
emails: formatNumber(tier.limits.emails),
count: tier.limits.emails,
2023-05-24 07:13:01 +12:00
})}
2023-02-22 16:44:30 +13:00
</Feature>
{tier.limits.calls > 0 && (
2023-02-22 16:44:30 +13:00
<Feature>
{t("account_upgrade_dialog_tier_features_calls", {
calls: formatNumber(tier.limits.calls),
count: tier.limits.calls,
2023-05-24 07:13:01 +12:00
})}
2023-02-22 16:44:30 +13:00
</Feature>
2023-05-24 07:13:01 +12:00
)}
2023-02-22 16:44:30 +13:00
<Feature>
{t("account_upgrade_dialog_tier_features_attachment_file_size", {
filesize: formatBytes(tier.limits.attachment_file_size, 0),
})}
</Feature>
{tier.limits.reservations === 0 && <NoFeature>{t("account_upgrade_dialog_tier_features_no_reservations")}</NoFeature>}
{tier.limits.calls === 0 && <NoFeature>{t("account_upgrade_dialog_tier_features_no_calls")}</NoFeature>}
2023-05-24 07:13:01 +12:00
</List>
2023-02-22 16:44:30 +13:00
{tier.prices && props.interval === SubscriptionInterval.MONTH && (
<Typography variant="body2" color="gray">
{t("account_upgrade_dialog_tier_price_billed_monthly", {
price: formatPrice(tier.prices.month * 12),
2023-05-24 07:13:01 +12:00
})}
2023-01-18 13:40:03 +13:00
</Typography>
2023-05-24 07:13:01 +12:00
)}
2023-02-22 16:44:30 +13:00
{tier.prices && props.interval === SubscriptionInterval.YEAR && (
<Typography variant="body2" color="gray">
{t("account_upgrade_dialog_tier_price_billed_yearly", {
price: formatPrice(tier.prices.year),
save: formatPrice(tier.prices.month * 12 - tier.prices.year),
2023-05-24 07:13:01 +12:00
})}
2023-01-18 13:40:03 +13:00
</Typography>
2023-05-24 07:13:01 +12:00
)}
2023-01-18 14:21:19 +13:00
</CardContent>
</CardActionArea>
2023-05-24 07:13:01 +12:00
</Card>
</Box>
2023-01-15 00:43:44 +13:00
);
};
2023-01-10 09:40:46 +13:00
export default UpgradeDialog;