AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.tsx 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { Form, Head } from '@inertiajs/react';
  2. import { Check } from 'lucide-react';
  3. import BillingController from '@/actions/App/Http/Controllers/BillingController';
  4. import Heading from '@/components/heading';
  5. import { daysLeft } from '@/components/plan-notice';
  6. import { Badge } from '@/components/ui/badge';
  7. import { Button } from '@/components/ui/button';
  8. import {
  9. Card,
  10. CardContent,
  11. CardDescription,
  12. CardHeader,
  13. CardTitle,
  14. } from '@/components/ui/card';
  15. import {
  16. Dialog,
  17. DialogClose,
  18. DialogContent,
  19. DialogDescription,
  20. DialogFooter,
  21. DialogHeader,
  22. DialogTitle,
  23. DialogTrigger,
  24. } from '@/components/ui/dialog';
  25. import { formatRinggit } from '@/lib/money';
  26. import { cn } from '@/lib/utils';
  27. import { dashboard } from '@/routes';
  28. import type { Plan, SubscriptionSummary, Usage, UsageCount } from '@/types';
  29. const dateFormat = new Intl.DateTimeFormat('en-MY', { dateStyle: 'long' });
  30. function formatDate(iso: string | null): string {
  31. return iso ? dateFormat.format(new Date(iso)) : '';
  32. }
  33. function Meter({
  34. label,
  35. usage,
  36. note,
  37. }: {
  38. label: string;
  39. usage: UsageCount;
  40. note: string;
  41. }) {
  42. const percent =
  43. usage.limit > 0
  44. ? Math.min(100, Math.round((usage.used / usage.limit) * 100))
  45. : 100;
  46. const full = usage.used >= usage.limit;
  47. return (
  48. <div>
  49. <div className="flex items-baseline justify-between gap-3 text-sm">
  50. <span className="font-medium">{label}</span>
  51. <span className="whitespace-nowrap text-muted-foreground tabular-nums">
  52. {usage.used} of {usage.limit}
  53. </span>
  54. </div>
  55. <div
  56. className="mt-2 h-2 overflow-hidden rounded-full bg-muted"
  57. role="progressbar"
  58. aria-label={label}
  59. aria-valuemin={0}
  60. aria-valuemax={usage.limit}
  61. aria-valuenow={Math.min(usage.used, usage.limit)}
  62. >
  63. <div
  64. className={cn(
  65. 'h-full rounded-full',
  66. full ? 'bg-amber-500' : 'bg-foreground',
  67. )}
  68. style={{ width: `${percent}%` }}
  69. />
  70. </div>
  71. <p className="mt-1.5 text-xs text-muted-foreground">{note}</p>
  72. </div>
  73. );
  74. }
  75. export default function BillingIndex({
  76. usage,
  77. plans,
  78. subscription,
  79. }: {
  80. usage: Usage;
  81. plans: Plan[];
  82. subscription: SubscriptionSummary | null;
  83. }) {
  84. const paying = subscription?.active ?? false;
  85. const ending = paying && subscription?.cancel_at_period_end;
  86. const pastDue = subscription?.status === 'past_due';
  87. const counted = usage.period === 'month' ? 'this month' : 'in total';
  88. const trialDays = daysLeft(usage.trial_ends_at);
  89. return (
  90. <>
  91. <Head title="Billing" />
  92. <div className="mx-auto w-full max-w-5xl space-y-8 p-4">
  93. <Heading
  94. title="Billing"
  95. description="Your plan, what it includes, and how much you have used."
  96. />
  97. <Card>
  98. <CardHeader>
  99. <div className="flex flex-wrap items-center gap-3">
  100. <CardTitle>{usage.plan_name}</CardTitle>
  101. {usage.on_trial && (
  102. <Badge variant="secondary">Free trial</Badge>
  103. )}
  104. {paying && !ending && !pastDue && (
  105. <Badge>Active</Badge>
  106. )}
  107. {ending && (
  108. <Badge variant="secondary">Ending</Badge>
  109. )}
  110. {pastDue && (
  111. <Badge variant="destructive">
  112. Payment failed
  113. </Badge>
  114. )}
  115. </div>
  116. <CardDescription data-test="plan-status">
  117. {!usage.active &&
  118. 'Your free trial has ended. Your stores are offline and AI is switched off until you choose a plan below.'}
  119. {usage.on_trial &&
  120. `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.`}
  121. {paying &&
  122. !ending &&
  123. !pastDue &&
  124. `Renews on ${formatDate(subscription?.current_period_end ?? null)}.`}
  125. {ending &&
  126. `Your plan ends on ${formatDate(subscription?.current_period_end ?? null)}. After that your stores go offline.`}
  127. {pastDue &&
  128. 'Your last payment did not go through. We will try again; please check your card.'}
  129. </CardDescription>
  130. </CardHeader>
  131. <CardContent className="space-y-6">
  132. <div className="grid gap-6 md:grid-cols-3">
  133. <Meter
  134. label="Stores"
  135. usage={usage.stores}
  136. note="Stores you have created"
  137. />
  138. <Meter
  139. label="Regenerations"
  140. usage={usage.regenerations}
  141. note={`Full redesigns ${counted}`}
  142. />
  143. <Meter
  144. label="AI edits"
  145. usage={usage.edits}
  146. note={`Changes by chat ${counted}`}
  147. />
  148. </div>
  149. {paying && (
  150. <div className="border-t pt-4">
  151. {ending ? (
  152. <Form {...BillingController.resume.form()}>
  153. {({ processing }) => (
  154. <Button
  155. variant="outline"
  156. size="sm"
  157. disabled={processing}
  158. data-test="resume-plan-button"
  159. >
  160. Keep my plan
  161. </Button>
  162. )}
  163. </Form>
  164. ) : (
  165. <Dialog>
  166. <DialogTrigger asChild>
  167. <Button
  168. variant="ghost"
  169. size="sm"
  170. className="text-muted-foreground"
  171. data-test="cancel-plan-button"
  172. >
  173. Cancel plan
  174. </Button>
  175. </DialogTrigger>
  176. <DialogContent>
  177. <DialogHeader>
  178. <DialogTitle>
  179. Cancel your plan?
  180. </DialogTitle>
  181. <DialogDescription>
  182. You keep your plan until{' '}
  183. {formatDate(
  184. subscription?.current_period_end ??
  185. null,
  186. )}
  187. , and you will not be
  188. charged again. After that
  189. your stores go offline.
  190. Nothing is deleted, and you
  191. can come back any time.
  192. </DialogDescription>
  193. </DialogHeader>
  194. <DialogFooter>
  195. <DialogClose asChild>
  196. <Button variant="outline">
  197. Keep my plan
  198. </Button>
  199. </DialogClose>
  200. <Form
  201. {...BillingController.cancel.form()}
  202. >
  203. {({ processing }) => (
  204. <Button
  205. variant="destructive"
  206. disabled={
  207. processing
  208. }
  209. data-test="confirm-cancel-plan-button"
  210. >
  211. Cancel plan
  212. </Button>
  213. )}
  214. </Form>
  215. </DialogFooter>
  216. </DialogContent>
  217. </Dialog>
  218. )}
  219. </div>
  220. )}
  221. </CardContent>
  222. </Card>
  223. <section className="space-y-4">
  224. <h2 className="font-medium">Plans</h2>
  225. <div className="grid gap-4 lg:grid-cols-3">
  226. {plans.map((plan) => {
  227. const current = paying && usage.plan === plan.key;
  228. return (
  229. <Card
  230. key={plan.key}
  231. className={cn(
  232. 'flex flex-col',
  233. current && 'border-foreground',
  234. )}
  235. >
  236. <CardHeader>
  237. <div className="flex items-center justify-between gap-3">
  238. <CardTitle>{plan.name}</CardTitle>
  239. {current && (
  240. <Badge>Your plan</Badge>
  241. )}
  242. </div>
  243. <p className="text-2xl font-semibold tabular-nums">
  244. {formatRinggit(plan.price)}
  245. <span className="text-sm font-normal text-muted-foreground">
  246. {' '}
  247. / month
  248. </span>
  249. </p>
  250. </CardHeader>
  251. <CardContent className="flex flex-1 flex-col gap-5">
  252. <ul className="space-y-2 text-sm">
  253. {[
  254. plan.stores === 1
  255. ? '1 store'
  256. : `${plan.stores} stores`,
  257. `${plan.regenerations} full redesigns a month`,
  258. `${plan.edits.toLocaleString('en-MY')} AI edits a month`,
  259. 'Your own store address',
  260. ].map((feature) => (
  261. <li
  262. key={feature}
  263. className="flex gap-2"
  264. >
  265. <Check className="mt-0.5 size-4 shrink-0 text-green-600 dark:text-green-400" />
  266. {feature}
  267. </li>
  268. ))}
  269. </ul>
  270. <Form
  271. {...BillingController.subscribe.form()}
  272. className="mt-auto"
  273. >
  274. {({ processing }) => (
  275. <>
  276. <input
  277. type="hidden"
  278. name="plan"
  279. value={plan.key}
  280. />
  281. <Button
  282. className="w-full"
  283. variant={
  284. current
  285. ? 'outline'
  286. : 'default'
  287. }
  288. disabled={
  289. processing ||
  290. current
  291. }
  292. data-test={`choose-plan-${plan.key}`}
  293. >
  294. {current
  295. ? 'Current plan'
  296. : paying
  297. ? `Switch to ${plan.name}`
  298. : `Choose ${plan.name}`}
  299. </Button>
  300. </>
  301. )}
  302. </Form>
  303. </CardContent>
  304. </Card>
  305. );
  306. })}
  307. </div>
  308. <p className="text-xs text-muted-foreground">
  309. Prices are in Malaysian Ringgit. You pay on a secure
  310. Stripe page. When you switch plans, the difference for
  311. the rest of the month is charged or credited on your
  312. next bill.
  313. </p>
  314. </section>
  315. </div>
  316. </>
  317. );
  318. }
  319. BillingIndex.layout = {
  320. breadcrumbs: [
  321. { title: 'Dashboard', href: dashboard() },
  322. { title: 'Billing', href: '#' },
  323. ],
  324. };