-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathsubscription-card.tsx
333 lines (324 loc) · 11.5 KB
/
subscription-card.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import Link from "next/link";
import { Subscription } from "@/types/subscription";
import { capitalizeInital } from "@/lib/utils";
import {
Card,
CardHeader,
CardTitle,
CardContent,
CardFooter,
} from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Progress } from "@/components/ui/progress";
import { Badge } from "@/components/ui/badge";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { useCancelSubscription } from "@/hooks/useCancelSubscription";
import { User } from "@supabase/supabase-js";
import { Info } from "lucide-react";
import { UsageType } from "../dashboard";
import { toast } from "sonner";
import { useUpgradeSubscription } from "@/hooks/useUpgradeSubscription";
import { useState } from "react";
type SubscriptionCardProps = {
subscription: Subscription | null;
usage?: UsageType;
openAppQueryParams?: string;
user: User;
loading: boolean;
};
const DEFAULT_OPEN_APP_CALLBACK = "pearai://pearai.pearai/auth";
export default function SubscriptionCard({
subscription,
usage,
openAppQueryParams,
user,
loading,
}: SubscriptionCardProps) {
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [isUpgradeDialogOpen, setIsUpgradeDialogOpen] = useState(false);
const { handleCancelSubscription, isCanceling, isCanceled } =
useCancelSubscription(user, subscription);
const { handleUpgradeSubscription, isUpgrading } = useUpgradeSubscription(
user,
subscription,
);
const handleCancelClick = () => {
if (isCanceled) {
window.location.href = "/pricing";
} else {
setIsDialogOpen(true);
}
};
const handleConfirmCancel = async () => {
try {
await handleCancelSubscription(subscription!.subscription_id);
} finally {
setIsDialogOpen(false);
}
};
const handleUpgradeSubscriptionClick = async () => {
try {
if (subscription?.subscription_id) {
const response = await fetch("/api/upgrade-subscription", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
subscriptionId: subscription.subscription_id,
}),
});
if (!response.ok) {
throw new Error("Failed to create checkout session");
}
const { checkoutUrl } = await response.json();
window.location.href = checkoutUrl;
} else {
toast.error(
"Failed to upgrade subscription. Subscription ID not found.",
);
}
} catch (error) {
toast.error("Failed to upgrade subscription.");
} finally {
setIsUpgradeDialogOpen(false);
}
};
if (!subscription) {
return (
<Card className="flex h-full flex-col overflow-auto bg-gray-100/10 text-card-foreground">
<div className="grid gap-4">
<CardHeader className="pb-4">
<CardTitle className="text-xl font-semibold">
Subscription
</CardTitle>
</CardHeader>
<CardContent>
<div className="flex items-center justify-between text-muted-foreground">
<p>You are currently not subscribed.</p>
</div>
</CardContent>
<CardFooter className="mt-8 pt-6">
<Button
variant="outline"
asChild
className="ml-auto text-primary-800"
>
<Link href="/pricing">Subscribe Now</Link>
</Button>
</CardFooter>
</div>
</Card>
);
}
return (
<Card className="overflow-auto bg-gray-100/10 text-card-foreground">
<div className="grid gap-4">
<CardHeader className="flex-row justify-between pb-4">
<CardTitle className="text-xl font-semibold">
Subscription & Usage
</CardTitle>
<div className="flex">
<Badge
variant="secondary"
className="border-primary-800 bg-primary-800/10 px-2 py-1 text-xs text-primary-800"
>
Pro - {capitalizeInital(subscription.pricing_tier)}
</Badge>
</div>
</CardHeader>
<CardContent>
{usage && (
<div className="mb-4">
<div className="flex justify-between">
<p className="font-medium">Requests</p>
<p className="text-sm/6 text-muted-foreground">
{loading ? (
"-"
) : (
<strong>
{usage?.percent_credit_used != null
? `${Math.min(usage.percent_credit_used, 100)}%`
: "Cannot find remaining percentage. Please contact PearAI support."}
</strong>
)}
</p>
</div>
<Progress
value={usage.percent_credit_used}
className="mb-2 mt-2 h-2 w-full"
indicatorColor="bg-primary-800 bg-opacity-75"
/>
<p className="text-sm text-muted-foreground">
{loading
? "-"
: `${Math.min(usage?.percent_credit_used ?? 0, 100)}% of PearAI Credits used`}
</p>
</div>
)}
<div className="mb-4">
<div className="flex justify-between">
<p className="font-medium">Current Plan</p>
<div className="flex items-center space-x-2">
<p className="text-muted-foreground">
{capitalizeInital(subscription.pricing_tier)}
</p>
{subscription.pricing_tier === "monthly" && (
<Dialog
open={isUpgradeDialogOpen}
onOpenChange={setIsUpgradeDialogOpen}
>
<DialogTrigger asChild>
<Button variant="outline" size="sm">
Upgrade
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Upgrade</DialogTitle>
<DialogDescription>
Are you sure you want to upgrade your subscription to
the Yearly Tier?
<br />
<br />
<b>
This change will take effect immediately, and be
charged on your current payment method. The price is
reflected on the{" "}
<a
href="/pricing"
target="_blank"
className="cursor-pointer text-primary-700 transition-colors hover:text-primary-800"
>
pricing page
</a>
.
</b>
<br />
<br />
We'll refund the remaining funds from the current
monthly subscription depending on the days remaining
on your cycle. You will not be able to downgrade
afterwards.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button
variant="outline"
onClick={() => setIsUpgradeDialogOpen(false)}
>
Cancel
</Button>
<Button
disabled={isUpgrading}
onClick={() => {
handleUpgradeSubscriptionClick();
}}
>
{isUpgrading ? "Upgrading..." : "Go to Upgrade"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)}
</div>
</div>
</div>
<div className="mb-4">
<div className="flex justify-between">
<p className="font-medium">Current Period</p>
<p className="text-sm/6 text-muted-foreground">
{new Date(
subscription.current_period_start * 1000,
).toLocaleDateString()}{" "}
-{" "}
{subscription.current_period_end
? new Date(
subscription.current_period_end * 1000,
).toLocaleDateString()
: "Now"}
</p>
</div>
</div>
<div className="mt-8 flex justify-between space-x-4">
<div className="hidden space-x-2 sm:block">
<Button variant="default" asChild>
<Link
href={DEFAULT_OPEN_APP_CALLBACK + "?" + openAppQueryParams}
target="_parent"
>
Open PearAI
</Link>
</Button>
</div>
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
<DialogTrigger asChild>
<Button
onClick={handleCancelClick}
disabled={isCanceling}
variant="link"
className="px-0"
>
{isCanceling
? "Canceling..."
: isCanceled
? "Subscription canceled, reactivate?"
: "Cancel Subscription"}
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Cancel Subscription</DialogTitle>
<DialogDescription>
Are you sure you want to cancel your subscription?
You'll lose access to premium features at the end of
your current billing period.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button
variant="outline"
onClick={() => setIsDialogOpen(false)}
className="mt-2"
>
Keep Subscription
</Button>
<Button
variant="destructive"
onClick={handleConfirmCancel}
disabled={isCanceling}
className="mt-2"
>
{isCanceling ? "Canceling..." : "Confirm Cancellation"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</div>
<div className="flex items-center">
<Info className="inline text-muted-foreground" size={14} />
<p className="ml-1.5 text-xs/6 text-muted-foreground">
Make sure PearAI is{" "}
<Button
variant="link"
asChild
className="p-0 text-xs text-primary-800"
>
<Link href="/pricing">installed.</Link>
</Button>{" "}
Use this button to open the app and login directly.
</p>
</div>
</CardContent>
</div>
</Card>
);
}