| 1 | 'use client' |
| 2 | |
| 3 | import { CreditCard, Sparkles } from 'lucide-react' |
| 4 | import { useCallback, useEffect |
| 5 | import { toast } |
| 6 | import { proPlanFeatures } |
| 7 | import { Badge } |
| 8 | import { Button } |
| 9 | import { |
| 10 | Card, |
| 11 | CardContent, |
| 12 | CardDescription, |
| 13 | CardHeader, |
| 14 | CardTitle, |
| 15 | } from '@/components/ui/card' |
| 16 | import { authClient, useSession |
| 17 | |
| 18 | interface SubscriptionState { |
| 19 | currentPeriodEnd?: string |
| 20 | productName?: string |
| 21 | status: 'loading |
| 22 | } |
| 23 | |
| 24 | export function BillingCard() |
| 25 | const session = |
| 26 | const [subscription, |
| 27 | status: 'loading', |
| 28 | }) |
| 29 | const [checkoutLoading, |
| 30 | |
| 31 | useEffect(() => |
| 32 | authClient.customer.subscriptions |
| 33 | .list({ |
| 34 | query: { |
| 35 | limit: 1, |
| 36 | active: true, |
| 37 | referenceId: session.data |
| 38 | }, |
| 39 | }) |
| 40 | .then(({ data |
| 41 | const sub = |
| 42 | if (sub) { |
| 43 | setSubscription({ |
| 44 | status: 'active', |
| 45 | currentPeriodEnd: sub.currentPeriodEnd |
| 46 | productName: |
| 47 | 'productName' in sub |
| 48 | }) |
| 49 | } else { |
| 50 | setSubscription({ status: |
| 51 | } |
| 52 | }) |
| 53 | .catch(() => |
| 54 | setSubscription({ status: |
| 55 | }) |
| 56 | }, [session.data |
| 57 | |
| 58 | const handleUpgrade = |
| 59 | setCheckoutLoading(true) |
| 60 | try { |
| 61 | await authClient.checkout |
| 62 | slug: 'pro-plan', |
| 63 | referenceId: session.data |
| 64 | }) |
| 65 | } catch { |
| 66 | toast.error('Failed to start checkout |
| 67 | setCheckoutLoading(false) |
| 68 | } |
| 69 | }, [session.data |
| 70 | |
| 71 | const handleManageBilling = |
| 72 | try { |
| 73 | await authClient.customer |
| 74 | } catch { |
| 75 | toast.error('Failed to open billing portal |
| 76 | } |
| 77 | }, []) |
| 78 | |
| 79 | if (subscription.status |
| 80 | return null |
| 81 | } |
| 82 | |
| 83 | return ( |
| 84 | <Card |
| 85 | className={ |
| 86 | subscription.status === |
| 87 | ? 'border-primary/30 bg-linear-to-br from-primary/5 to-transparent' |
| 88 | : '' |
| 89 | } |
| 90 | > |
| 91 | <CardHeader> |
| 92 | <div className='flex items-center justify-between |
| 93 | <div className='space-y-1 |
| 94 | <CardTitle className=' |
| 95 | Plan & Billing |
| 96 | {subscription.status === |
| 97 | <Badge |
| 98 | className='border-teal-300 bg-teal-100 text-teal-800 dark:border-teal-700 dark:bg-teal-900/40 dark:text-teal-300' |
| 99 | variant='outline' |
| 100 | > |
| 101 | Pro |
| 102 | </Badge> |
| 103 | ) : ( |
| 104 | <Badge variant=' |
| 105 | )} |
| 106 | </CardTitle> |
| 107 | <CardDescription> |
| 108 | {subscription.status === |
| 109 | ? 'Your workspace is on the Pro plan.' |
| 110 | : 'Upgrade to Pro to unlock premium features for your workspace.'} |
| 111 | </CardDescription> |
| 112 | </div> |
| 113 | </div> |
| 114 | </CardHeader> |
| 115 | <CardContent> |
| 116 | {subscription.status === |
| 117 | <div className='space-y-4 |
| 118 | <div className='grid grid-cols-2 gap-4 |
| 119 | <div className='rounded-lg border p-3 |
| 120 | <p className='text-muted-foreground text-sm |
| 121 | <p className='font-medium text-lg |
| 122 | {subscription.productName ?? |
| 123 | </p> |
| 124 | </div> |
| 125 | {subscription.currentPeriodEnd && |
| 126 | <div className='rounded-lg border p-3 |
| 127 | <p className='text-muted-foreground text-sm |
| 128 | <p className='font-medium text-lg |
| 129 | {new Date(subscription |
| 130 | undefined, |
| 131 | { |
| 132 | month: 'long', |
| 133 | day: 'numeric', |
| 134 | year: 'numeric', |
| 135 | } |
| 136 | )} |
| 137 | </p> |
| 138 | </div> |
| 139 | )} |
| 140 | </div> |
| 141 | <Button |
| 142 | onClick={handleManageBilling} |
| 143 | type='button' |
| 144 | variant='outline' |
| 145 | > |
| 146 | <CreditCard className=' |
| 147 | Manage Billing |
| 148 | </Button> |
| 149 | </div> |
| 150 | ) : ( |
| 151 | <div className='space-y-4 |
| 152 | <ul className='space-y-2 text-muted-foreground text-sm |
| 153 | {proPlanFeatures.map(( |
| 154 | <li className='flex items-center gap-2 |
| 155 | <Sparkles className=' |
| 156 | {e} |
| 157 | </li> |
| 158 | ))} |
| 159 | </ul> |
| 160 | <Button |
| 161 | loading={checkoutLoading} |
| 162 | onClick={handleUpgrade} |
| 163 | type='button' |
| 164 | > |
| 165 | <Sparkles className=' |
| 166 | Upgrade to Pro |
| 167 | </Button> |
| 168 | </div> |
| 169 | )} |
| 170 | </CardContent> |
| 171 | </Card> |
| 172 | ) |
| 173 | } |