@codaijs/keel 0.2.3 → 0.2.4
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/dist/__tests__/sail-installer.test.js +25 -25
- package/dist/sail-installer.js +174 -174
- package/dist/scaffold.js +68 -68
- package/package.json +58 -58
- package/sails/_template/addon.json +20 -20
- package/sails/_template/install.ts +402 -402
- package/sails/admin-dashboard/README.md +117 -117
- package/sails/admin-dashboard/addon.json +28 -28
- package/sails/admin-dashboard/files/backend/middleware/admin.ts +34 -34
- package/sails/admin-dashboard/files/backend/routes/admin.ts +243 -243
- package/sails/admin-dashboard/files/frontend/components/admin/StatsCard.tsx +40 -40
- package/sails/admin-dashboard/files/frontend/components/admin/UsersTable.tsx +240 -240
- package/sails/admin-dashboard/files/frontend/hooks/useAdmin.ts +149 -149
- package/sails/admin-dashboard/files/frontend/pages/admin/Dashboard.tsx +173 -173
- package/sails/admin-dashboard/files/frontend/pages/admin/UserDetail.tsx +203 -203
- package/sails/admin-dashboard/install.ts +305 -305
- package/sails/analytics/README.md +178 -178
- package/sails/analytics/addon.json +27 -27
- package/sails/analytics/files/frontend/components/AnalyticsProvider.tsx +58 -58
- package/sails/analytics/files/frontend/hooks/useAnalytics.ts +64 -64
- package/sails/analytics/files/frontend/lib/analytics.ts +103 -103
- package/sails/analytics/install.ts +297 -297
- package/sails/file-uploads/addon.json +30 -30
- package/sails/file-uploads/files/backend/routes/files.ts +198 -198
- package/sails/file-uploads/files/backend/schema/files.ts +36 -36
- package/sails/file-uploads/files/backend/services/file-storage.ts +128 -128
- package/sails/file-uploads/files/frontend/components/FileList.tsx +248 -248
- package/sails/file-uploads/files/frontend/components/FileUploadButton.tsx +147 -147
- package/sails/file-uploads/files/frontend/hooks/useFileUpload.ts +106 -106
- package/sails/file-uploads/files/frontend/hooks/useFiles.ts +118 -118
- package/sails/file-uploads/files/frontend/pages/Files.tsx +37 -37
- package/sails/file-uploads/install.ts +466 -466
- package/sails/gdpr/README.md +174 -174
- package/sails/gdpr/addon.json +27 -27
- package/sails/gdpr/files/backend/routes/gdpr.ts +140 -140
- package/sails/gdpr/files/backend/services/gdpr.ts +293 -293
- package/sails/gdpr/files/frontend/components/auth/ConsentCheckboxes.tsx +97 -97
- package/sails/gdpr/files/frontend/components/gdpr/AccountDeletionRequest.tsx +192 -192
- package/sails/gdpr/files/frontend/components/gdpr/DataExportButton.tsx +75 -75
- package/sails/gdpr/files/frontend/pages/PrivacyPolicy.tsx +186 -186
- package/sails/gdpr/install.ts +756 -756
- package/sails/google-oauth/README.md +121 -121
- package/sails/google-oauth/addon.json +22 -22
- package/sails/google-oauth/files/GoogleButton.tsx +50 -50
- package/sails/google-oauth/install.ts +252 -252
- package/sails/i18n/README.md +193 -193
- package/sails/i18n/addon.json +30 -30
- package/sails/i18n/files/frontend/components/LanguageSwitcher.tsx +108 -108
- package/sails/i18n/files/frontend/hooks/useLanguage.ts +31 -31
- package/sails/i18n/files/frontend/lib/i18n.ts +32 -32
- package/sails/i18n/files/frontend/locales/de/common.json +44 -44
- package/sails/i18n/files/frontend/locales/en/common.json +44 -44
- package/sails/i18n/install.ts +407 -407
- package/sails/push-notifications/README.md +163 -163
- package/sails/push-notifications/addon.json +31 -31
- package/sails/push-notifications/files/backend/routes/notifications.ts +153 -153
- package/sails/push-notifications/files/backend/schema/notifications.ts +31 -31
- package/sails/push-notifications/files/backend/services/notifications.ts +117 -117
- package/sails/push-notifications/files/frontend/components/PushNotificationInit.tsx +12 -12
- package/sails/push-notifications/files/frontend/hooks/usePushNotifications.ts +154 -154
- package/sails/push-notifications/install.ts +384 -384
- package/sails/r2-storage/addon.json +29 -29
- package/sails/r2-storage/files/backend/services/storage.ts +71 -71
- package/sails/r2-storage/files/frontend/components/ProfilePictureUpload.tsx +167 -167
- package/sails/r2-storage/install.ts +412 -412
- package/sails/rate-limiting/addon.json +20 -20
- package/sails/rate-limiting/files/backend/middleware/rate-limit-store.ts +104 -104
- package/sails/rate-limiting/files/backend/middleware/rate-limit.ts +137 -137
- package/sails/rate-limiting/install.ts +300 -300
- package/sails/registry.json +107 -107
- package/sails/stripe/README.md +214 -214
- package/sails/stripe/addon.json +24 -24
- package/sails/stripe/files/backend/routes/stripe.ts +154 -154
- package/sails/stripe/files/backend/schema/stripe.ts +74 -74
- package/sails/stripe/files/backend/services/stripe.ts +224 -224
- package/sails/stripe/files/frontend/components/SubscriptionStatus.tsx +135 -135
- package/sails/stripe/files/frontend/hooks/useSubscription.ts +86 -86
- package/sails/stripe/files/frontend/pages/Checkout.tsx +116 -116
- package/sails/stripe/files/frontend/pages/Pricing.tsx +226 -226
- package/sails/stripe/install.ts +378 -378
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
import { useSubscription } from "@/hooks/useSubscription";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Displays the current user's subscription status with options to manage,
|
|
5
|
-
* upgrade, or subscribe.
|
|
6
|
-
*/
|
|
7
|
-
export function SubscriptionStatus() {
|
|
8
|
-
const { subscription, isLoading, error } = useSubscription();
|
|
9
|
-
|
|
10
|
-
const handleManage = async () => {
|
|
11
|
-
try {
|
|
12
|
-
const response = await fetch("/api/stripe/create-portal-session", {
|
|
13
|
-
method: "POST",
|
|
14
|
-
headers: { "Content-Type": "application/json" },
|
|
15
|
-
credentials: "include",
|
|
16
|
-
});
|
|
17
|
-
const data = await response.json();
|
|
18
|
-
if (data.url) {
|
|
19
|
-
window.location.href = data.url;
|
|
20
|
-
}
|
|
21
|
-
} catch (err) {
|
|
22
|
-
console.error("Failed to open customer portal:", err);
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
if (isLoading) {
|
|
27
|
-
return (
|
|
28
|
-
<div className="animate-pulse rounded-lg border border-gray-200 p-6 dark:border-gray-700">
|
|
29
|
-
<div className="h-4 w-32 rounded bg-gray-200 dark:bg-gray-700" />
|
|
30
|
-
<div className="mt-3 h-3 w-48 rounded bg-gray-200 dark:bg-gray-700" />
|
|
31
|
-
</div>
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (error) {
|
|
36
|
-
return (
|
|
37
|
-
<div className="rounded-lg border border-red-200 bg-red-50 p-6 dark:border-red-800 dark:bg-red-900/20">
|
|
38
|
-
<p className="text-sm text-red-600 dark:text-red-400">
|
|
39
|
-
Failed to load subscription status.
|
|
40
|
-
</p>
|
|
41
|
-
</div>
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// No subscription — show upgrade prompt
|
|
46
|
-
if (!subscription) {
|
|
47
|
-
return (
|
|
48
|
-
<div className="rounded-lg border border-gray-200 p-6 dark:border-gray-700">
|
|
49
|
-
<div className="flex items-center justify-between">
|
|
50
|
-
<div>
|
|
51
|
-
<h3 className="text-sm font-semibold text-gray-900 dark:text-white">
|
|
52
|
-
Free Plan
|
|
53
|
-
</h3>
|
|
54
|
-
<p className="mt-1 text-sm text-gray-500 dark:text-gray-400">
|
|
55
|
-
Upgrade to unlock premium features.
|
|
56
|
-
</p>
|
|
57
|
-
</div>
|
|
58
|
-
<a
|
|
59
|
-
href="/pricing"
|
|
60
|
-
className="rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white hover:bg-blue-700"
|
|
61
|
-
>
|
|
62
|
-
Upgrade
|
|
63
|
-
</a>
|
|
64
|
-
</div>
|
|
65
|
-
</div>
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Status badge colors
|
|
70
|
-
const statusColors: Record<string, string> = {
|
|
71
|
-
active: "bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400",
|
|
72
|
-
trialing: "bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400",
|
|
73
|
-
past_due: "bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-400",
|
|
74
|
-
canceled: "bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400",
|
|
75
|
-
incomplete: "bg-gray-100 text-gray-800 dark:bg-gray-900/30 dark:text-gray-400",
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const statusLabel: Record<string, string> = {
|
|
79
|
-
active: "Active",
|
|
80
|
-
trialing: "Trial",
|
|
81
|
-
past_due: "Past Due",
|
|
82
|
-
canceled: "Canceled",
|
|
83
|
-
incomplete: "Incomplete",
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
const periodEnd = subscription.currentPeriodEnd
|
|
87
|
-
? new Date(subscription.currentPeriodEnd).toLocaleDateString(undefined, {
|
|
88
|
-
year: "numeric",
|
|
89
|
-
month: "long",
|
|
90
|
-
day: "numeric",
|
|
91
|
-
})
|
|
92
|
-
: null;
|
|
93
|
-
|
|
94
|
-
return (
|
|
95
|
-
<div className="rounded-lg border border-gray-200 p-6 dark:border-gray-700">
|
|
96
|
-
<div className="flex items-start justify-between">
|
|
97
|
-
<div>
|
|
98
|
-
<div className="flex items-center gap-2">
|
|
99
|
-
<h3 className="text-sm font-semibold text-gray-900 dark:text-white">
|
|
100
|
-
Subscription
|
|
101
|
-
</h3>
|
|
102
|
-
<span
|
|
103
|
-
className={`inline-flex rounded-full px-2 py-0.5 text-xs font-medium ${
|
|
104
|
-
statusColors[subscription.status] ?? statusColors.incomplete
|
|
105
|
-
}`}
|
|
106
|
-
>
|
|
107
|
-
{statusLabel[subscription.status] ?? subscription.status}
|
|
108
|
-
</span>
|
|
109
|
-
</div>
|
|
110
|
-
|
|
111
|
-
{subscription.cancelAtPeriodEnd && (
|
|
112
|
-
<p className="mt-1 text-sm text-yellow-600 dark:text-yellow-400">
|
|
113
|
-
Cancels at end of billing period
|
|
114
|
-
</p>
|
|
115
|
-
)}
|
|
116
|
-
|
|
117
|
-
{periodEnd && (
|
|
118
|
-
<p className="mt-1 text-sm text-gray-500 dark:text-gray-400">
|
|
119
|
-
{subscription.cancelAtPeriodEnd
|
|
120
|
-
? `Access until ${periodEnd}`
|
|
121
|
-
: `Renews on ${periodEnd}`}
|
|
122
|
-
</p>
|
|
123
|
-
)}
|
|
124
|
-
</div>
|
|
125
|
-
|
|
126
|
-
<button
|
|
127
|
-
onClick={handleManage}
|
|
128
|
-
className="rounded-lg border border-gray-300 px-4 py-2 text-sm font-semibold text-gray-700 hover:bg-gray-50 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-800"
|
|
129
|
-
>
|
|
130
|
-
Manage
|
|
131
|
-
</button>
|
|
132
|
-
</div>
|
|
133
|
-
</div>
|
|
134
|
-
);
|
|
135
|
-
}
|
|
1
|
+
import { useSubscription } from "@/hooks/useSubscription";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Displays the current user's subscription status with options to manage,
|
|
5
|
+
* upgrade, or subscribe.
|
|
6
|
+
*/
|
|
7
|
+
export function SubscriptionStatus() {
|
|
8
|
+
const { subscription, isLoading, error } = useSubscription();
|
|
9
|
+
|
|
10
|
+
const handleManage = async () => {
|
|
11
|
+
try {
|
|
12
|
+
const response = await fetch("/api/stripe/create-portal-session", {
|
|
13
|
+
method: "POST",
|
|
14
|
+
headers: { "Content-Type": "application/json" },
|
|
15
|
+
credentials: "include",
|
|
16
|
+
});
|
|
17
|
+
const data = await response.json();
|
|
18
|
+
if (data.url) {
|
|
19
|
+
window.location.href = data.url;
|
|
20
|
+
}
|
|
21
|
+
} catch (err) {
|
|
22
|
+
console.error("Failed to open customer portal:", err);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
if (isLoading) {
|
|
27
|
+
return (
|
|
28
|
+
<div className="animate-pulse rounded-lg border border-gray-200 p-6 dark:border-gray-700">
|
|
29
|
+
<div className="h-4 w-32 rounded bg-gray-200 dark:bg-gray-700" />
|
|
30
|
+
<div className="mt-3 h-3 w-48 rounded bg-gray-200 dark:bg-gray-700" />
|
|
31
|
+
</div>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (error) {
|
|
36
|
+
return (
|
|
37
|
+
<div className="rounded-lg border border-red-200 bg-red-50 p-6 dark:border-red-800 dark:bg-red-900/20">
|
|
38
|
+
<p className="text-sm text-red-600 dark:text-red-400">
|
|
39
|
+
Failed to load subscription status.
|
|
40
|
+
</p>
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// No subscription — show upgrade prompt
|
|
46
|
+
if (!subscription) {
|
|
47
|
+
return (
|
|
48
|
+
<div className="rounded-lg border border-gray-200 p-6 dark:border-gray-700">
|
|
49
|
+
<div className="flex items-center justify-between">
|
|
50
|
+
<div>
|
|
51
|
+
<h3 className="text-sm font-semibold text-gray-900 dark:text-white">
|
|
52
|
+
Free Plan
|
|
53
|
+
</h3>
|
|
54
|
+
<p className="mt-1 text-sm text-gray-500 dark:text-gray-400">
|
|
55
|
+
Upgrade to unlock premium features.
|
|
56
|
+
</p>
|
|
57
|
+
</div>
|
|
58
|
+
<a
|
|
59
|
+
href="/pricing"
|
|
60
|
+
className="rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white hover:bg-blue-700"
|
|
61
|
+
>
|
|
62
|
+
Upgrade
|
|
63
|
+
</a>
|
|
64
|
+
</div>
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Status badge colors
|
|
70
|
+
const statusColors: Record<string, string> = {
|
|
71
|
+
active: "bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400",
|
|
72
|
+
trialing: "bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400",
|
|
73
|
+
past_due: "bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-400",
|
|
74
|
+
canceled: "bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400",
|
|
75
|
+
incomplete: "bg-gray-100 text-gray-800 dark:bg-gray-900/30 dark:text-gray-400",
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const statusLabel: Record<string, string> = {
|
|
79
|
+
active: "Active",
|
|
80
|
+
trialing: "Trial",
|
|
81
|
+
past_due: "Past Due",
|
|
82
|
+
canceled: "Canceled",
|
|
83
|
+
incomplete: "Incomplete",
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const periodEnd = subscription.currentPeriodEnd
|
|
87
|
+
? new Date(subscription.currentPeriodEnd).toLocaleDateString(undefined, {
|
|
88
|
+
year: "numeric",
|
|
89
|
+
month: "long",
|
|
90
|
+
day: "numeric",
|
|
91
|
+
})
|
|
92
|
+
: null;
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<div className="rounded-lg border border-gray-200 p-6 dark:border-gray-700">
|
|
96
|
+
<div className="flex items-start justify-between">
|
|
97
|
+
<div>
|
|
98
|
+
<div className="flex items-center gap-2">
|
|
99
|
+
<h3 className="text-sm font-semibold text-gray-900 dark:text-white">
|
|
100
|
+
Subscription
|
|
101
|
+
</h3>
|
|
102
|
+
<span
|
|
103
|
+
className={`inline-flex rounded-full px-2 py-0.5 text-xs font-medium ${
|
|
104
|
+
statusColors[subscription.status] ?? statusColors.incomplete
|
|
105
|
+
}`}
|
|
106
|
+
>
|
|
107
|
+
{statusLabel[subscription.status] ?? subscription.status}
|
|
108
|
+
</span>
|
|
109
|
+
</div>
|
|
110
|
+
|
|
111
|
+
{subscription.cancelAtPeriodEnd && (
|
|
112
|
+
<p className="mt-1 text-sm text-yellow-600 dark:text-yellow-400">
|
|
113
|
+
Cancels at end of billing period
|
|
114
|
+
</p>
|
|
115
|
+
)}
|
|
116
|
+
|
|
117
|
+
{periodEnd && (
|
|
118
|
+
<p className="mt-1 text-sm text-gray-500 dark:text-gray-400">
|
|
119
|
+
{subscription.cancelAtPeriodEnd
|
|
120
|
+
? `Access until ${periodEnd}`
|
|
121
|
+
: `Renews on ${periodEnd}`}
|
|
122
|
+
</p>
|
|
123
|
+
)}
|
|
124
|
+
</div>
|
|
125
|
+
|
|
126
|
+
<button
|
|
127
|
+
onClick={handleManage}
|
|
128
|
+
className="rounded-lg border border-gray-300 px-4 py-2 text-sm font-semibold text-gray-700 hover:bg-gray-50 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-800"
|
|
129
|
+
>
|
|
130
|
+
Manage
|
|
131
|
+
</button>
|
|
132
|
+
</div>
|
|
133
|
+
</div>
|
|
134
|
+
);
|
|
135
|
+
}
|
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
import { useState, useEffect, useCallback } from "react";
|
|
2
|
-
|
|
3
|
-
interface Subscription {
|
|
4
|
-
id: string;
|
|
5
|
-
status: string;
|
|
6
|
-
priceId: string;
|
|
7
|
-
currentPeriodEnd: string | null;
|
|
8
|
-
cancelAtPeriodEnd: boolean;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
interface UseSubscriptionResult {
|
|
12
|
-
subscription: Subscription | null;
|
|
13
|
-
isLoading: boolean;
|
|
14
|
-
error: Error | null;
|
|
15
|
-
refetch: () => Promise<void>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Hook to fetch and manage the current user's subscription state.
|
|
20
|
-
*
|
|
21
|
-
* Automatically fetches on mount. Call `refetch()` to manually refresh
|
|
22
|
-
* (e.g., after returning from Stripe Checkout or the customer portal).
|
|
23
|
-
*/
|
|
24
|
-
export function useSubscription(): UseSubscriptionResult {
|
|
25
|
-
const [subscription, setSubscription] = useState<Subscription | null>(null);
|
|
26
|
-
const [isLoading, setIsLoading] = useState(true);
|
|
27
|
-
const [error, setError] = useState<Error | null>(null);
|
|
28
|
-
|
|
29
|
-
const fetchSubscription = useCallback(async () => {
|
|
30
|
-
setIsLoading(true);
|
|
31
|
-
setError(null);
|
|
32
|
-
|
|
33
|
-
try {
|
|
34
|
-
const response = await fetch("/api/stripe/subscription", {
|
|
35
|
-
credentials: "include",
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
if (!response.ok) {
|
|
39
|
-
throw new Error(`Failed to fetch subscription: ${response.statusText}`);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const data = await response.json();
|
|
43
|
-
setSubscription(data.subscription ?? null);
|
|
44
|
-
} catch (err) {
|
|
45
|
-
setError(
|
|
46
|
-
err instanceof Error ? err : new Error("Unknown error fetching subscription")
|
|
47
|
-
);
|
|
48
|
-
setSubscription(null);
|
|
49
|
-
} finally {
|
|
50
|
-
setIsLoading(false);
|
|
51
|
-
}
|
|
52
|
-
}, []);
|
|
53
|
-
|
|
54
|
-
useEffect(() => {
|
|
55
|
-
fetchSubscription();
|
|
56
|
-
}, [fetchSubscription]);
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
subscription,
|
|
60
|
-
isLoading,
|
|
61
|
-
error,
|
|
62
|
-
refetch: fetchSubscription,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Helper to check if a subscription is in an active/usable state.
|
|
68
|
-
*/
|
|
69
|
-
export function isSubscriptionActive(
|
|
70
|
-
subscription: Subscription | null
|
|
71
|
-
): boolean {
|
|
72
|
-
if (!subscription) return false;
|
|
73
|
-
return ["active", "trialing"].includes(subscription.status);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Helper to check if the user has a specific plan by price ID.
|
|
78
|
-
*/
|
|
79
|
-
export function hasPlans(
|
|
80
|
-
subscription: Subscription | null,
|
|
81
|
-
priceIds: string[]
|
|
82
|
-
): boolean {
|
|
83
|
-
if (!subscription) return false;
|
|
84
|
-
if (!isSubscriptionActive(subscription)) return false;
|
|
85
|
-
return priceIds.includes(subscription.priceId);
|
|
86
|
-
}
|
|
1
|
+
import { useState, useEffect, useCallback } from "react";
|
|
2
|
+
|
|
3
|
+
interface Subscription {
|
|
4
|
+
id: string;
|
|
5
|
+
status: string;
|
|
6
|
+
priceId: string;
|
|
7
|
+
currentPeriodEnd: string | null;
|
|
8
|
+
cancelAtPeriodEnd: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface UseSubscriptionResult {
|
|
12
|
+
subscription: Subscription | null;
|
|
13
|
+
isLoading: boolean;
|
|
14
|
+
error: Error | null;
|
|
15
|
+
refetch: () => Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Hook to fetch and manage the current user's subscription state.
|
|
20
|
+
*
|
|
21
|
+
* Automatically fetches on mount. Call `refetch()` to manually refresh
|
|
22
|
+
* (e.g., after returning from Stripe Checkout or the customer portal).
|
|
23
|
+
*/
|
|
24
|
+
export function useSubscription(): UseSubscriptionResult {
|
|
25
|
+
const [subscription, setSubscription] = useState<Subscription | null>(null);
|
|
26
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
27
|
+
const [error, setError] = useState<Error | null>(null);
|
|
28
|
+
|
|
29
|
+
const fetchSubscription = useCallback(async () => {
|
|
30
|
+
setIsLoading(true);
|
|
31
|
+
setError(null);
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
const response = await fetch("/api/stripe/subscription", {
|
|
35
|
+
credentials: "include",
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (!response.ok) {
|
|
39
|
+
throw new Error(`Failed to fetch subscription: ${response.statusText}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const data = await response.json();
|
|
43
|
+
setSubscription(data.subscription ?? null);
|
|
44
|
+
} catch (err) {
|
|
45
|
+
setError(
|
|
46
|
+
err instanceof Error ? err : new Error("Unknown error fetching subscription")
|
|
47
|
+
);
|
|
48
|
+
setSubscription(null);
|
|
49
|
+
} finally {
|
|
50
|
+
setIsLoading(false);
|
|
51
|
+
}
|
|
52
|
+
}, []);
|
|
53
|
+
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
fetchSubscription();
|
|
56
|
+
}, [fetchSubscription]);
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
subscription,
|
|
60
|
+
isLoading,
|
|
61
|
+
error,
|
|
62
|
+
refetch: fetchSubscription,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Helper to check if a subscription is in an active/usable state.
|
|
68
|
+
*/
|
|
69
|
+
export function isSubscriptionActive(
|
|
70
|
+
subscription: Subscription | null
|
|
71
|
+
): boolean {
|
|
72
|
+
if (!subscription) return false;
|
|
73
|
+
return ["active", "trialing"].includes(subscription.status);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Helper to check if the user has a specific plan by price ID.
|
|
78
|
+
*/
|
|
79
|
+
export function hasPlans(
|
|
80
|
+
subscription: Subscription | null,
|
|
81
|
+
priceIds: string[]
|
|
82
|
+
): boolean {
|
|
83
|
+
if (!subscription) return false;
|
|
84
|
+
if (!isSubscriptionActive(subscription)) return false;
|
|
85
|
+
return priceIds.includes(subscription.priceId);
|
|
86
|
+
}
|