@umituz/web-dashboard 2.2.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +12 -1
- package/src/domains/analytics/components/AnalyticsLayout.tsx +100 -64
- package/src/domains/analytics/types/analytics.ts +45 -0
- package/src/domains/auth/components/AuthLayout.tsx +1 -0
- package/src/domains/auth/components/LoginForm.tsx +59 -0
- package/src/domains/auth/components/RegisterForm.tsx +59 -0
- package/src/domains/auth/types/auth.ts +12 -0
- package/src/domains/billing/components/BillingLayout.tsx +63 -0
- package/src/domains/billing/components/BillingPortal.tsx +198 -0
- package/src/domains/billing/components/InvoiceCard.tsx +143 -0
- package/src/domains/billing/components/PaymentMethodsList.tsx +116 -0
- package/src/domains/billing/components/PlanComparison.tsx +199 -0
- package/src/domains/billing/components/UsageCard.tsx +103 -0
- package/src/domains/billing/components/index.ts +12 -0
- package/src/domains/billing/hooks/index.ts +7 -0
- package/src/domains/billing/hooks/useBilling.ts +385 -0
- package/src/domains/billing/index.ts +69 -0
- package/src/domains/billing/types/billing.ts +347 -0
- package/src/domains/billing/types/index.ts +28 -0
- package/src/domains/billing/utils/billing.ts +344 -0
- package/src/domains/billing/utils/index.ts +29 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Billing Portal Component
|
|
3
|
+
*
|
|
4
|
+
* Main billing portal with tabs
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { CreditCard, FileText, BarChart3, Settings, Loader2, AlertCircle } from "lucide-react";
|
|
8
|
+
import { cn } from "@umituz/web-design-system/utils";
|
|
9
|
+
import { Button } from "@umituz/web-design-system/atoms";
|
|
10
|
+
import type { BillingPortalProps } from "../types/billing";
|
|
11
|
+
import { UsageCard } from "./UsageCard";
|
|
12
|
+
import { PaymentMethodsList } from "./PaymentMethodsList";
|
|
13
|
+
import { InvoiceCard } from "./InvoiceCard";
|
|
14
|
+
import { getDaysRemaining, getStatusColor, getStatusLabel, formatPrice } from "../utils/billing";
|
|
15
|
+
|
|
16
|
+
export const BillingPortal = ({
|
|
17
|
+
billing,
|
|
18
|
+
loading = false,
|
|
19
|
+
error,
|
|
20
|
+
showTabs = true,
|
|
21
|
+
activeTab = "overview",
|
|
22
|
+
onTabChange,
|
|
23
|
+
}: BillingPortalProps) => {
|
|
24
|
+
const tabs = [
|
|
25
|
+
{ id: "overview", label: "Overview", icon: BarChart3 },
|
|
26
|
+
{ id: "payment-methods", label: "Payment Methods", icon: CreditCard },
|
|
27
|
+
{ id: "invoices", label: "Invoices", icon: FileText },
|
|
28
|
+
{ id: "plan", label: "Plan", icon: Settings },
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
if (loading) {
|
|
32
|
+
return (
|
|
33
|
+
<div className="flex items-center justify-center py-24">
|
|
34
|
+
<Loader2 className="h-12 w-12 animate-spin text-muted-foreground" />
|
|
35
|
+
</div>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (error) {
|
|
40
|
+
return (
|
|
41
|
+
<div className="flex items-center justify-center py-24 gap-4 text-destructive">
|
|
42
|
+
<AlertCircle className="h-6 w-6" />
|
|
43
|
+
<p>{error}</p>
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!billing) return null;
|
|
49
|
+
|
|
50
|
+
const renderContent = () => {
|
|
51
|
+
switch (activeTab) {
|
|
52
|
+
case "overview":
|
|
53
|
+
return (
|
|
54
|
+
<div className="space-y-6">
|
|
55
|
+
{/* Current Subscription */}
|
|
56
|
+
<div className="p-6 rounded-xl border border-border bg-background">
|
|
57
|
+
<h3 className="text-lg font-semibold text-foreground mb-4">
|
|
58
|
+
Current Subscription
|
|
59
|
+
</h3>
|
|
60
|
+
<div className="flex items-start justify-between">
|
|
61
|
+
<div>
|
|
62
|
+
<p className="text-2xl font-bold text-foreground">
|
|
63
|
+
{billing.subscription.plan.name}
|
|
64
|
+
</p>
|
|
65
|
+
<p className={cn("text-sm font-medium", getStatusColor(billing.subscription.status))}>
|
|
66
|
+
{getStatusLabel(billing.subscription.status)}
|
|
67
|
+
</p>
|
|
68
|
+
<p className="text-sm text-muted-foreground mt-1">
|
|
69
|
+
{formatPrice(
|
|
70
|
+
getPlanPrice(billing.subscription.plan, billing.subscription.cycle),
|
|
71
|
+
billing.subscription.plan.currency
|
|
72
|
+
)}
|
|
73
|
+
/{billing.subscription.cycle}
|
|
74
|
+
</p>
|
|
75
|
+
</div>
|
|
76
|
+
<div className="text-right">
|
|
77
|
+
{billing.subscription.status === "trialing" && billing.subscription.trialEnd && (
|
|
78
|
+
<p className="text-sm text-muted-foreground">
|
|
79
|
+
{getDaysRemaining(billing.subscription.trialEnd)} days left in trial
|
|
80
|
+
</p>
|
|
81
|
+
)}
|
|
82
|
+
{billing.upcomingInvoice && (
|
|
83
|
+
<p className="text-sm text-muted-foreground">
|
|
84
|
+
Next billing:{" "}
|
|
85
|
+
{new Date(billing.upcomingInvoice.date).toLocaleDateString()}
|
|
86
|
+
</p>
|
|
87
|
+
)}
|
|
88
|
+
</div>
|
|
89
|
+
</div>
|
|
90
|
+
</div>
|
|
91
|
+
|
|
92
|
+
{/* Usage Metrics */}
|
|
93
|
+
{billing.usage.length > 0 && (
|
|
94
|
+
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
|
95
|
+
{billing.usage.map((metric) => (
|
|
96
|
+
<UsageCard key={metric.id} metric={metric} />
|
|
97
|
+
))}
|
|
98
|
+
</div>
|
|
99
|
+
)}
|
|
100
|
+
|
|
101
|
+
{/* Upcoming Invoice */}
|
|
102
|
+
{billing.upcomingInvoice && (
|
|
103
|
+
<div className="p-6 rounded-xl border border-border bg-background">
|
|
104
|
+
<h3 className="text-lg font-semibold text-foreground mb-2">
|
|
105
|
+
Upcoming Invoice
|
|
106
|
+
</h3>
|
|
107
|
+
<div className="flex items-center justify-between">
|
|
108
|
+
<div>
|
|
109
|
+
<p className="text-3xl font-bold text-foreground">
|
|
110
|
+
{formatPrice(billing.upcomingInvoice.amount, billing.upcomingInvoice.currency)}
|
|
111
|
+
</p>
|
|
112
|
+
<p className="text-sm text-muted-foreground">
|
|
113
|
+
Due {new Date(billing.upcomingInvoice.date).toLocaleDateString()}
|
|
114
|
+
</p>
|
|
115
|
+
</div>
|
|
116
|
+
</div>
|
|
117
|
+
</div>
|
|
118
|
+
)}
|
|
119
|
+
</div>
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
case "payment-methods":
|
|
123
|
+
return (
|
|
124
|
+
<PaymentMethodsList
|
|
125
|
+
paymentMethods={billing.paymentMethods}
|
|
126
|
+
onAddNew={() => console.log("Add payment method")}
|
|
127
|
+
onSetDefault={(id) => console.log("Set default:", id)}
|
|
128
|
+
onRemove={(id) => console.log("Remove:", id)}
|
|
129
|
+
/>
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
case "invoices":
|
|
133
|
+
return (
|
|
134
|
+
<div className="space-y-4">
|
|
135
|
+
{billing.recentInvoices.length > 0 ? (
|
|
136
|
+
billing.recentInvoices.map((invoice) => (
|
|
137
|
+
<InvoiceCard
|
|
138
|
+
key={invoice.id}
|
|
139
|
+
invoice={invoice}
|
|
140
|
+
onClick={(inv) => console.log("View invoice:", inv)}
|
|
141
|
+
/>
|
|
142
|
+
))
|
|
143
|
+
) : (
|
|
144
|
+
<div className="text-center py-12 text-muted-foreground">
|
|
145
|
+
<FileText className="h-12 w-12 mx-auto mb-4 opacity-50" />
|
|
146
|
+
<p>No invoices yet</p>
|
|
147
|
+
</div>
|
|
148
|
+
)}
|
|
149
|
+
</div>
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
case "plan":
|
|
153
|
+
return (
|
|
154
|
+
<div className="text-center py-12">
|
|
155
|
+
<p className="text-muted-foreground">Plan management coming soon...</p>
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
default:
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
<div className="w-full">
|
|
166
|
+
{/* Tabs */}
|
|
167
|
+
{showTabs && (
|
|
168
|
+
<div className="border-b border-border mb-6">
|
|
169
|
+
<div className="flex gap-6">
|
|
170
|
+
{tabs.map((tab) => {
|
|
171
|
+
const Icon = tab.icon;
|
|
172
|
+
return (
|
|
173
|
+
<button
|
|
174
|
+
key={tab.id}
|
|
175
|
+
onClick={() => onTabChange?.(tab.id)}
|
|
176
|
+
className={cn(
|
|
177
|
+
"flex items-center gap-2 pb-4 border-b-2 transition-colors",
|
|
178
|
+
activeTab === tab.id
|
|
179
|
+
? "border-primary text-foreground"
|
|
180
|
+
: "border-transparent text-muted-foreground hover:text-foreground"
|
|
181
|
+
)}
|
|
182
|
+
>
|
|
183
|
+
<Icon className="h-4 w-4" />
|
|
184
|
+
<span className="font-medium">{tab.label}</span>
|
|
185
|
+
</button>
|
|
186
|
+
);
|
|
187
|
+
})}
|
|
188
|
+
</div>
|
|
189
|
+
</div>
|
|
190
|
+
)}
|
|
191
|
+
|
|
192
|
+
{/* Content */}
|
|
193
|
+
{renderContent()}
|
|
194
|
+
</div>
|
|
195
|
+
);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
export default BillingPortal;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Invoice Card Component
|
|
3
|
+
*
|
|
4
|
+
* Display invoice information
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { FileText, Download, ExternalLink } from "lucide-react";
|
|
8
|
+
import { cn } from "@umituz/web-design-system/utils";
|
|
9
|
+
import { Button } from "@umituz/web-design-system/atoms";
|
|
10
|
+
import type { InvoiceCardProps } from "../types/billing";
|
|
11
|
+
import { formatPrice, getInvoiceStatusColor, getInvoiceStatusLabel } from "../utils/billing";
|
|
12
|
+
|
|
13
|
+
export const InvoiceCard = ({
|
|
14
|
+
invoice,
|
|
15
|
+
compact = false,
|
|
16
|
+
onClick,
|
|
17
|
+
}: InvoiceCardProps) => {
|
|
18
|
+
const handleClick = () => {
|
|
19
|
+
onClick?.(invoice);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
if (compact) {
|
|
23
|
+
return (
|
|
24
|
+
<div
|
|
25
|
+
onClick={handleClick}
|
|
26
|
+
className="flex items-center justify-between p-4 rounded-lg border border-border hover:border-primary/50 bg-background cursor-pointer transition-colors"
|
|
27
|
+
>
|
|
28
|
+
<div className="flex items-center gap-3">
|
|
29
|
+
<div className="w-10 h-10 rounded-lg bg-muted flex items-center justify-center">
|
|
30
|
+
<FileText className="h-5 w-5 text-muted-foreground" />
|
|
31
|
+
</div>
|
|
32
|
+
<div>
|
|
33
|
+
<p className="font-medium text-foreground text-sm">
|
|
34
|
+
{invoice.number}
|
|
35
|
+
</p>
|
|
36
|
+
<p className="text-xs text-muted-foreground">
|
|
37
|
+
{new Date(invoice.date).toLocaleDateString()}
|
|
38
|
+
</p>
|
|
39
|
+
</div>
|
|
40
|
+
</div>
|
|
41
|
+
|
|
42
|
+
<div className="text-right">
|
|
43
|
+
<p className="font-bold text-foreground">
|
|
44
|
+
{formatPrice(invoice.amount, invoice.currency)}
|
|
45
|
+
</p>
|
|
46
|
+
<p className={cn("text-xs font-medium", getInvoiceStatusColor(invoice.status))}>
|
|
47
|
+
{getInvoiceStatusLabel(invoice.status)}
|
|
48
|
+
</p>
|
|
49
|
+
</div>
|
|
50
|
+
</div>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<div
|
|
56
|
+
onClick={handleClick}
|
|
57
|
+
className="p-6 rounded-xl border border-border hover:border-primary/50 bg-background cursor-pointer transition-colors"
|
|
58
|
+
>
|
|
59
|
+
{/* Header */}
|
|
60
|
+
<div className="flex items-start justify-between mb-4">
|
|
61
|
+
<div className="flex items-center gap-3">
|
|
62
|
+
<div className="w-12 h-12 rounded-lg bg-muted flex items-center justify-center">
|
|
63
|
+
<FileText className="h-6 w-6 text-muted-foreground" />
|
|
64
|
+
</div>
|
|
65
|
+
<div>
|
|
66
|
+
<p className="font-bold text-foreground">{invoice.number}</p>
|
|
67
|
+
<p className="text-sm text-muted-foreground">
|
|
68
|
+
{new Date(invoice.date).toLocaleDateString(undefined, {
|
|
69
|
+
year: "numeric",
|
|
70
|
+
month: "long",
|
|
71
|
+
day: "numeric",
|
|
72
|
+
})}
|
|
73
|
+
</p>
|
|
74
|
+
</div>
|
|
75
|
+
</div>
|
|
76
|
+
|
|
77
|
+
<div className="text-right">
|
|
78
|
+
<p className="text-2xl font-bold text-foreground">
|
|
79
|
+
{formatPrice(invoice.amount, invoice.currency)}
|
|
80
|
+
</p>
|
|
81
|
+
<p className={cn("text-sm font-medium", getInvoiceStatusColor(invoice.status))}>
|
|
82
|
+
{getInvoiceStatusLabel(invoice.status)}
|
|
83
|
+
</p>
|
|
84
|
+
</div>
|
|
85
|
+
</div>
|
|
86
|
+
|
|
87
|
+
{/* Items Preview */}
|
|
88
|
+
{invoice.items && invoice.items.length > 0 && (
|
|
89
|
+
<div className="space-y-2 mb-4">
|
|
90
|
+
{invoice.items.slice(0, 2).map((item, index) => (
|
|
91
|
+
<div key={index} className="flex justify-between text-sm">
|
|
92
|
+
<span className="text-muted-foreground">{item.description}</span>
|
|
93
|
+
<span className="text-foreground">
|
|
94
|
+
{formatPrice(item.amount, invoice.currency)}
|
|
95
|
+
</span>
|
|
96
|
+
</div>
|
|
97
|
+
))}
|
|
98
|
+
{invoice.items.length > 2 && (
|
|
99
|
+
<p className="text-xs text-muted-foreground">
|
|
100
|
+
+{invoice.items.length - 2} more items
|
|
101
|
+
</p>
|
|
102
|
+
)}
|
|
103
|
+
</div>
|
|
104
|
+
)}
|
|
105
|
+
|
|
106
|
+
{/* Actions */}
|
|
107
|
+
<div className="flex items-center justify-between pt-4 border-t border-border">
|
|
108
|
+
<p className="text-xs text-muted-foreground">
|
|
109
|
+
Due {new Date(invoice.dueDate).toLocaleDateString()}
|
|
110
|
+
</p>
|
|
111
|
+
|
|
112
|
+
<div className="flex items-center gap-2">
|
|
113
|
+
{invoice.invoiceUrl && (
|
|
114
|
+
<Button
|
|
115
|
+
variant="ghost"
|
|
116
|
+
size="sm"
|
|
117
|
+
onClick={(e) => {
|
|
118
|
+
e.stopPropagation();
|
|
119
|
+
window.open(invoice.invoiceUrl, "_blank");
|
|
120
|
+
}}
|
|
121
|
+
>
|
|
122
|
+
<ExternalLink className="h-4 w-4" />
|
|
123
|
+
</Button>
|
|
124
|
+
)}
|
|
125
|
+
{invoice.pdfUrl && (
|
|
126
|
+
<Button
|
|
127
|
+
variant="ghost"
|
|
128
|
+
size="sm"
|
|
129
|
+
onClick={(e) => {
|
|
130
|
+
e.stopPropagation();
|
|
131
|
+
window.open(invoice.pdfUrl, "_blank");
|
|
132
|
+
}}
|
|
133
|
+
>
|
|
134
|
+
<Download className="h-4 w-4" />
|
|
135
|
+
</Button>
|
|
136
|
+
)}
|
|
137
|
+
</div>
|
|
138
|
+
</div>
|
|
139
|
+
</div>
|
|
140
|
+
);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export default InvoiceCard;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payment Methods List Component
|
|
3
|
+
*
|
|
4
|
+
* Display and manage payment methods
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { CreditCard, Trash2, Check, Plus, Loader2 } from "lucide-react";
|
|
8
|
+
import { cn } from "@umituz/web-design-system/utils";
|
|
9
|
+
import { Button } from "@umituz/web-design-system/atoms";
|
|
10
|
+
import type { PaymentMethodsListProps } from "../types/billing";
|
|
11
|
+
import { formatCardNumber, formatExpiry } from "../utils/billing";
|
|
12
|
+
|
|
13
|
+
export const PaymentMethodsList = ({
|
|
14
|
+
paymentMethods,
|
|
15
|
+
loading = false,
|
|
16
|
+
onSetDefault,
|
|
17
|
+
onRemove,
|
|
18
|
+
onAddNew,
|
|
19
|
+
}: PaymentMethodsListProps) => {
|
|
20
|
+
if (loading) {
|
|
21
|
+
return (
|
|
22
|
+
<div className="flex items-center justify-center py-12">
|
|
23
|
+
<Loader2 className="h-8 w-8 animate-spin text-muted-foreground" />
|
|
24
|
+
</div>
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (paymentMethods.length === 0) {
|
|
29
|
+
return (
|
|
30
|
+
<div className="text-center py-12">
|
|
31
|
+
<CreditCard className="h-12 w-12 mx-auto text-muted-foreground mb-4" />
|
|
32
|
+
<p className="text-muted-foreground mb-6">No payment methods yet</p>
|
|
33
|
+
<Button onClick={onAddNew} variant="outline">
|
|
34
|
+
<Plus className="h-4 w-4 mr-2" />
|
|
35
|
+
Add Payment Method
|
|
36
|
+
</Button>
|
|
37
|
+
</div>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<div className="space-y-4">
|
|
43
|
+
{paymentMethods.map((method) => (
|
|
44
|
+
<div
|
|
45
|
+
key={method.id}
|
|
46
|
+
className={cn(
|
|
47
|
+
"flex items-center justify-between p-4 rounded-xl border",
|
|
48
|
+
method.isDefault
|
|
49
|
+
? "border-primary bg-primary/5"
|
|
50
|
+
: "border-border bg-background"
|
|
51
|
+
)}
|
|
52
|
+
>
|
|
53
|
+
<div className="flex items-center gap-4">
|
|
54
|
+
{/* Card Icon */}
|
|
55
|
+
<div className="w-12 h-8 rounded bg-gradient-to-br from-blue-600 to-purple-600 flex items-center justify-center">
|
|
56
|
+
{method.card && (
|
|
57
|
+
<CreditCard className="h-5 w-5 text-white" />
|
|
58
|
+
)}
|
|
59
|
+
</div>
|
|
60
|
+
|
|
61
|
+
{/* Card Details */}
|
|
62
|
+
<div>
|
|
63
|
+
<p className="font-medium text-foreground">
|
|
64
|
+
{method.card && formatCardNumber(method.card.last4, method.card.brand)}
|
|
65
|
+
</p>
|
|
66
|
+
<p className="text-sm text-muted-foreground">
|
|
67
|
+
Expires {method.card && formatExpiry(method.card.expiryMonth, method.card.expiryYear)}
|
|
68
|
+
</p>
|
|
69
|
+
</div>
|
|
70
|
+
|
|
71
|
+
{/* Default Badge */}
|
|
72
|
+
{method.isDefault && (
|
|
73
|
+
<span className="text-xs bg-primary text-primary-foreground px-2 py-1 rounded-full font-medium">
|
|
74
|
+
Default
|
|
75
|
+
</span>
|
|
76
|
+
)}
|
|
77
|
+
</div>
|
|
78
|
+
|
|
79
|
+
{/* Actions */}
|
|
80
|
+
<div className="flex items-center gap-2">
|
|
81
|
+
{!method.isDefault && onSetDefault && (
|
|
82
|
+
<Button
|
|
83
|
+
variant="ghost"
|
|
84
|
+
size="sm"
|
|
85
|
+
onClick={() => onSetDefault(method.id)}
|
|
86
|
+
className="text-muted-foreground hover:text-foreground"
|
|
87
|
+
>
|
|
88
|
+
<Check className="h-4 w-4" />
|
|
89
|
+
</Button>
|
|
90
|
+
)}
|
|
91
|
+
{onRemove && paymentMethods.length > 1 && (
|
|
92
|
+
<Button
|
|
93
|
+
variant="ghost"
|
|
94
|
+
size="sm"
|
|
95
|
+
onClick={() => onRemove(method.id)}
|
|
96
|
+
className="text-muted-foreground hover:text-destructive"
|
|
97
|
+
>
|
|
98
|
+
<Trash2 className="h-4 w-4" />
|
|
99
|
+
</Button>
|
|
100
|
+
)}
|
|
101
|
+
</div>
|
|
102
|
+
</div>
|
|
103
|
+
))}
|
|
104
|
+
|
|
105
|
+
{/* Add New Button */}
|
|
106
|
+
{onAddNew && (
|
|
107
|
+
<Button onClick={onAddNew} variant="outline" className="w-full">
|
|
108
|
+
<Plus className="h-4 w-4 mr-2" />
|
|
109
|
+
Add Payment Method
|
|
110
|
+
</Button>
|
|
111
|
+
)}
|
|
112
|
+
</div>
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export default PaymentMethodsList;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plan Comparison Component
|
|
3
|
+
*
|
|
4
|
+
* Configurable plan selection and comparison
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Check, Loader2 } from "lucide-react";
|
|
8
|
+
import { cn } from "@umituz/web-design-system/utils";
|
|
9
|
+
import { Button } from "@umituz/web-design-system/atoms";
|
|
10
|
+
import type { PlanComparisonProps } from "../types/billing";
|
|
11
|
+
import { formatPrice, getPlanPrice, calculateDiscount, isPopularPlan, formatFeature } from "../utils/billing";
|
|
12
|
+
|
|
13
|
+
export const PlanComparison = ({
|
|
14
|
+
plans,
|
|
15
|
+
selectedPlan,
|
|
16
|
+
cycle = "monthly",
|
|
17
|
+
showCycleToggle = true,
|
|
18
|
+
showFeatures = true,
|
|
19
|
+
onPlanSelect,
|
|
20
|
+
onCycleChange,
|
|
21
|
+
loading = false,
|
|
22
|
+
}: PlanComparisonProps) => {
|
|
23
|
+
const handleCycleChange = (newCycle: "monthly" | "yearly") => {
|
|
24
|
+
onCycleChange?.(newCycle);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const handleSelectPlan = (planId: string) => {
|
|
28
|
+
if (!loading) {
|
|
29
|
+
onPlanSelect?.(planId);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<div className="w-full space-y-8">
|
|
35
|
+
{/* Billing Cycle Toggle */}
|
|
36
|
+
{showCycleToggle && (
|
|
37
|
+
<div className="flex items-center justify-center">
|
|
38
|
+
<div className="inline-flex items-center bg-muted rounded-full p-1">
|
|
39
|
+
<button
|
|
40
|
+
onClick={() => handleCycleChange("monthly")}
|
|
41
|
+
className={cn(
|
|
42
|
+
"px-6 py-2 rounded-full text-sm font-medium transition-all",
|
|
43
|
+
cycle === "monthly"
|
|
44
|
+
? "bg-background text-foreground shadow-sm"
|
|
45
|
+
: "text-muted-foreground hover:text-foreground"
|
|
46
|
+
)}
|
|
47
|
+
>
|
|
48
|
+
Monthly
|
|
49
|
+
</button>
|
|
50
|
+
<button
|
|
51
|
+
onClick={() => handleCycleChange("yearly")}
|
|
52
|
+
className={cn(
|
|
53
|
+
"px-6 py-2 rounded-full text-sm font-medium transition-all relative",
|
|
54
|
+
cycle === "yearly"
|
|
55
|
+
? "bg-background text-foreground shadow-sm"
|
|
56
|
+
: "text-muted-foreground hover:text-foreground"
|
|
57
|
+
)}
|
|
58
|
+
>
|
|
59
|
+
Yearly
|
|
60
|
+
<span className="ml-1 text-xs bg-primary text-primary-foreground px-2 py-0.5 rounded-full">
|
|
61
|
+
Save 17%
|
|
62
|
+
</span>
|
|
63
|
+
</button>
|
|
64
|
+
</div>
|
|
65
|
+
</div>
|
|
66
|
+
)}
|
|
67
|
+
|
|
68
|
+
{/* Plans Grid */}
|
|
69
|
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
|
|
70
|
+
{plans.map((plan) => {
|
|
71
|
+
const isSelected = selectedPlan === plan.id;
|
|
72
|
+
const price = getPlanPrice(plan, cycle);
|
|
73
|
+
const isPopular = isPopularPlan(plan, plans);
|
|
74
|
+
const discount = calculateDiscount(plan.monthlyPrice, plan.yearlyPrice);
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<div
|
|
78
|
+
key={plan.id}
|
|
79
|
+
className={cn(
|
|
80
|
+
"relative bg-background border-2 rounded-2xl p-6 transition-all",
|
|
81
|
+
isSelected
|
|
82
|
+
? "border-primary shadow-lg shadow-primary/20"
|
|
83
|
+
: "border-border hover:border-primary/50",
|
|
84
|
+
isPopular && "border-primary shadow-lg shadow-primary/20"
|
|
85
|
+
)}
|
|
86
|
+
>
|
|
87
|
+
{/* Popular Badge */}
|
|
88
|
+
{isPopular && (
|
|
89
|
+
<div className="absolute -top-3 left-1/2 -translate-x-1/2">
|
|
90
|
+
<span className="bg-primary text-primary-foreground text-xs font-bold px-4 py-1 rounded-full">
|
|
91
|
+
Most Popular
|
|
92
|
+
</span>
|
|
93
|
+
</div>
|
|
94
|
+
)}
|
|
95
|
+
|
|
96
|
+
{/* Plan Badge */}
|
|
97
|
+
{plan.badge && (
|
|
98
|
+
<div className="mb-4">
|
|
99
|
+
<span
|
|
100
|
+
className={cn(
|
|
101
|
+
"text-xs font-bold uppercase tracking-wider px-3 py-1 rounded-full",
|
|
102
|
+
plan.badgeColor || "bg-primary/10 text-primary"
|
|
103
|
+
)}
|
|
104
|
+
>
|
|
105
|
+
{plan.badge}
|
|
106
|
+
</span>
|
|
107
|
+
</div>
|
|
108
|
+
)}
|
|
109
|
+
|
|
110
|
+
{/* Plan Name */}
|
|
111
|
+
<h3 className="text-2xl font-bold text-foreground mb-2">
|
|
112
|
+
{plan.name}
|
|
113
|
+
</h3>
|
|
114
|
+
|
|
115
|
+
{/* Description */}
|
|
116
|
+
<p className="text-muted-foreground text-sm mb-6">
|
|
117
|
+
{plan.description}
|
|
118
|
+
</p>
|
|
119
|
+
|
|
120
|
+
{/* Price */}
|
|
121
|
+
<div className="mb-6">
|
|
122
|
+
<div className="flex items-baseline gap-1">
|
|
123
|
+
<span className="text-4xl font-extrabold text-foreground">
|
|
124
|
+
{formatPrice(price, plan.currency)}
|
|
125
|
+
</span>
|
|
126
|
+
<span className="text-muted-foreground">
|
|
127
|
+
/{cycle === "monthly" ? "mo" : "yr"}
|
|
128
|
+
</span>
|
|
129
|
+
</div>
|
|
130
|
+
|
|
131
|
+
{cycle === "yearly" && (
|
|
132
|
+
<p className="text-sm text-green-600 dark:text-green-500 mt-1">
|
|
133
|
+
Save {discount}% with yearly billing
|
|
134
|
+
</p>
|
|
135
|
+
)}
|
|
136
|
+
</div>
|
|
137
|
+
|
|
138
|
+
{/* Features */}
|
|
139
|
+
{showFeatures && plan.features.length > 0 && (
|
|
140
|
+
<ul className="space-y-3 mb-6 flex-1">
|
|
141
|
+
{plan.features.map((feature, index) => {
|
|
142
|
+
const { text, bold, included } = formatFeature(feature);
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<li
|
|
146
|
+
key={index}
|
|
147
|
+
className={cn(
|
|
148
|
+
"flex items-start gap-3 text-sm",
|
|
149
|
+
included === false && "opacity-50 line-through"
|
|
150
|
+
)}
|
|
151
|
+
>
|
|
152
|
+
<Check
|
|
153
|
+
className={cn(
|
|
154
|
+
"h-5 w-5 shrink-0 mt-0.5",
|
|
155
|
+
included === false
|
|
156
|
+
? "text-muted-foreground"
|
|
157
|
+
: "text-primary"
|
|
158
|
+
)}
|
|
159
|
+
/>
|
|
160
|
+
<span className={cn(bold && "font-semibold", "text-foreground")}>
|
|
161
|
+
{text}
|
|
162
|
+
</span>
|
|
163
|
+
</li>
|
|
164
|
+
);
|
|
165
|
+
})}
|
|
166
|
+
</ul>
|
|
167
|
+
)}
|
|
168
|
+
|
|
169
|
+
{/* Select Button */}
|
|
170
|
+
<Button
|
|
171
|
+
onClick={() => handleSelectPlan(plan.id)}
|
|
172
|
+
disabled={loading}
|
|
173
|
+
className={cn(
|
|
174
|
+
"w-full rounded-full py-6",
|
|
175
|
+
isSelected || isPopular
|
|
176
|
+
? "bg-primary text-primary-foreground hover:bg-primary/90"
|
|
177
|
+
: "bg-muted text-foreground hover:bg-muted/80"
|
|
178
|
+
)}
|
|
179
|
+
>
|
|
180
|
+
{loading ? (
|
|
181
|
+
<>
|
|
182
|
+
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
|
183
|
+
Processing...
|
|
184
|
+
</>
|
|
185
|
+
) : isSelected ? (
|
|
186
|
+
"Current Plan"
|
|
187
|
+
) : (
|
|
188
|
+
"Select Plan"
|
|
189
|
+
)}
|
|
190
|
+
</Button>
|
|
191
|
+
</div>
|
|
192
|
+
);
|
|
193
|
+
})}
|
|
194
|
+
</div>
|
|
195
|
+
</div>
|
|
196
|
+
);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
export default PlanComparison;
|