| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- import { Form, Head } from '@inertiajs/react';
- import { Check } from 'lucide-react';
- import BillingController from '@/actions/App/Http/Controllers/BillingController';
- import Heading from '@/components/heading';
- import { daysLeft } from '@/components/plan-notice';
- import { Badge } from '@/components/ui/badge';
- import { Button } from '@/components/ui/button';
- import {
- Card,
- CardContent,
- CardDescription,
- CardHeader,
- CardTitle,
- } from '@/components/ui/card';
- import {
- Dialog,
- DialogClose,
- DialogContent,
- DialogDescription,
- DialogFooter,
- DialogHeader,
- DialogTitle,
- DialogTrigger,
- } from '@/components/ui/dialog';
- import { formatRinggit } from '@/lib/money';
- import { cn } from '@/lib/utils';
- import { dashboard } from '@/routes';
- import type { Plan, SubscriptionSummary, Usage, UsageCount } from '@/types';
-
- const dateFormat = new Intl.DateTimeFormat('en-MY', { dateStyle: 'long' });
-
- function formatDate(iso: string | null): string {
- return iso ? dateFormat.format(new Date(iso)) : '';
- }
-
- function Meter({
- label,
- usage,
- note,
- }: {
- label: string;
- usage: UsageCount;
- note: string;
- }) {
- const percent =
- usage.limit > 0
- ? Math.min(100, Math.round((usage.used / usage.limit) * 100))
- : 100;
- const full = usage.used >= usage.limit;
-
- return (
- <div>
- <div className="flex items-baseline justify-between gap-3 text-sm">
- <span className="font-medium">{label}</span>
- <span className="whitespace-nowrap text-muted-foreground tabular-nums">
- {usage.used} of {usage.limit}
- </span>
- </div>
- <div
- className="mt-2 h-2 overflow-hidden rounded-full bg-muted"
- role="progressbar"
- aria-label={label}
- aria-valuemin={0}
- aria-valuemax={usage.limit}
- aria-valuenow={Math.min(usage.used, usage.limit)}
- >
- <div
- className={cn(
- 'h-full rounded-full',
- full ? 'bg-amber-500' : 'bg-foreground',
- )}
- style={{ width: `${percent}%` }}
- />
- </div>
- <p className="mt-1.5 text-xs text-muted-foreground">{note}</p>
- </div>
- );
- }
-
- export default function BillingIndex({
- usage,
- plans,
- subscription,
- }: {
- usage: Usage;
- plans: Plan[];
- subscription: SubscriptionSummary | null;
- }) {
- const paying = subscription?.active ?? false;
- const ending = paying && subscription?.cancel_at_period_end;
- const pastDue = subscription?.status === 'past_due';
- const counted = usage.period === 'month' ? 'this month' : 'in total';
- const trialDays = daysLeft(usage.trial_ends_at);
-
- return (
- <>
- <Head title="Billing" />
-
- <div className="mx-auto w-full max-w-5xl space-y-8 p-4">
- <Heading
- title="Billing"
- description="Your plan, what it includes, and how much you have used."
- />
-
- <Card>
- <CardHeader>
- <div className="flex flex-wrap items-center gap-3">
- <CardTitle>{usage.plan_name}</CardTitle>
- {usage.on_trial && (
- <Badge variant="secondary">Free trial</Badge>
- )}
- {paying && !ending && !pastDue && (
- <Badge>Active</Badge>
- )}
- {ending && (
- <Badge variant="secondary">Ending</Badge>
- )}
- {pastDue && (
- <Badge variant="destructive">
- Payment failed
- </Badge>
- )}
- </div>
- <CardDescription data-test="plan-status">
- {!usage.active &&
- 'Your free trial has ended. Your stores are offline and AI is switched off until you choose a plan below.'}
- {usage.on_trial &&
- `Your free trial ends on ${formatDate(usage.trial_ends_at)} (${trialDays === 1 ? '1 day' : `${trialDays} days`} left). Choose a plan below to keep your stores online after that.`}
- {paying &&
- !ending &&
- !pastDue &&
- `Renews on ${formatDate(subscription?.current_period_end ?? null)}.`}
- {ending &&
- `Your plan ends on ${formatDate(subscription?.current_period_end ?? null)}. After that your stores go offline.`}
- {pastDue &&
- 'Your last payment did not go through. We will try again; please check your card.'}
- </CardDescription>
- </CardHeader>
- <CardContent className="space-y-6">
- <div className="grid gap-6 md:grid-cols-3">
- <Meter
- label="Stores"
- usage={usage.stores}
- note="Stores you have created"
- />
- <Meter
- label="Regenerations"
- usage={usage.regenerations}
- note={`Full redesigns ${counted}`}
- />
- <Meter
- label="AI edits"
- usage={usage.edits}
- note={`Changes by chat ${counted}`}
- />
- </div>
-
- {paying && (
- <div className="border-t pt-4">
- {ending ? (
- <Form {...BillingController.resume.form()}>
- {({ processing }) => (
- <Button
- variant="outline"
- size="sm"
- disabled={processing}
- data-test="resume-plan-button"
- >
- Keep my plan
- </Button>
- )}
- </Form>
- ) : (
- <Dialog>
- <DialogTrigger asChild>
- <Button
- variant="ghost"
- size="sm"
- className="text-muted-foreground"
- data-test="cancel-plan-button"
- >
- Cancel plan
- </Button>
- </DialogTrigger>
- <DialogContent>
- <DialogHeader>
- <DialogTitle>
- Cancel your plan?
- </DialogTitle>
- <DialogDescription>
- You keep your plan until{' '}
- {formatDate(
- subscription?.current_period_end ??
- null,
- )}
- , and you will not be
- charged again. After that
- your stores go offline.
- Nothing is deleted, and you
- can come back any time.
- </DialogDescription>
- </DialogHeader>
- <DialogFooter>
- <DialogClose asChild>
- <Button variant="outline">
- Keep my plan
- </Button>
- </DialogClose>
- <Form
- {...BillingController.cancel.form()}
- >
- {({ processing }) => (
- <Button
- variant="destructive"
- disabled={
- processing
- }
- data-test="confirm-cancel-plan-button"
- >
- Cancel plan
- </Button>
- )}
- </Form>
- </DialogFooter>
- </DialogContent>
- </Dialog>
- )}
- </div>
- )}
- </CardContent>
- </Card>
-
- <section className="space-y-4">
- <h2 className="font-medium">Plans</h2>
-
- <div className="grid gap-4 lg:grid-cols-3">
- {plans.map((plan) => {
- const current = paying && usage.plan === plan.key;
-
- return (
- <Card
- key={plan.key}
- className={cn(
- 'flex flex-col',
- current && 'border-foreground',
- )}
- >
- <CardHeader>
- <div className="flex items-center justify-between gap-3">
- <CardTitle>{plan.name}</CardTitle>
- {current && (
- <Badge>Your plan</Badge>
- )}
- </div>
- <p className="text-2xl font-semibold tabular-nums">
- {formatRinggit(plan.price)}
- <span className="text-sm font-normal text-muted-foreground">
- {' '}
- / month
- </span>
- </p>
- </CardHeader>
- <CardContent className="flex flex-1 flex-col gap-5">
- <ul className="space-y-2 text-sm">
- {[
- plan.stores === 1
- ? '1 store'
- : `${plan.stores} stores`,
- `${plan.regenerations} full redesigns a month`,
- `${plan.edits.toLocaleString('en-MY')} AI edits a month`,
- 'Your own store address',
- ].map((feature) => (
- <li
- key={feature}
- className="flex gap-2"
- >
- <Check className="mt-0.5 size-4 shrink-0 text-green-600 dark:text-green-400" />
- {feature}
- </li>
- ))}
- </ul>
-
- <Form
- {...BillingController.subscribe.form()}
- className="mt-auto"
- >
- {({ processing }) => (
- <>
- <input
- type="hidden"
- name="plan"
- value={plan.key}
- />
- <Button
- className="w-full"
- variant={
- current
- ? 'outline'
- : 'default'
- }
- disabled={
- processing ||
- current
- }
- data-test={`choose-plan-${plan.key}`}
- >
- {current
- ? 'Current plan'
- : paying
- ? `Switch to ${plan.name}`
- : `Choose ${plan.name}`}
- </Button>
- </>
- )}
- </Form>
- </CardContent>
- </Card>
- );
- })}
- </div>
-
- <p className="text-xs text-muted-foreground">
- Prices are in Malaysian Ringgit. You pay on a secure
- Stripe page. When you switch plans, the difference for
- the rest of the month is charged or credited on your
- next bill.
- </p>
- </section>
- </div>
- </>
- );
- }
-
- BillingIndex.layout = {
- breadcrumbs: [
- { title: 'Dashboard', href: dashboard() },
- { title: 'Billing', href: '#' },
- ],
- };
|