pricing-section.tsx212 lines, 6.3 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { useState } from "react"; |
| 4 | import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; |
| 5 | import { PricingCard, PricingFeature } from "@/components/pricing-card"; |
| 6 | import { SubscriptionPlan } from "@/lib/api/types"; |
| 7 | import { useCreateCheckoutSession } from "@/lib/hooks/queries/subscriptions"; |
| 8 | |
| 9 | interface PricingTier { |
| 10 | id: string; |
| 11 | name: string; |
| 12 | description: string; |
| 13 | popular?: boolean; |
| 14 | highlighted?: boolean; |
| 15 | prices: { |
| 16 | monthly: number; |
| 17 | yearly: number; |
| 18 | }; |
| 19 | features: PricingFeature[]; |
| 20 | stripePriceId: { |
| 21 | monthly: string; |
| 22 | yearly: string; |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | interface PricingSectionProps { |
| 27 | plans: SubscriptionPlan[]; |
| 28 | currentPlanId?: string; |
| 29 | className?: string; |
| 30 | } |
| 31 | |
| 32 | export default function PricingSection({ |
| 33 | plans, |
| 34 | currentPlanId, |
| 35 | className, |
| 36 | }: PricingSectionProps) { |
| 37 | const [billingInterval, setBillingInterval] = useState<"monthly" | "yearly">( |
| 38 | "monthly" |
| 39 | ); |
| 40 | const { mutate: createCheckout, isPending } = useCreateCheckoutSession(); |
| 41 | |
| 42 | // Group plans by billing interval to organize them |
| 43 | const getFilteredPlans = () => { |
| 44 | const filteredPlans = plans.filter( |
| 45 | (plan) => plan.billingInterval === billingInterval && plan.isActive |
| 46 | ); |
| 47 | |
| 48 | // Sort plans by price |
| 49 | return filteredPlans.sort((a, b) => a.price - b.price); |
| 50 | }; |
| 51 | |
| 52 | // Convert database plans to pricing tiers with features |
| 53 | const formatPlansAsTiers = (plans: SubscriptionPlan[]): PricingTier[] => { |
| 54 | return plans.map((plan) => { |
| 55 | // Generate features list |
| 56 | const features: PricingFeature[] = []; |
| 57 | |
| 58 | // Video limit feature |
| 59 | if (plan.videoLimit === -1) { |
| 60 | features.push({ |
| 61 | text: "Unlimited videos", |
| 62 | included: true, |
| 63 | highlight: true, |
| 64 | }); |
| 65 | } else { |
| 66 | features.push({ |
| 67 | text: `${plan.videoLimit} videos per ${plan.billingInterval}`, |
| 68 | included: true, |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | // Minutes limit feature |
| 73 | if (plan.minutesLimit === -1) { |
| 74 | features.push({ |
| 75 | text: "Unlimited minutes", |
| 76 | included: true, |
| 77 | highlight: true, |
| 78 | }); |
| 79 | } else { |
| 80 | features.push({ |
| 81 | text: `${plan.minutesLimit} minutes per ${plan.billingInterval}`, |
| 82 | included: true, |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | // Add other common features |
| 87 | features.push( |
| 88 | { text: "Video transcription", included: true }, |
| 89 | { text: "AI summaries", included: true }, |
| 90 | { text: "Key point extraction", included: true }, |
| 91 | { text: "Topic detection", included: true } |
| 92 | ); |
| 93 | |
| 94 | // Add premium features based on tier |
| 95 | if (plan.price > 0) { |
| 96 | features.push({ text: "Priority processing", included: true }); |
| 97 | } else { |
| 98 | features.push({ text: "Priority processing", included: false }); |
| 99 | } |
| 100 | |
| 101 | if (plan.price >= 20) { |
| 102 | features.push({ text: "Advanced analytics", included: true }); |
| 103 | } else { |
| 104 | features.push({ text: "Advanced analytics", included: false }); |
| 105 | } |
| 106 | |
| 107 | // Determine if this is a popular plan (middle tier is usually highlighted) |
| 108 | const isPopular = plan.name.includes("Pro") || plan.name.includes("Plus"); |
| 109 | |
| 110 | // Determine if this is a highlighted plan (usually the best value plan) |
| 111 | const isHighlighted = isPopular; |
| 112 | |
| 113 | // Format price for display |
| 114 | const monthlyPrice = plan.price; |
| 115 | // Calculate yearly price (monthly price * 10 for 2 months free) |
| 116 | const yearlyPrice = |
| 117 | plan.billingInterval === "yearly" ? plan.price / 12 : plan.price * 10; |
| 118 | |
| 119 | return { |
| 120 | id: plan.id, |
| 121 | name: plan.name, |
| 122 | description: plan.description, |
| 123 | popular: isPopular, |
| 124 | highlighted: isHighlighted, |
| 125 | prices: { |
| 126 | monthly: monthlyPrice, |
| 127 | yearly: yearlyPrice, |
| 128 | }, |
| 129 | features, |
| 130 | stripePriceId: { |
| 131 | monthly: plan.stripePriceId, |
| 132 | yearly: plan.stripePriceId, // You would need yearly IDs in your plans |
| 133 | }, |
| 134 | }; |
| 135 | }); |
| 136 | }; |
| 137 | |
| 138 | const handleSubscribe = (planId: string) => { |
| 139 | createCheckout({ planId }); |
| 140 | }; |
| 141 | |
| 142 | const filteredPlans = getFilteredPlans(); |
| 143 | const pricingTiers = formatPlansAsTiers(filteredPlans); |
| 144 | |
| 145 | return ( |
| 146 | <div className={className}> |
| 147 | {/* Billing interval toggle */} |
| 148 | <div className="flex justify-center mb-8"> |
| 149 | <ToggleGroup |
| 150 | type="single" |
| 151 | value={billingInterval} |
| 152 | onValueChange={(value: string) => { |
| 153 | if (value) setBillingInterval(value as "monthly" | "yearly"); |
| 154 | }} |
| 155 | className="bg-muted rounded-full p-1.5" |
| 156 | > |
| 157 | <ToggleGroupItem |
| 158 | value="monthly" |
| 159 | className="rounded-full px-4 py-2 text-sm" |
| 160 | aria-label="Monthly billing" |
| 161 | > |
| 162 | Monthly |
| 163 | </ToggleGroupItem> |
| 164 | <ToggleGroupItem |
| 165 | value="yearly" |
| 166 | className="rounded-full px-4 py-2 text-sm" |
| 167 | aria-label="Yearly billing" |
| 168 | > |
| 169 | Yearly |
| 170 | <span className="ml-1.5 inline-flex items-center rounded-full border px-1.5 text-xs font-medium text-primary border-primary/30 bg-primary/10"> |
| 171 | Save 16% |
| 172 | </span> |
| 173 | </ToggleGroupItem> |
| 174 | </ToggleGroup> |
| 175 | </div> |
| 176 | |
| 177 | {/* Pricing cards */} |
| 178 | <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"> |
| 179 | {pricingTiers.map((tier) => ( |
| 180 | <PricingCard |
| 181 | key={tier.id} |
| 182 | name={tier.name} |
| 183 | description={tier.description} |
| 184 | price={tier.prices[billingInterval]} |
| 185 | priceUnit="$" |
| 186 | billingPeriod={billingInterval} |
| 187 | features={tier.features} |
| 188 | buttonText="Subscribe" |
| 189 | onSubscribe={() => handleSubscribe(tier.id)} |
| 190 | popular={tier.popular} |
| 191 | highlighted={tier.highlighted} |
| 192 | currentPlan={currentPlanId === tier.id} |
| 193 | isLoading={isPending} |
| 194 | /> |
| 195 | ))} |
| 196 | </div> |
| 197 | |
| 198 | {/* Free plan info */} |
| 199 | {!pricingTiers.some((tier) => tier.prices[billingInterval] === 0) && ( |
| 200 | <div className="mt-6 text-center"> |
| 201 | <p className="text-sm text-muted-foreground"> |
| 202 | Not ready to commit? Try our{" "} |
| 203 | <a href="#" className="text-primary hover:underline"> |
| 204 | free plan |
| 205 | </a>{" "} |
| 206 | with 3 videos and 30 minutes per month. |
| 207 | </p> |
| 208 | </div> |
| 209 | )} |
| 210 | </div> |
| 211 | ); |
| 212 | } |