|
| 1 | +import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; |
| 2 | +import { authClient } from "@/lib/auth"; |
| 3 | +import { BACKEND_URL } from "@/lib/const"; |
| 4 | +import { getStripePrices } from "@/lib/stripe"; |
| 5 | +import { cn } from "@/lib/utils"; |
| 6 | +import { Loader2 } from "lucide-react"; |
| 7 | +import { useState } from "react"; |
| 8 | +import { toast } from "sonner"; |
| 9 | +import { Badge } from "../../ui/badge"; |
| 10 | + |
| 11 | +interface PlanDialogProps { |
| 12 | + open: boolean; |
| 13 | + onOpenChange: (open: boolean) => void; |
| 14 | + currentPlanName?: string; |
| 15 | +} |
| 16 | + |
| 17 | +const EVENT_TIERS = [ |
| 18 | + { events: 100_000, label: "100k" }, |
| 19 | + { events: 250_000, label: "250k" }, |
| 20 | + { events: 500_000, label: "500k" }, |
| 21 | + { events: 1_000_000, label: "1M" }, |
| 22 | + { events: 2_000_000, label: "2M" }, |
| 23 | + { events: 5_000_000, label: "5M" }, |
| 24 | + { events: 10_000_000, label: "10M" }, |
| 25 | +]; |
| 26 | + |
| 27 | +export function PlanDialog({ open, onOpenChange, currentPlanName }: PlanDialogProps) { |
| 28 | + const [isAnnual, setIsAnnual] = useState(false); |
| 29 | + const [loadingPriceId, setLoadingPriceId] = useState<string | null>(null); |
| 30 | + const stripePrices = getStripePrices(); |
| 31 | + const { data: activeOrg } = authClient.useActiveOrganization(); |
| 32 | + |
| 33 | + const handleCheckout = async (priceId: string, planName: string) => { |
| 34 | + if (!activeOrg) { |
| 35 | + toast.error("Please select an organization"); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + setLoadingPriceId(priceId); |
| 40 | + try { |
| 41 | + const baseUrl = window.location.origin; |
| 42 | + const successUrl = `${baseUrl}/settings/organization/subscription?session_id={CHECKOUT_SESSION_ID}`; |
| 43 | + const cancelUrl = `${baseUrl}/settings/organization/subscription`; |
| 44 | + |
| 45 | + const response = await fetch(`${BACKEND_URL}/stripe/create-checkout-session`, { |
| 46 | + method: "POST", |
| 47 | + headers: { |
| 48 | + "Content-Type": "application/json", |
| 49 | + }, |
| 50 | + credentials: "include", |
| 51 | + body: JSON.stringify({ |
| 52 | + priceId, |
| 53 | + successUrl, |
| 54 | + cancelUrl, |
| 55 | + organizationId: activeOrg.id, |
| 56 | + }), |
| 57 | + }); |
| 58 | + |
| 59 | + const data = await response.json(); |
| 60 | + |
| 61 | + if (!response.ok) { |
| 62 | + throw new Error(data.error || "Failed to create checkout session."); |
| 63 | + } |
| 64 | + |
| 65 | + if (data.checkoutUrl) { |
| 66 | + window.location.href = data.checkoutUrl; |
| 67 | + } else { |
| 68 | + throw new Error("Checkout URL not received."); |
| 69 | + } |
| 70 | + } catch (error: any) { |
| 71 | + toast.error(`Checkout failed: ${error.message}`); |
| 72 | + setLoadingPriceId(null); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + const getPriceForTier = (events: number, planType: "standard" | "pro") => { |
| 77 | + const suffix = isAnnual ? "-annual" : ""; |
| 78 | + const planName = `${planType}${events >= 1_000_000 ? events / 1_000_000 + "m" : events / 1_000 + "k"}${suffix}`; |
| 79 | + const plan = stripePrices.find(p => p.name === planName); |
| 80 | + return plan; |
| 81 | + }; |
| 82 | + |
| 83 | + const isCurrentPlan = (planName: string) => { |
| 84 | + return currentPlanName === planName; |
| 85 | + }; |
| 86 | + |
| 87 | + return ( |
| 88 | + <Dialog open={open} onOpenChange={onOpenChange}> |
| 89 | + <DialogContent className="max-w-3xl max-h-[90vh] overflow-y-auto"> |
| 90 | + <DialogHeader> |
| 91 | + <DialogTitle>Choose Your Plan</DialogTitle> |
| 92 | + </DialogHeader> |
| 93 | + |
| 94 | + {/* Billing toggle */} |
| 95 | + <div className="flex justify-center mb-0"> |
| 96 | + <div className="flex gap-3 text-sm"> |
| 97 | + <button |
| 98 | + onClick={() => setIsAnnual(false)} |
| 99 | + className={cn( |
| 100 | + "px-4 py-2 rounded-full transition-colors cursor-pointer", |
| 101 | + !isAnnual ? "bg-emerald-500/20 text-emerald-400 font-medium" : "text-neutral-400 hover:text-neutral-200" |
| 102 | + )} |
| 103 | + > |
| 104 | + Monthly |
| 105 | + </button> |
| 106 | + <button |
| 107 | + onClick={() => setIsAnnual(true)} |
| 108 | + className={cn( |
| 109 | + "px-4 py-2 rounded-full transition-colors cursor-pointer", |
| 110 | + isAnnual ? "bg-emerald-500/20 text-emerald-400 font-medium" : "text-neutral-400 hover:text-neutral-200" |
| 111 | + )} |
| 112 | + > |
| 113 | + Annual |
| 114 | + <span className="ml-1 text-xs text-emerald-500">-17%</span> |
| 115 | + </button> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + |
| 119 | + {/* Plan columns */} |
| 120 | + <div className="grid grid-cols-2 gap-6"> |
| 121 | + {[ |
| 122 | + { type: "standard" as const, title: "Standard", subtitle: "Core analytics features" }, |
| 123 | + { type: "pro" as const, title: "Pro", subtitle: "Advanced features + session replays" }, |
| 124 | + ].map(({ type, title, subtitle }) => ( |
| 125 | + <div key={type} className="space-y-3"> |
| 126 | + <div className="text-center mb-4"> |
| 127 | + <h3 className="text-xl font-bold">{title}</h3> |
| 128 | + <p className="text-sm text-neutral-400">{subtitle}</p> |
| 129 | + </div> |
| 130 | + <div className="space-y-2"> |
| 131 | + {EVENT_TIERS.map(tier => { |
| 132 | + const plan = getPriceForTier(tier.events, type); |
| 133 | + if (!plan) return null; |
| 134 | + const isCurrent = isCurrentPlan(plan.name); |
| 135 | + const isLoading = loadingPriceId === plan.priceId; |
| 136 | + |
| 137 | + return ( |
| 138 | + <div |
| 139 | + key={plan.name} |
| 140 | + className={cn( |
| 141 | + "flex flex-col gap-2 justify-between p-3 rounded-lg border cursor-pointer", |
| 142 | + isCurrent |
| 143 | + ? "bg-emerald-500/10 border-emerald-500" |
| 144 | + : "bg-neutral-800/20 border-neutral-700/50 hover:bg-neutral-800/30", |
| 145 | + isLoading && "opacity-50" |
| 146 | + )} |
| 147 | + onClick={() => handleCheckout(plan.priceId, plan.name)} |
| 148 | + > |
| 149 | + <div className="flex items-center justify-between w-full"> |
| 150 | + <Badge variant="success">{type === "pro" ? "Pro" : "Standard"}</Badge> |
| 151 | + <div className="text-xs text-neutral-400"> |
| 152 | + <span className="text-neutral-200 font-semibold text-base"> |
| 153 | + ${isAnnual ? Math.round(plan.price / 12) : plan.price} |
| 154 | + </span>{" "} |
| 155 | + / month |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + <div className="text-neutral-100 font-medium flex items-center gap-2"> |
| 159 | + {tier.label} events <span className="text-neutral-400 text-xs font-normal">/ month</span> |
| 160 | + {isLoading && <Loader2 className="w-4 h-4 animate-spin" />} |
| 161 | + </div> |
| 162 | + </div> |
| 163 | + ); |
| 164 | + })} |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + ))} |
| 168 | + </div> |
| 169 | + |
| 170 | + {/* Features comparison link */} |
| 171 | + <div className="mt-3 text-center"> |
| 172 | + <a |
| 173 | + href="https://www.rybbit.io/pricing" |
| 174 | + target="_blank" |
| 175 | + rel="noopener noreferrer" |
| 176 | + className="text-neutral-200 hover:text-neutral-100 text-sm underline" |
| 177 | + > |
| 178 | + View detailed feature comparison → |
| 179 | + </a> |
| 180 | + </div> |
| 181 | + </DialogContent> |
| 182 | + </Dialog> |
| 183 | + ); |
| 184 | +} |
0 commit comments