Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor PlanStep and PlanDialog components for improved subscription management

Bill Yang0d6b841ff4f9
diff --git a/client/src/app/signup/components/PlanStep.tsx b/client/src/app/signup/components/PlanStep.tsx
index fb0d6121c..db8d8291d 100644
--- a/client/src/app/signup/components/PlanStep.tsx
+++ b/client/src/app/signup/components/PlanStep.tsx
@@ -1,10 +1,11 @@
+import { PlanRow } from "@/components/subscription/components/PlanRow";
import { Button } from "@/components/ui/button";
import { Slider } from "@/components/ui/slider";
import { ArrowRight } from "lucide-react";
import { useExtracted } from "next-intl";
import { cn } from "../../../lib/utils";
-import { EVENT_TIERS, findPriceForTier, formatEventTier } from "../../subscribe/components/utils";
+import { EVENT_TIERS, formatEventTier } from "../../subscribe/components/utils";
interface PlanStepProps {
eventLimitIndex: number;
@@ -17,97 +18,6 @@ interface PlanStepProps {
isLoading: boolean;
}
-function PlanRow({
- plan,
- label,
- description,
- eventLimit,
- isAnnual,
- selectedPlan,
- onSelect,
- disabled,
- disabledReason,
-}: {
- plan: "standard" | "pro";
- label: string;
- description: string;
- eventLimit: number | string;
- isAnnual: boolean;
- selectedPlan: string;
- onSelect: () => void;
- disabled?: boolean;
- disabledReason?: string;
-}) {
- const price =
- eventLimit !== "Custom"
- ? findPriceForTier(eventLimit, isAnnual ? "year" : "month", plan)
- : null;
- const displayPrice = price
- ? isAnnual
- ? Math.round(price.price / 12)
- : price.price
- : 0;
-
- return (
- <button
- onClick={disabled ? undefined : onSelect}
- className={cn(
- "w-full rounded-2xl p-1.5 transition-all text-left",
- disabled
- ? "opacity-50 cursor-not-allowed"
- : "cursor-pointer",
- !disabled && selectedPlan === plan
- ? "bg-emerald-500/10 border border-emerald-500/30"
- : "bg-neutral-200/20 dark:bg-neutral-900/40 border border-neutral-200 dark:border-neutral-800"
- )}
- >
- <div
- className={cn(
- "flex items-center justify-between p-3 rounded-xl border",
- !disabled && selectedPlan === plan
- ? "border-emerald-500 bg-neutral-50 dark:bg-neutral-800/60"
- : "border-neutral-200 dark:border-neutral-800 bg-neutral-50/50 dark:bg-neutral-900",
- !disabled && selectedPlan !== plan && "hover:border-neutral-300 dark:hover:border-neutral-700"
- )}
- >
- <div className="flex items-center gap-3">
- <div
- className={cn(
- "w-4 h-4 rounded-full border-2 flex items-center justify-center",
- !disabled && selectedPlan === plan
- ? "border-emerald-500"
- : "border-neutral-300 dark:border-neutral-600"
- )}
- >
- {!disabled && selectedPlan === plan && (
- <div className="w-2 h-2 rounded-full bg-emerald-500" />
- )}
- </div>
- <div>
- <div className="flex items-center gap-2">
- <span className="font-medium">{label}</span>
- {disabled && disabledReason && (
- <span className="text-[10px] text-neutral-400 dark:text-neutral-500 bg-neutral-100 dark:bg-neutral-800 px-1.5 py-0.5 rounded">
- {disabledReason}
- </span>
- )}
- </div>
- <p className="text-xs text-neutral-500 dark:text-neutral-400">
- {description}
- </p>
- </div>
- </div>
- <div className="text-right">
- <span className="font-semibold">${displayPrice}</span>
- <span className="text-sm text-neutral-500 dark:text-neutral-400">
- /mo
- </span>
- </div>
- </div>
- </button>
- );
-}
-
export function PlanStep({
eventLimitIndex,
setEventLimitIndex,
diff --git a/client/src/app/subscribe/components/utils.ts b/client/src/app/subscribe/components/utils.ts
index ec0566390..2a382c73a 100644
--- a/client/src/app/subscribe/components/utils.ts
+++ b/client/src/app/subscribe/components/utils.ts
@@ -1,11 +1,11 @@
// Common utility functions and constants for subscription components
import { BASIC_SITE_LIMIT, BASIC_TEAM_LIMIT, FREE_SITE_LIMIT, STANDARD_SITE_LIMIT, STANDARD_TEAM_LIMIT } from "../../../lib/const";
-import { getStripePrices, STRIPE_TIERS } from "../../../lib/stripe";
import { FeatureItem } from "@/components/pricing/PricingCard";
-export const EVENT_TIERS = [...STRIPE_TIERS.map(tier => tier.events), "Custom"];
+// Re-export shared utils from canonical location
+export { EVENT_TIERS, findPriceForTier, formatEventTier } from "../../../lib/subscription/planUtils";
export const BASIC_FEATURES = [
`${BASIC_SITE_LIMIT} website`,
@@ -64,47 +64,3 @@ export const FREE_FEATURES: FeatureItem[] = [
{ feature: "Advanced features", included: false },
{ feature: "Email support", included: false },
];
-
-const stripePrices = getStripePrices();
-
-// Find the appropriate price for a tier at current event limit
-export function findPriceForTier(
- eventLimit: number | string,
- interval: "month" | "year",
- planType: "basic" | "standard" | "pro" = "standard"
-) {
- // Check if we have a custom tier
- if (eventLimit === "Custom") {
- return null;
- }
-
- // Convert eventLimit to number to ensure type safety
- const eventLimitValue = Number(eventLimit);
-
- // Determine if we need to look for annual plans
- const isAnnual = interval === "year";
-
- // Filter plans by name pattern (with or without -annual suffix) and interval
- const plans = stripePrices.filter(
- plan =>
- (isAnnual
- ? plan.name.startsWith(planType) && plan.name.includes("-annual")
- : plan.name.startsWith(planType) && !plan.name.includes("-annual")) && plan.interval === interval
- );
-
- // Find a plan that matches or exceeds the event limit
- const matchingPlan = plans.find(plan => plan.events >= eventLimitValue);
- const selectedPlan = matchingPlan || plans[plans.length - 1] || null;
-
- // Return the matching plan or the highest tier available
- return selectedPlan;
-}
-
-// Format event tier for display
-export function formatEventTier(tier: number | string): string {
- if (typeof tier === "string") {
- return tier;
- }
-
- return tier >= 1_000_000 ? `${tier / 1_000_000}M` : `${tier / 1_000}k`;
-}
diff --git a/client/src/components/subscription/components/PlanDialog.tsx b/client/src/components/subscription/components/PlanDialog.tsx
index 68ca1033d..bd1ab7fde 100644
--- a/client/src/components/subscription/components/PlanDialog.tsx
+++ b/client/src/components/subscription/components/PlanDialog.tsx
@@ -1,13 +1,18 @@
+import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
+import { Slider } from "@/components/ui/slider";
import { toast } from "@/components/ui/sonner";
import { authClient } from "@/lib/auth";
import { BACKEND_URL } from "@/lib/const";
-import { getStripePrices, STRIPE_TIERS } from "@/lib/stripe";
+import { STRIPE_TIERS } from "@/lib/stripe";
+import { EVENT_TIERS, findPriceForTier, formatEventTier } from "@/lib/subscription/planUtils";
import { usePreviewSubscriptionUpdate, useUpdateSubscription } from "@/lib/subscription/useSubscriptionMutations";
import { cn } from "@/lib/utils";
-import { useState } from "react";
+import { ArrowRight } from "lucide-react";
+import { useMemo, useState } from "react";
import { CheckoutModal } from "./CheckoutModal";
import { PlanChangePreviewDialog } from "./PlanChangePreviewDialog";
+import { PlanRow } from "./PlanRow";
interface PlanDialogProps {
open: boolean;
@@ -16,44 +21,82 @@ interface PlanDialogProps {
hasActiveSubscription?: boolean;
}
+function parseCurrentPlan(currentPlanName?: string) {
+ if (!currentPlanName) return { tierIndex: 0, planType: "standard" as const, isAnnual: false };
+
+ const isAnnual = currentPlanName.includes("-annual");
+ const planType = currentPlanName.includes("pro") ? "pro" as const : "standard" as const;
+
+ // Find current tier index by matching event count
+ const currentTierIndex = STRIPE_TIERS.findIndex(tier => {
+ const suffix = isAnnual ? "-annual" : "";
+ const expectedName = `${planType}${tier.events >= 1_000_000 ? tier.events / 1_000_000 + "m" : tier.events / 1_000 + "k"}${suffix}`;
+ return expectedName === currentPlanName;
+ });
+
+ return {
+ tierIndex: currentTierIndex >= 0 ? currentTierIndex : 0,
+ planType,
+ isAnnual,
+ };
+}
+
export function PlanDialog({ open, onOpenChange, currentPlanName, hasActiveSubscription }: PlanDialogProps) {
- const [isAnnual, setIsAnnual] = useState(false);
+ const defaults = useMemo(() => parseCurrentPlan(currentPlanName), [currentPlanName]);
+
+ // Default to next tier up for existing subscribers
+ const defaultTierIndex = hasActiveSubscription
+ ? Math.min(defaults.tierIndex + 1, STRIPE_TIERS.length - 1)
+ : 0;
+
+ const [eventLimitIndex, setEventLimitIndex] = useState(defaultTierIndex);
+ const [isAnnual, setIsAnnual] = useState(defaults.isAnnual);
+ const [selectedPlan, setSelectedPlan] = useState<"standard" | "pro">(defaults.planType);
const [showProrationDialog, setShowProrationDialog] = useState(false);
const [pendingPriceId, setPendingPriceId] = useState<string | null>(null);
const [pendingPlanName, setPendingPlanName] = useState<string | null>(null);
const [checkoutClientSecret, setCheckoutClientSecret] = useState<string | null>(null);
- const stripePrices = getStripePrices();
+ const [isLoading, setIsLoading] = useState(false);
const { data: activeOrg } = authClient.useActiveOrganization();
const previewMutation = usePreviewSubscriptionUpdate();
const updateMutation = useUpdateSubscription();
- const handlePlanSelection = async (priceId: string, planName: string) => {
+ const eventLimit = EVENT_TIERS[eventLimitIndex];
+
+ const handleSelectPlan = async () => {
if (!activeOrg) {
toast.error("Please select an organization");
return;
}
+ if (eventLimit === "Custom") return;
+
+ const price = findPriceForTier(eventLimit, isAnnual ? "year" : "month", selectedPlan);
+ if (!price) {
+ toast.error("Could not find a matching plan.");
+ return;
+ }
+
// Don't allow selecting the current plan
- if (isCurrentPlan(planName)) {
+ if (currentPlanName === price.name) {
+ toast.error("You are already on this plan.");
return;
}
- setPendingPriceId(priceId);
- setPendingPlanName(planName);
+ setPendingPriceId(price.priceId);
+ setPendingPlanName(price.name);
+ setIsLoading(true);
try {
const baseUrl = window.location.origin;
if (hasActiveSubscription) {
- // First get proration preview
- const result = await previewMutation.mutateAsync({
+ await previewMutation.mutateAsync({
organizationId: activeOrg.id,
- newPriceId: priceId,
+ newPriceId: price.priceId,
});
-
setShowProrationDialog(true);
} else {
- // For new subscriptions, create a checkout session
const returnUrl = `${baseUrl}/settings/organization/subscription?session_id={CHECKOUT_SESSION_ID}`;
const response = await fetch(`${BACKEND_URL}/stripe/create-checkout-session`, {
@@ -63,7 +106,7 @@ export function PlanDialog({ open, onOpenChange, currentPlanName, hasActiveSubsc
},
credentials: "include",
body: JSON.stringify({
- priceId,
+ priceId: price.priceId,
returnUrl,
organizationId: activeOrg.id,
referral: window.Rewardful?.referral || undefined,
@@ -78,37 +121,26 @@ export function PlanDialog({ open, onOpenChange, currentPlanName, hasActiveSubsc
if (data.clientSecret) {
setCheckoutClientSecret(data.clientSecret);
- onOpenChange(false); // close plan selection dialog
+ onOpenChange(false);
} else {
throw new Error("Checkout session not received.");
}
}
} catch (error: any) {
toast.error(`${hasActiveSubscription ? "Failed to preview subscription" : "Checkout"} failed: ${error.message}`);
+ } finally {
+ setIsLoading(false);
}
};
- const getPriceForTier = (events: number, planType: "standard" | "pro") => {
- const suffix = isAnnual ? "-annual" : "";
- const planName = `${planType}${events >= 1_000_000 ? events / 1_000_000 + "m" : events / 1_000 + "k"}${suffix}`;
- const plan = stripePrices.find(p => p.name === planName);
- return plan;
- };
-
- const isCurrentPlan = (planName: string) => {
- return currentPlanName === planName;
- };
-
- const confirmSubscriptionUpdate = async (priceId: string, planName: string) => {
- if (!activeOrg) return;
+ const confirmSubscriptionUpdate = async () => {
+ if (!activeOrg || !pendingPriceId) return;
try {
await updateMutation.mutateAsync({
organizationId: activeOrg.id,
- newPriceId: priceId,
+ newPriceId: pendingPriceId,
});
-
- // Reload the page to reflect the new subscription
window.location.reload();
} catch (error) {
// Error is already handled by the mutation
@@ -125,112 +157,164 @@ export function PlanDialog({ open, onOpenChange, currentPlanName, hasActiveSubsc
const handlePreviewConfirm = async () => {
setShowProrationDialog(false);
if (pendingPriceId && pendingPlanName) {
- await confirmSubscriptionUpdate(pendingPriceId, pendingPlanName);
+ await confirmSubscriptionUpdate();
}
};
+ // Check if the currently selected options match the user's current plan
+ const isCurrentSelection = (() => {
+ if (!currentPlanName || eventLimit === "Custom") return false;
+ const price = findPriceForTier(eventLimit, isAnnual ? "year" : "month", selectedPlan);
+ return price?.name === currentPlanName;
+ })();
+
return (
<>
<Dialog open={open} onOpenChange={onOpenChange}>
- <DialogContent className="max-w-5xl max-h-[90vh] overflow-y-auto">
+ <DialogContent className="max-w-lg max-h-[90vh] overflow-y-auto">
<DialogHeader>
- <DialogTitle>Choose Your Plan</DialogTitle>
+ <div className="flex items-center justify-between">
+ <DialogTitle>Choose Your Plan</DialogTitle>
+ {/* Monthly/Annual toggle */}
+ <div className="relative flex bg-neutral-150 dark:bg-neutral-850 border border-neutral-250 dark:border-neutral-750 rounded-full p-0.5 text-sm">
+ <button
+ onClick={() => setIsAnnual(false)}
+ className={cn(
+ "px-2.5 py-1 rounded-full transition-colors cursor-pointer",
+ !isAnnual
+ ? "bg-white dark:bg-white/20 text-neutral-700 dark:text-neutral-100 font-medium"
+ : "text-neutral-600 dark:text-neutral-400 hover:text-neutral-900 dark:hover:text-neutral-200"
+ )}
+ >
+ Monthly
+ </button>
+ <button
+ onClick={() => setIsAnnual(true)}
+ className={cn(
+ "px-2.5 py-1 rounded-full transition-colors cursor-pointer",
+ isAnnual
+ ? "bg-white dark:bg-white/20 text-neutral-700 dark:text-neutral-100 font-medium"
+ : "text-neutral-600 dark:text-neutral-400 hover:text-neutral-900 dark:hover:text-neutral-200"
+ )}
+ >
+ Annual
+ </button>
+ {isAnnual && (
+ <span className="absolute -top-3 -right-12 text-[10px] text-white bg-emerald-500 px-1.5 py-0.5 rounded-full whitespace-nowrap">
+ 4 months free
+ </span>
+ )}
+ </div>
+ </div>
</DialogHeader>
- {/* Billing toggle */}
- <div className="flex justify-center mb-0">
- <div className="flex gap-3 text-sm">
- <button
- onClick={() => setIsAnnual(false)}
- className={cn(
- "px-4 py-2 rounded-full transition-colors cursor-pointer",
- !isAnnual
- ? "bg-emerald-500/20 text-emerald-400 font-medium"
- : "text-neutral-500 dark:text-neutral-400 hover:text-neutral-700 dark:hover:text-neutral-200"
- )}
+ <div className="space-y-6">
+ {/* Event slider */}
+ <div className="space-y-3">
+ <div className="flex items-center justify-between">
+ <span className="text-sm text-neutral-500 dark:text-neutral-400">
+ Monthly events
+ </span>
+ <span className="text-sm font-medium">
+ {eventLimit === "Custom"
+ ? "Custom"
+ : `${formatEventTier(eventLimit)} events`}
+ </span>
+ </div>
+ <Slider
+ value={[eventLimitIndex]}
+ onValueChange={([val]) => setEventLimitIndex(val)}
+ min={0}
+ max={EVENT_TIERS.length - 1}
+ step={1}
+ />
+ <div className="flex justify-between text-xs text-neutral-400 dark:text-neutral-500">
+ <span>{formatEventTier(EVENT_TIERS[0])}</span>
+ <span>
+ {typeof EVENT_TIERS[EVENT_TIERS.length - 1] === "string"
+ ? EVENT_TIERS[EVENT_TIERS.length - 1]
+ : formatEventTier(EVENT_TIERS[EVENT_TIERS.length - 1])}
+ </span>
+ </div>
+ </div>
+
+ {/* Plan rows */}
+ {eventLimit !== "Custom" ? (
+ <div className="space-y-2">
+ <PlanRow
+ plan="standard"
+ label="Standard"
+ description="Up to 5 sites, 3 team members, advanced features"
+ eventLimit={eventLimit}
+ isAnnual={isAnnual}
+ selectedPlan={selectedPlan}
+ onSelect={() => setSelectedPlan("standard")}
+ isCurrent={(() => {
+ const price = findPriceForTier(eventLimit, isAnnual ? "year" : "month", "standard");
+ return price?.name === currentPlanName;
+ })()}
+ />
+ <PlanRow
+ plan="pro"
+ label="Pro"
+ description="Unlimited sites, session replays"
+ eventLimit={eventLimit}
+ isAnnual={isAnnual}
+ selectedPlan={selectedPlan}
+ onSelect={() => setSelectedPlan("pro")}
+ isCurrent={(() => {
+ const price = findPriceForTier(eventLimit, isAnnual ? "year" : "month", "pro");
+ return price?.name === currentPlanName;
+ })()}
+ />
+ </div>
+ ) : (
+ <div className="p-4 rounded-lg border border-neutral-200 dark:border-neutral-800 text-center">
+ <p className="text-sm text-neutral-500 dark:text-neutral-400">
+ Need more than {formatEventTier(STRIPE_TIERS[STRIPE_TIERS.length - 1].events)} events? Contact us for a custom plan.
+ </p>
+ <a
+ href="mailto:[email protected]"
+ className="text-sm text-emerald-500 hover:text-emerald-400 font-medium"
+ >
+ </a>
+ </div>
+ )}
+
+ {/* Action button */}
+ {eventLimit !== "Custom" && (
+ <Button
+ className="w-full h-11"
+ variant="success"
+ onClick={handleSelectPlan}
+ disabled={isLoading || previewMutation.isPending || isCurrentSelection}
>
- Monthly
- </button>
- <button
- onClick={() => setIsAnnual(true)}
- className={cn(
- "px-4 py-2 rounded-full transition-colors cursor-pointer",
- isAnnual
- ? "bg-emerald-500/20 text-emerald-400 font-medium"
- : "text-neutral-500 dark:text-neutral-400 hover:text-neutral-700 dark:hover:text-neutral-200"
+ {isLoading || previewMutation.isPending
+ ? "Loading..."
+ : isCurrentSelection
+ ? "Current Plan"
+ : hasActiveSubscription
+ ? "Change Plan"
+ : "Subscribe"}
+ {!isLoading && !previewMutation.isPending && !isCurrentSelection && (
+ <ArrowRight className="ml-2 h-4 w-4" />
)}
+ </Button>
+ )}
+
+ {/* Features comparison link */}
+ <div className="text-center">
+ <a
+ href="https://www.rybbit.com/pricing"
+ target="_blank"
+ rel="noopener noreferrer"
+ className="text-neutral-600 dark:text-neutral-200 hover:text-neutral-900 dark:hover:text-neutral-100 text-sm underline"
>
- Annual
- <span className="ml-1 text-xs text-emerald-500">-33%</span>
- </button>
+ View detailed feature comparison →
+ </a>
</div>
</div>
-
- {/* Plan columns */}
- <div className="grid grid-cols-2 gap-6">
- {[
- { type: "standard" as const, title: "Standard", subtitle: "Core analytics features" },
- { type: "pro" as const, title: "Pro", subtitle: "Advanced features + session replays" },
- ].map(({ type, title, subtitle }) => {
- const tiers = STRIPE_TIERS;
-
- return (
- <div key={type} className="space-y-3">
- <div className="mb-4">
- <h3 className="text-xl font-bold">{title}</h3>
- <p className="text-sm text-neutral-500 dark:text-neutral-400">{subtitle}</p>
- </div>
- <div className="space-y-2">
- {tiers.map(tier => {
- const plan = getPriceForTier(tier.events, type);
- if (!plan) return null;
- const isCurrent = isCurrentPlan(plan.name);
- const isLoading = previewMutation.isPending && pendingPriceId === plan.priceId;
-
- return (
- <div
- key={plan.name}
- className={cn(
- "flex flex-col gap-2 justify-between p-3 rounded-lg border cursor-pointer",
- isCurrent
- ? "bg-emerald-500/10 border-emerald-500"
- : "bg-neutral-50 dark:bg-neutral-800/20 border-neutral-100 dark:border-neutral-700/50 hover:bg-neutral-200 dark:hover:bg-neutral-800/30",
- isLoading && "opacity-50"
- )}
- onClick={() => handlePlanSelection(plan.priceId, plan.name)}
- >
- <div className="flex justify-between w-full">
- <div className="text-neutral-900 dark:text-neutral-100 font-medium flex-end">
- {tier.shortName} events{" "}
- <span className="text-neutral-500 dark:text-neutral-400 text-xs font-normal">/ month</span>
- </div>
- <div className="text-xs text-neutral-500 dark:text-neutral-400">
- <span className="text-neutral-700 dark:text-neutral-200 font-semibold text-base">
- ${isAnnual ? Math.round(plan.price / 12) : plan.price}
- </span>{" "}
- /month
- </div>
- </div>
- </div>
- );
- })}
- </div>
- </div>
- );
- })}
- </div>
-
- {/* Features comparison link */}
- <div className="mt-3 text-center">
- <a
- href="https://www.rybbit.com/pricing"
- target="_blank"
- rel="noopener noreferrer"
- className="text-neutral-600 dark:text-neutral-200 hover:text-neutral-900 dark:hover:text-neutral-100 text-sm underline"
- >
- View detailed feature comparison →
- </a>
- </div>
</DialogContent>
</Dialog>
diff --git a/client/src/components/subscription/components/PlanRow.tsx b/client/src/components/subscription/components/PlanRow.tsx
new file mode 100644
index 000000000..e3d21f79c
--- /dev/null
+++ b/client/src/components/subscription/components/PlanRow.tsx
@@ -0,0 +1,102 @@
+import { cn } from "@/lib/utils";
+import { findPriceForTier } from "@/lib/subscription/planUtils";
+
+interface PlanRowProps {
+ plan: "standard" | "pro";
+ label: string;
+ description: string;
+ eventLimit: number | string;
+ isAnnual: boolean;
+ selectedPlan: string;
+ onSelect: () => void;
+ isCurrent?: boolean;
+ disabled?: boolean;
+ disabledReason?: string;
+}
+
+export function PlanRow({
+ plan,
+ label,
+ description,
+ eventLimit,
+ isAnnual,
+ selectedPlan,
+ onSelect,
+ isCurrent,
+ disabled,
+ disabledReason,
+}: PlanRowProps) {
+ const price =
+ eventLimit !== "Custom"
+ ? findPriceForTier(eventLimit, isAnnual ? "year" : "month", plan)
+ : null;
+ const displayPrice = price
+ ? isAnnual
+ ? Math.round(price.price / 12)
+ : price.price
+ : 0;
+
+ return (
+ <button
+ onClick={disabled ? undefined : onSelect}
+ className={cn(
+ "w-full rounded-2xl p-1.5 transition-all text-left",
+ disabled
+ ? "opacity-50 cursor-not-allowed"
+ : "cursor-pointer",
+ !disabled && selectedPlan === plan
+ ? "bg-emerald-500/10 border border-emerald-500/30"
+ : "bg-neutral-200/20 dark:bg-neutral-900/40 border border-neutral-200 dark:border-neutral-800"
+ )}
+ >
+ <div
+ className={cn(
+ "flex items-center justify-between p-3 rounded-xl border",
+ !disabled && selectedPlan === plan
+ ? "border-emerald-500 bg-neutral-50 dark:bg-neutral-800/60"
+ : "border-neutral-200 dark:border-neutral-800 bg-neutral-50/50 dark:bg-neutral-900",
+ !disabled && selectedPlan !== plan && "hover:border-neutral-300 dark:hover:border-neutral-700"
+ )}
+ >
+ <div className="flex items-center gap-3">
+ <div
+ className={cn(
+ "w-4 h-4 rounded-full border-2 flex items-center justify-center",
+ !disabled && selectedPlan === plan
+ ? "border-emerald-500"
+ : "border-neutral-300 dark:border-neutral-600"
+ )}
+ >
+ {!disabled && selectedPlan === plan && (
+ <div className="w-2 h-2 rounded-full bg-emerald-500" />
+ )}
+ </div>
+ <div>
+ <div className="flex items-center gap-2">
+ <span className="font-medium">{label}</span>
+ {isCurrent && (
+ <span className="text-[10px] text-emerald-500 bg-emerald-500/10 px-1.5 py-0.5 rounded">
+ Current
+ </span>
+ )}
+ {disabled && disabledReason && (
+ <span className="text-[10px] text-neutral-400 dark:text-neutral-500 bg-neutral-100 dark:bg-neutral-800 px-1.5 py-0.5 rounded">
+ {disabledReason}
+ </span>
+ )}
+ </div>
+ <p className="text-xs text-neutral-500 dark:text-neutral-400">
+ {description}
+ </p>
+ </div>
+ </div>
+ <div className="text-right">
+ <span className="font-semibold">${displayPrice}</span>
+ <span className="text-sm text-neutral-500 dark:text-neutral-400">
+ /mo
+ </span>
+ </div>
+ </div>
+ </button>
+ );
+}
diff --git a/client/src/lib/subscription/planUtils.tsx b/client/src/lib/subscription/planUtils.tsx
index 71000faff..e8cef019e 100644
--- a/client/src/lib/subscription/planUtils.tsx
+++ b/client/src/lib/subscription/planUtils.tsx
@@ -1,3 +1,5 @@
+import { getStripePrices, STRIPE_TIERS } from "../stripe";
+
// Helper function to format dates
export const formatDate = (dateString: string | Date | null | undefined) => {
if (!dateString) return "N/A";
@@ -8,3 +10,42 @@ export const formatDate = (dateString: string | Date | null | undefined) => {
day: "numeric",
}).format(date);
};
+
+export const EVENT_TIERS = [...STRIPE_TIERS.map(tier => tier.events), "Custom"];
+
+const stripePrices = getStripePrices();
+
+// Find the appropriate price for a tier at current event limit
+export function findPriceForTier(
+ eventLimit: number | string,
+ interval: "month" | "year",
+ planType: "basic" | "standard" | "pro" = "standard"
+) {
+ if (eventLimit === "Custom") {
+ return null;
+ }
+
+ const eventLimitValue = Number(eventLimit);
+ const isAnnual = interval === "year";
+
+ const plans = stripePrices.filter(
+ plan =>
+ (isAnnual
+ ? plan.name.startsWith(planType) && plan.name.includes("-annual")
+ : plan.name.startsWith(planType) && !plan.name.includes("-annual")) && plan.interval === interval
+ );
+
+ const matchingPlan = plans.find(plan => plan.events >= eventLimitValue);
+ const selectedPlan = matchingPlan || plans[plans.length - 1] || null;
+
+ return selectedPlan;
+}
+
+// Format event tier for display
+export function formatEventTier(tier: number | string): string {
+ if (typeof tier === "string") {
+ return tier;
+ }
+
+ return tier >= 1_000_000 ? `${tier / 1_000_000}M` : `${tier / 1_000}k`;
+}