forked from michaelshimeles/nextjs-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpricing.tsx
250 lines (229 loc) · 6.68 KB
/
pricing.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"use client";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { api } from "@/convex/_generated/api";
import { cn } from "@/lib/utils";
import { useUser } from "@clerk/nextjs";
import { useAction, useQuery } from "convex/react";
import { CheckCircle2, DollarSign } from "lucide-react";
import { motion } from "motion/react";
import { useRouter } from "next/navigation";
import { useState } from "react";
type PricingSwitchProps = {
onSwitch: (value: string) => void;
};
type PricingCardProps = {
user: any;
isYearly?: boolean;
title: string;
monthlyPrice?: number;
yearlyPrice?: number;
description: string;
features: string[];
actionLabel: string;
popular?: boolean;
exclusive?: boolean;
};
const PricingHeader = ({
title,
subtitle,
}: {
title: string;
subtitle: string;
}) => (
<div className="text-center mb-10">
{/* Pill badge */}
<div className="mx-auto w-fit rounded-full border border-blue-200 dark:border-blue-900 bg-blue-50 dark:bg-blue-900/30 px-4 py-1 mb-6">
<div className="flex items-center gap-2 text-sm font-medium text-blue-900 dark:text-blue-200">
<DollarSign className="h-4 w-4" />
<span>Pricing</span>
</div>
</div>
<h2 className="text-3xl md:text-4xl font-bold bg-clip-text text-transparent bg-gradient-to-r from-gray-900 via-blue-800 to-gray-900 dark:from-white dark:via-blue-300 dark:to-white pb-2">
{title}
</h2>
<p className="text-gray-600 dark:text-gray-300 mt-4 max-w-2xl mx-auto">
{subtitle}
</p>
</div>
);
const PricingSwitch = ({ onSwitch }: PricingSwitchProps) => (
<div className="flex justify-center items-center gap-3">
<Tabs defaultValue="0" className="w-[400px]" onValueChange={onSwitch}>
<TabsList className="w-full">
<TabsTrigger value="0" className="w-full">
Monthly
</TabsTrigger>
<TabsTrigger value="1" className="w-full">
Yearly
</TabsTrigger>
</TabsList>
</Tabs>
</div>
);
const PricingCard = ({
user,
isYearly,
title,
monthlyPrice,
yearlyPrice,
description,
features,
actionLabel,
popular,
exclusive,
}: PricingCardProps) => {
const router = useRouter();
const getProCheckoutUrl = useAction(api.subscriptions.getProOnboardingCheckoutUrl);
const subscriptionStatus = useQuery(api.subscriptions.getUserSubscriptionStatus);
const handleCheckout = async (interval: "month" | "year") => {
try {
const checkoutProUrl = await getProCheckoutUrl({
interval
});
if (checkoutProUrl) {
window.location.href = checkoutProUrl;
}
} catch (error) {
console.error("Failed to get checkout URL:", error);
}
};
return (
<Card
className={cn("w-full max-w-sm flex flex-col justify-between px-2 py-1", {
"relative border-2 border-blue-500 dark:border-blue-400": popular,
"shadow-2xl bg-gradient-to-b from-gray-900 to-gray-800 text-white":
exclusive,
})}
>
{popular && (
<div className="absolute -top-3 left-0 right-0 mx-auto w-fit rounded-full bg-blue-500 dark:bg-blue-400 px-3 py-1">
<p className="text-sm font-medium text-white">Most Popular</p>
</div>
)}
<div>
<CardHeader className="space-y-2 pb-4">
<CardTitle className="text-xl">{title}</CardTitle>
<CardDescription
className={cn("", {
"text-gray-300": exclusive,
})}
>
{description}
</CardDescription>
</CardHeader>
<CardContent className="pb-4">
<div className="flex items-baseline gap-1">
<span
className={cn("text-4xl font-bold", {
"text-white": exclusive,
})}
>
${isYearly ? yearlyPrice : monthlyPrice}
</span>
<span
className={cn("text-muted-foreground", {
"text-gray-300": exclusive,
})}
>
{isYearly ? "/yr" : "/mo"}
</span>
</div>
<div className="mt-6 space-y-2">
{features.map((feature) => (
<div key={feature} className="flex gap-2">
<CheckCircle2
className={cn("h-5 w-5 text-blue-500", {
"text-blue-400": exclusive,
})}
/>
<p
className={cn("text-muted-foreground", {
"text-gray-300": exclusive,
})}
>
{feature}
</p>
</div>
))}
</div>
</CardContent>
</div>
<CardFooter>
<Button
onClick={() => {
if (!user) {
router.push("/sign-in");
return;
}
handleCheckout("month")
}}
className={cn("w-full", {
"bg-blue-500 hover:bg-blue-400": popular,
"bg-white text-gray-900 hover:bg-gray-100": exclusive,
})}
>
{actionLabel}
</Button>
</CardFooter>
</Card>
);
};
export default function Pricing() {
const [isYearly, setIsYearly] = useState<boolean>(false);
const togglePricingPeriod = (value: string) =>
setIsYearly(parseInt(value) === 1);
const { user } = useUser();
const plans = [
{
title: "Pro",
monthlyPrice: 12,
yearlyPrice: 100,
description: "Advanced features for growing teams and businesses.",
features: [
"All Basic features",
"Up to 20 team members",
"50GB storage",
"Priority support",
"Advanced analytics",
],
actionLabel: "Get Pro",
popular: true,
},
];
return (
<section className="px-4">
<div className="max-w-7xl mx-auto">
<PricingHeader
title="Choose Your Plan"
subtitle="Select the perfect plan for your needs. All plans include a 14-day free trial."
/>
<PricingSwitch onSwitch={togglePricingPeriod} />
<motion.div
initial={{ opacity: 0, y: 20 }}
whileInView={{ opacity: 1, y: 0 }}
transition={{ duration: 0.5 }}
viewport={{ once: true }}
className="flex justify-center mt-10"
>
{plans.map((plan) => (
<PricingCard
key={plan.title}
user={user}
{...plan}
isYearly={isYearly}
/>
))}
</motion.div>
</div>
</section>
);
}