|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { allPlans } from "@openstatus/db/src/schema/plan/config"; |
| 4 | +import type { BillingInterval } from "@openstatus/db/src/schema/plan/schema"; |
| 5 | +import { getPriceConfig } from "@openstatus/db/src/schema/plan/utils"; |
| 6 | +import { Badge } from "@openstatus/ui/components/ui/badge"; |
| 7 | +import { Tabs, TabsList, TabsTrigger } from "@openstatus/ui/components/ui/tabs"; |
| 8 | +import { usePathname, useRouter, useSearchParams } from "next/navigation"; |
| 9 | +import { useCallback } from "react"; |
| 10 | + |
| 11 | +const planColumns = [ |
| 12 | + { plan: "free" as const, header: "Hobby" }, |
| 13 | + { plan: "starter" as const, header: "Starter" }, |
| 14 | + { plan: "team" as const, header: "Pro" }, |
| 15 | +] as const; |
| 16 | + |
| 17 | +export function PricingTabs() { |
| 18 | + const searchParams = useSearchParams(); |
| 19 | + const router = useRouter(); |
| 20 | + const pathname = usePathname(); |
| 21 | + |
| 22 | + const interval = ( |
| 23 | + searchParams.get("interval") === "yearly" ? "yearly" : "monthly" |
| 24 | + ) as BillingInterval; |
| 25 | + |
| 26 | + const setInterval = useCallback( |
| 27 | + (value: string) => { |
| 28 | + const params = new URLSearchParams(searchParams.toString()); |
| 29 | + if (value === "monthly") { |
| 30 | + params.delete("interval"); |
| 31 | + } else { |
| 32 | + params.set("interval", value); |
| 33 | + } |
| 34 | + const qs = params.toString(); |
| 35 | + router.replace(`${pathname}${qs ? `?${qs}` : ""}`, { scroll: false }); |
| 36 | + }, |
| 37 | + [searchParams, router, pathname], |
| 38 | + ); |
| 39 | + |
| 40 | + return ( |
| 41 | + <div className="not-prose mb-4"> |
| 42 | + <Tabs value={interval} onValueChange={setInterval}> |
| 43 | + <TabsList className="h-auto w-full rounded-none"> |
| 44 | + <TabsTrigger |
| 45 | + value="monthly" |
| 46 | + className="h-auto w-full truncate rounded-none p-3.5" |
| 47 | + > |
| 48 | + Monthly |
| 49 | + </TabsTrigger> |
| 50 | + <TabsTrigger |
| 51 | + value="yearly" |
| 52 | + className="h-auto w-full truncate rounded-none p-3.5" |
| 53 | + > |
| 54 | + Yearly{" "} |
| 55 | + <Badge variant="secondary" className="ml-1.5 h-auto rounded-none"> |
| 56 | + 2 months free |
| 57 | + </Badge> |
| 58 | + </TabsTrigger> |
| 59 | + </TabsList> |
| 60 | + </Tabs> |
| 61 | + <PricingUpdater interval={interval} /> |
| 62 | + </div> |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +function PricingUpdater({ interval }: { interval: BillingInterval }) { |
| 67 | + const formatPrice = useCallback( |
| 68 | + (plan: (typeof planColumns)[number]["plan"]) => { |
| 69 | + const config = allPlans[plan]; |
| 70 | + if (plan === "free") return "Free - Hobby"; |
| 71 | + const price = getPriceConfig(plan, "USD", interval); |
| 72 | + const formatted = new Intl.NumberFormat(price.locale, { |
| 73 | + style: "currency", |
| 74 | + currency: price.currency, |
| 75 | + maximumFractionDigits: 0, |
| 76 | + }).format(price.value); |
| 77 | + const suffix = interval === "yearly" ? "/year" : "/month"; |
| 78 | + return `${formatted}${suffix} - ${config.title}`; |
| 79 | + }, |
| 80 | + [interval], |
| 81 | + ); |
| 82 | + |
| 83 | + // Walk the DOM to update prices in the GFM table headers |
| 84 | + if (typeof window !== "undefined") { |
| 85 | + // Use requestAnimationFrame to run after render |
| 86 | + requestAnimationFrame(() => { |
| 87 | + const tables = document.querySelectorAll(".table-wrapper table"); |
| 88 | + for (const table of tables) { |
| 89 | + const headers = table.querySelectorAll("thead th"); |
| 90 | + // The pricing table has 4 columns: Features | Hobby | Starter | Pro |
| 91 | + if (headers.length !== 4) continue; |
| 92 | + |
| 93 | + for (let i = 0; i < planColumns.length; i++) { |
| 94 | + const th = headers[i + 1]; // skip first "Features comparison" column |
| 95 | + if (!th) continue; |
| 96 | + const { plan } = planColumns[i]; |
| 97 | + th.textContent = formatPrice(plan); |
| 98 | + } |
| 99 | + } |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + return null; |
| 104 | +} |
0 commit comments