|
| 1 | +import { License } from "@sourcebot/db"; |
| 2 | +import { Badge } from "@/components/ui/badge"; |
| 3 | +import { SettingsCard } from "../components/settingsCard"; |
| 4 | + |
| 5 | +interface CurrentPlanCardProps { |
| 6 | + license: License; |
| 7 | +} |
| 8 | + |
| 9 | +export function CurrentPlanCard({ license }: CurrentPlanCardProps) { |
| 10 | + if ( |
| 11 | + license.status !== 'active' |
| 12 | + && license.status !== 'trialing' |
| 13 | + && license.status !== 'past_due' |
| 14 | + ) { |
| 15 | + return null; |
| 16 | + } |
| 17 | + |
| 18 | + const { |
| 19 | + planName, |
| 20 | + unitAmount, |
| 21 | + currency, |
| 22 | + interval, |
| 23 | + intervalCount, |
| 24 | + seats, |
| 25 | + nextRenewalAt, |
| 26 | + nextRenewalAmount, |
| 27 | + } = license; |
| 28 | + |
| 29 | + if ( |
| 30 | + !planName |
| 31 | + || unitAmount === null |
| 32 | + || !currency |
| 33 | + || !interval |
| 34 | + || intervalCount === null |
| 35 | + || !nextRenewalAt |
| 36 | + ) { |
| 37 | + return null; |
| 38 | + } |
| 39 | + |
| 40 | + const monthlyPerSeat = normalizeToMonthly(unitAmount, interval, intervalCount); |
| 41 | + |
| 42 | + return ( |
| 43 | + <SettingsCard> |
| 44 | + <div className="flex items-center justify-between gap-6"> |
| 45 | + <div className="flex flex-col gap-1"> |
| 46 | + <div className="flex items-center gap-2"> |
| 47 | + <p className="font-medium">{planName} plan</p> |
| 48 | + <Badge variant="outline" className="border-primary/30 text-primary"> |
| 49 | + Current |
| 50 | + </Badge> |
| 51 | + </div> |
| 52 | + {monthlyPerSeat !== null ? ( |
| 53 | + <p className="text-sm text-muted-foreground"> |
| 54 | + {formatCurrency(monthlyPerSeat, currency)} per user/mo, billed {formatCadence(interval, intervalCount)} |
| 55 | + </p> |
| 56 | + ) : ( |
| 57 | + <p className="text-sm text-muted-foreground"> |
| 58 | + {formatCurrency(unitAmount, currency)} per user, billed {formatCadence(interval, intervalCount)} |
| 59 | + </p> |
| 60 | + )} |
| 61 | + </div> |
| 62 | + <div className="flex items-start gap-12"> |
| 63 | + <div className="flex flex-col items-end"> |
| 64 | + <p className="text-xs text-muted-foreground">Users</p> |
| 65 | + <p className="text-sm">{seats ?? 0}</p> |
| 66 | + </div> |
| 67 | + <div className="flex flex-col items-end"> |
| 68 | + <p className="text-xs text-muted-foreground">Next renewal</p> |
| 69 | + <p className="text-sm"> |
| 70 | + {formatCurrency(nextRenewalAmount ?? 0, currency)} on {formatDate(nextRenewalAt)} |
| 71 | + </p> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + </SettingsCard> |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +function normalizeToMonthly(unitAmount: number, interval: string, intervalCount: number): number | null { |
| 80 | + if (interval === 'year') { |
| 81 | + return unitAmount / (12 * intervalCount); |
| 82 | + } |
| 83 | + if (interval === 'month') { |
| 84 | + return unitAmount / intervalCount; |
| 85 | + } |
| 86 | + return null; |
| 87 | +} |
| 88 | + |
| 89 | +function formatCurrency(amountCents: number, currency: string): string { |
| 90 | + return new Intl.NumberFormat('en-US', { |
| 91 | + style: 'currency', |
| 92 | + currency: currency.toUpperCase(), |
| 93 | + minimumFractionDigits: 0, |
| 94 | + maximumFractionDigits: 2, |
| 95 | + }).format(amountCents / 100); |
| 96 | +} |
| 97 | + |
| 98 | +function formatCadence(interval: string, intervalCount: number): string { |
| 99 | + if (intervalCount === 1) { |
| 100 | + if (interval === 'year') { |
| 101 | + return 'annually'; |
| 102 | + } |
| 103 | + if (interval === 'month') { |
| 104 | + return 'monthly'; |
| 105 | + } |
| 106 | + if (interval === 'week') { |
| 107 | + return 'weekly'; |
| 108 | + } |
| 109 | + if (interval === 'day') { |
| 110 | + return 'daily'; |
| 111 | + } |
| 112 | + } |
| 113 | + if (interval === 'month' && intervalCount === 3) { |
| 114 | + return 'quarterly'; |
| 115 | + } |
| 116 | + if (interval === 'month' && intervalCount === 6) { |
| 117 | + return 'semi-annually'; |
| 118 | + } |
| 119 | + return `every ${intervalCount} ${interval}s`; |
| 120 | +} |
| 121 | + |
| 122 | +function formatDate(date: Date): string { |
| 123 | + return new Date(date).toLocaleDateString('en-US', { |
| 124 | + month: 'short', |
| 125 | + day: 'numeric', |
| 126 | + year: 'numeric', |
| 127 | + }); |
| 128 | +} |
0 commit comments