import dayjs from "dayjs"; import Layout from "../components/Layout"; import ky from "ky"; import * as React from "react"; import classnames from "classnames"; import prettyBytes from "pretty-bytes"; import config from "../config"; import useAccountsApi from "../services/useAccountsApi"; import { isFreeTier, isPaidTier } from "../services/tiers"; const ActiveBadge = () => { return ( active ); }; export default function Payments() { const { data: user } = useAccountsApi("user"); const { data: stats } = useAccountsApi("user/stats"); const { data: prices } = useAccountsApi("stripe/prices"); const [plans, setPlans] = React.useState([config.tiers.starter]); const [selectedPlan, setSelectedPlan] = React.useState(null); React.useEffect(() => { if (prices) setPlans((plans) => [...plans, ...prices].sort((a, b) => a.tier - b.tier)); }, [setPlans, prices]); const activePlan = plans.find(({ tier }) => user && user.tier === tier); React.useEffect(() => { setSelectedPlan(activePlan); }, [activePlan, setSelectedPlan]); const handleSubscribe = async () => { try { const price = selectedPlan.stripe; const { sessionId } = await ky.post("/api/stripe/checkout", { json: { price } }).json(); const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY); await stripe.redirectToCheckout({ sessionId }); } catch (error) { console.log(error); // todo: handle error } }; return ( Current plan {activePlan?.name || "—"} Subscription status {isFreeTier(activePlan?.tier) ? "—" : user?.subscriptionStatus} {user?.subscriptionCancelAtPeriodEnd && ( Your plan will be cancelled on {dayjs(user.subscriptionCancelAt).format("D MMM YYYY")}. )} Storage used {prettyBytes(stats?.totalUploadsSize ?? 0)} Plan Pricing plans {plans.map((plan, index) => ( {isFreeTier(activePlan?.tier) && ( setSelectedPlan(plan)} /> )} {plan.name} {activePlan === plan && } {plan.price ? `$${plan.price} / mo` : "no cost"} {plan.description} ))} {user && isPaidTier(user.tier) ? ( Use Stripe Customer Portal to manage your active subscription, payment methods and view your billing history Stripe Customer Portal ) : ( Subscribe )} ); }
{plan.price ? `$${plan.price} / mo` : "no cost"}
{plan.description}