create-githat-app 1.8.6 → 1.8.8
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/cli.js +2 -2
- package/package.json +1 -1
- package/templates/agent/app/account/files/page.tsx.hbs +241 -0
- package/templates/agent/app/billing/page.tsx.hbs +158 -0
- package/templates/base/README.md.hbs +119 -0
- package/templates/base/githat/billing/page.tsx.hbs +158 -0
- package/templates/classroom/app/account/files/page.tsx.hbs +241 -0
- package/templates/classroom/app/billing/page.tsx.hbs +158 -0
- package/templates/content/app/account/files/page.tsx.hbs +241 -0
- package/templates/content/app/billing/page.tsx.hbs +158 -0
- package/templates/dashboard/app/account/files/page.tsx.hbs +241 -0
- package/templates/dashboard/app/billing/page.tsx.hbs +158 -0
- package/templates/marketplace/app/account/files/page.tsx.hbs +241 -0
- package/templates/marketplace/app/billing/page.tsx.hbs +158 -0
- package/templates/nextjs/app/account/files/page.tsx.hbs +241 -0
- package/templates/nextjs/app/billing/page.tsx.hbs +158 -0
- package/templates/plain/app/account/files/page.tsx.hbs +241 -0
- package/templates/portfolio/app/account/files/page.tsx.hbs +241 -0
- package/templates/portfolio/app/billing/page.tsx.hbs +158 -0
- package/templates/saas/app/account/files/page.tsx.hbs +241 -0
- package/templates/saas/app/billing/page.tsx.hbs +158 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState, useCallback } from 'react';
|
|
4
|
+
import { useAuth, useAppBilling } from '@githat/nextjs';
|
|
5
|
+
import type { AppBillingStatus } from '@githat/nextjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* /billing — Subscription management for {{businessName}} end-users.
|
|
9
|
+
*
|
|
10
|
+
* Lets your users manage their subscription on your app. Backed by
|
|
11
|
+
* Stripe Connect — charges go to your connected Stripe account and
|
|
12
|
+
* GitHat takes a small platform fee.
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* 1. Complete Stripe Connect onboarding in the GitHat dashboard
|
|
16
|
+
* (Apps → your app → Billing → Connect Stripe).
|
|
17
|
+
* 2. This page shows users a "Manage subscription" button that
|
|
18
|
+
* opens the Stripe Customer Portal in their name.
|
|
19
|
+
*
|
|
20
|
+
* For new subscriptions, call createCheckout() from useAppBilling()
|
|
21
|
+
* with the relevant Stripe price IDs for your plans.
|
|
22
|
+
*/
|
|
23
|
+
export default function BillingPage() {
|
|
24
|
+
const { user, org } = useAuth();
|
|
25
|
+
const appId = process.env.NEXT_PUBLIC_GITHAT_APP_ID || '';
|
|
26
|
+
const orgId = org?.id || '';
|
|
27
|
+
|
|
28
|
+
const { getStatus, connectOnboarding, openPortal } = useAppBilling(appId, orgId);
|
|
29
|
+
|
|
30
|
+
const [status, setStatus] = useState<AppBillingStatus | null>(null);
|
|
31
|
+
const [loading, setLoading] = useState(true);
|
|
32
|
+
const [error, setError] = useState<string | null>(null);
|
|
33
|
+
const [portalLoading, setPortalLoading] = useState(false);
|
|
34
|
+
|
|
35
|
+
const load = useCallback(async () => {
|
|
36
|
+
if (!appId || !orgId) return;
|
|
37
|
+
setLoading(true);
|
|
38
|
+
setError(null);
|
|
39
|
+
try {
|
|
40
|
+
const data = await getStatus();
|
|
41
|
+
setStatus(data);
|
|
42
|
+
} catch (err: unknown) {
|
|
43
|
+
setError(err instanceof Error ? err.message : 'Failed to load billing info');
|
|
44
|
+
} finally {
|
|
45
|
+
setLoading(false);
|
|
46
|
+
}
|
|
47
|
+
}, [getStatus, appId, orgId]);
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (user) load();
|
|
51
|
+
}, [user, load]);
|
|
52
|
+
|
|
53
|
+
async function handlePortal() {
|
|
54
|
+
if (!user) return;
|
|
55
|
+
setPortalLoading(true);
|
|
56
|
+
setError(null);
|
|
57
|
+
try {
|
|
58
|
+
// In a real app, pass the Stripe customer ID for this user.
|
|
59
|
+
// You would look this up from your database or pass it as a prop.
|
|
60
|
+
const customerId = (user as Record<string, unknown>).stripeCustomerId as string | undefined;
|
|
61
|
+
if (!customerId) {
|
|
62
|
+
setError('No billing account found. Subscribe to a plan first.');
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const { url } = await openPortal({ customerId });
|
|
66
|
+
window.location.href = url;
|
|
67
|
+
} catch (err: unknown) {
|
|
68
|
+
setError(err instanceof Error ? err.message : 'Failed to open billing portal');
|
|
69
|
+
} finally {
|
|
70
|
+
setPortalLoading(false);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!user) {
|
|
75
|
+
return (
|
|
76
|
+
<div style={{ padding: '2rem', textAlign: 'center' }}>
|
|
77
|
+
<p>Sign in to manage your subscription.</p>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<div style={{ maxWidth: '640px', margin: '0 auto', padding: '2rem 1rem' }}>
|
|
84
|
+
<h1 style={{ fontSize: '1.5rem', fontWeight: 700, marginBottom: '0.5rem' }}>
|
|
85
|
+
Billing
|
|
86
|
+
</h1>
|
|
87
|
+
<p style={{ color: '#64748b', marginBottom: '2rem' }}>
|
|
88
|
+
Manage your {{businessName}} subscription.
|
|
89
|
+
</p>
|
|
90
|
+
|
|
91
|
+
{loading && <p style={{ color: '#64748b' }}>Loading…</p>}
|
|
92
|
+
|
|
93
|
+
{error && (
|
|
94
|
+
<div
|
|
95
|
+
style={{
|
|
96
|
+
background: 'rgba(239,68,68,0.1)',
|
|
97
|
+
border: '1px solid #ef4444',
|
|
98
|
+
borderRadius: '0.5rem',
|
|
99
|
+
padding: '0.75rem 1rem',
|
|
100
|
+
color: '#ef4444',
|
|
101
|
+
marginBottom: '1rem',
|
|
102
|
+
}}
|
|
103
|
+
>
|
|
104
|
+
{error}
|
|
105
|
+
</div>
|
|
106
|
+
)}
|
|
107
|
+
|
|
108
|
+
{!loading && status && (
|
|
109
|
+
<div
|
|
110
|
+
style={{
|
|
111
|
+
border: '1px solid #e2e8f0',
|
|
112
|
+
borderRadius: '0.75rem',
|
|
113
|
+
padding: '1.5rem',
|
|
114
|
+
background: '#f8fafc',
|
|
115
|
+
}}
|
|
116
|
+
>
|
|
117
|
+
<div style={{ marginBottom: '1rem' }}>
|
|
118
|
+
<div style={{ fontSize: '0.75rem', textTransform: 'uppercase', letterSpacing: '0.05em', color: '#64748b', marginBottom: '0.25rem' }}>
|
|
119
|
+
Connect status
|
|
120
|
+
</div>
|
|
121
|
+
<div style={{ fontWeight: 600 }}>
|
|
122
|
+
{status.connected ? (
|
|
123
|
+
status.chargesEnabled ? '✅ Active — charges enabled' : '⏳ Onboarding in progress'
|
|
124
|
+
) : '❌ Not connected'}
|
|
125
|
+
</div>
|
|
126
|
+
</div>
|
|
127
|
+
|
|
128
|
+
{status.connected && status.chargesEnabled && (
|
|
129
|
+
<button
|
|
130
|
+
onClick={handlePortal}
|
|
131
|
+
disabled={portalLoading}
|
|
132
|
+
style={{
|
|
133
|
+
background: '#7c3aed',
|
|
134
|
+
color: '#fff',
|
|
135
|
+
border: 'none',
|
|
136
|
+
borderRadius: '0.5rem',
|
|
137
|
+
padding: '0.6rem 1.25rem',
|
|
138
|
+
fontWeight: 600,
|
|
139
|
+
cursor: portalLoading ? 'default' : 'pointer',
|
|
140
|
+
opacity: portalLoading ? 0.7 : 1,
|
|
141
|
+
fontSize: '0.875rem',
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
{portalLoading ? 'Opening portal…' : 'Manage subscription →'}
|
|
145
|
+
</button>
|
|
146
|
+
)}
|
|
147
|
+
|
|
148
|
+
{!status.connected && (
|
|
149
|
+
<p style={{ fontSize: '0.875rem', color: '#64748b', marginTop: '0.5rem' }}>
|
|
150
|
+
The app owner needs to complete Stripe Connect onboarding before subscriptions
|
|
151
|
+
can be managed here.
|
|
152
|
+
</p>
|
|
153
|
+
)}
|
|
154
|
+
</div>
|
|
155
|
+
)}
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState, useRef } from 'react';
|
|
4
|
+
import { useStorage, StorageDropzone } from '@githat/nextjs';
|
|
5
|
+
import type { StorageObject } from '@githat/nextjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* /account/files — File storage for {{businessName}}.
|
|
9
|
+
*
|
|
10
|
+
* Lists uploaded files with download links. Uses <StorageDropzone/>
|
|
11
|
+
* for browser-direct uploads to S3 via the GitHat storage API.
|
|
12
|
+
* Works with Next.js static export — no server actions required.
|
|
13
|
+
*/
|
|
14
|
+
export default function FilesPage() {
|
|
15
|
+
const { list, getUrl, remove } = useStorage();
|
|
16
|
+
const [files, setFiles] = useState<StorageObject[]>([]);
|
|
17
|
+
const [nextCursor, setNextCursor] = useState<string | null>(null);
|
|
18
|
+
const [loading, setLoading] = useState(true);
|
|
19
|
+
const [error, setError] = useState<string | null>(null);
|
|
20
|
+
const [deletingId, setDeletingId] = useState<string | null>(null);
|
|
21
|
+
|
|
22
|
+
async function load(cursor?: string) {
|
|
23
|
+
setLoading(true);
|
|
24
|
+
setError(null);
|
|
25
|
+
try {
|
|
26
|
+
const result = await list({ limit: 20, cursor });
|
|
27
|
+
if (cursor) {
|
|
28
|
+
setFiles((prev) => [...prev, ...result.items]);
|
|
29
|
+
} else {
|
|
30
|
+
setFiles(result.items);
|
|
31
|
+
}
|
|
32
|
+
setNextCursor(result.nextCursor);
|
|
33
|
+
} catch (err: unknown) {
|
|
34
|
+
setError(err instanceof Error ? err.message : 'Failed to load files');
|
|
35
|
+
} finally {
|
|
36
|
+
setLoading(false);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
useEffect(() => { load(); }, []);
|
|
41
|
+
|
|
42
|
+
async function handleDownload(objectId: string) {
|
|
43
|
+
try {
|
|
44
|
+
const obj = await getUrl(objectId);
|
|
45
|
+
if (obj.downloadUrl) {
|
|
46
|
+
window.open(obj.downloadUrl, '_blank', 'noopener,noreferrer');
|
|
47
|
+
}
|
|
48
|
+
} catch (err: unknown) {
|
|
49
|
+
setError(err instanceof Error ? err.message : 'Failed to get download URL');
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function handleDelete(objectId: string) {
|
|
54
|
+
if (!confirm('Delete this file? This cannot be undone.')) return;
|
|
55
|
+
setDeletingId(objectId);
|
|
56
|
+
try {
|
|
57
|
+
await remove(objectId);
|
|
58
|
+
setFiles((prev) => prev.filter((f) => f.objectId !== objectId));
|
|
59
|
+
} catch (err: unknown) {
|
|
60
|
+
setError(err instanceof Error ? err.message : 'Failed to delete file');
|
|
61
|
+
} finally {
|
|
62
|
+
setDeletingId(null);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function formatBytes(bytes: number): string {
|
|
67
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
68
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
69
|
+
return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return (
|
|
73
|
+
<main
|
|
74
|
+
style=\{{
|
|
75
|
+
maxWidth: '720px',
|
|
76
|
+
margin: '0 auto',
|
|
77
|
+
padding: 'var(--space-8, 2rem) var(--space-4, 1rem)',
|
|
78
|
+
color: 'var(--fg, #111)',
|
|
79
|
+
}}
|
|
80
|
+
>
|
|
81
|
+
<header style=\{{ marginBottom: '1.5rem' }}>
|
|
82
|
+
<h1 style=\{{ fontSize: '1.875rem', fontWeight: 700, margin: 0 }}>Files</h1>
|
|
83
|
+
<p style=\{{ color: 'var(--fg-muted, #666)', marginTop: '0.25rem' }}>
|
|
84
|
+
Upload and manage files in your {{businessName}} account.
|
|
85
|
+
</p>
|
|
86
|
+
</header>
|
|
87
|
+
|
|
88
|
+
{/* Upload dropzone */}
|
|
89
|
+
<StorageDropzone
|
|
90
|
+
accept="image/*,application/pdf,text/*"
|
|
91
|
+
onUpload={() => load()}
|
|
92
|
+
onError={(err) => setError(err.message)}
|
|
93
|
+
uploadOptions=\{{ isPublic: false }}
|
|
94
|
+
style=\{{ marginBottom: '1.5rem' }}
|
|
95
|
+
/>
|
|
96
|
+
|
|
97
|
+
{error && (
|
|
98
|
+
<div
|
|
99
|
+
style=\{{
|
|
100
|
+
padding: '0.75rem 1rem',
|
|
101
|
+
background: '#fef2f2',
|
|
102
|
+
border: '1px solid #fecaca',
|
|
103
|
+
borderRadius: '6px',
|
|
104
|
+
color: '#dc2626',
|
|
105
|
+
marginBottom: '1rem',
|
|
106
|
+
}}
|
|
107
|
+
>
|
|
108
|
+
{error}
|
|
109
|
+
</div>
|
|
110
|
+
)}
|
|
111
|
+
|
|
112
|
+
{loading && files.length === 0 ? (
|
|
113
|
+
<p style=\{{ color: 'var(--fg-muted, #666)' }}>Loading files...</p>
|
|
114
|
+
) : files.length === 0 ? (
|
|
115
|
+
<p style=\{{ color: 'var(--fg-muted, #666)', textAlign: 'center', padding: '2rem 0' }}>
|
|
116
|
+
No files uploaded yet. Drop a file above to get started.
|
|
117
|
+
</p>
|
|
118
|
+
) : (
|
|
119
|
+
<div style=\{{ display: 'flex', flexDirection: 'column', gap: '0.625rem' }}>
|
|
120
|
+
{files.map((file) => (
|
|
121
|
+
<div
|
|
122
|
+
key={file.objectId}
|
|
123
|
+
style=\{{
|
|
124
|
+
display: 'flex',
|
|
125
|
+
alignItems: 'center',
|
|
126
|
+
gap: '0.75rem',
|
|
127
|
+
padding: '0.875rem 1rem',
|
|
128
|
+
border: '1px solid var(--border, #e5e7eb)',
|
|
129
|
+
borderRadius: '8px',
|
|
130
|
+
}}
|
|
131
|
+
>
|
|
132
|
+
{/* File icon */}
|
|
133
|
+
<svg
|
|
134
|
+
width="20"
|
|
135
|
+
height="20"
|
|
136
|
+
viewBox="0 0 24 24"
|
|
137
|
+
fill="none"
|
|
138
|
+
stroke="#9ca3af"
|
|
139
|
+
strokeWidth="1.5"
|
|
140
|
+
strokeLinecap="round"
|
|
141
|
+
strokeLinejoin="round"
|
|
142
|
+
style=\{{ flexShrink: 0 }}
|
|
143
|
+
aria-hidden
|
|
144
|
+
>
|
|
145
|
+
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
|
|
146
|
+
<polyline points="14 2 14 8 20 8" />
|
|
147
|
+
</svg>
|
|
148
|
+
|
|
149
|
+
{/* File info */}
|
|
150
|
+
<div style=\{{ flex: 1, minWidth: 0 }}>
|
|
151
|
+
<p
|
|
152
|
+
style=\{{
|
|
153
|
+
margin: 0,
|
|
154
|
+
fontWeight: 500,
|
|
155
|
+
fontSize: '0.9rem',
|
|
156
|
+
overflow: 'hidden',
|
|
157
|
+
textOverflow: 'ellipsis',
|
|
158
|
+
whiteSpace: 'nowrap',
|
|
159
|
+
}}
|
|
160
|
+
>
|
|
161
|
+
{file.objectKey}
|
|
162
|
+
</p>
|
|
163
|
+
<p style=\{{ margin: '0.125rem 0 0', fontSize: '0.78rem', color: 'var(--fg-muted, #666)' }}>
|
|
164
|
+
{formatBytes(file.size)} · {file.contentType} ·{' '}
|
|
165
|
+
{new Date(file.uploadedAt).toLocaleDateString()}
|
|
166
|
+
{file.isPublic && (
|
|
167
|
+
<span
|
|
168
|
+
style=\{{
|
|
169
|
+
marginLeft: '0.5rem',
|
|
170
|
+
padding: '0.1rem 0.4rem',
|
|
171
|
+
background: '#dcfce7',
|
|
172
|
+
color: '#16a34a',
|
|
173
|
+
borderRadius: '4px',
|
|
174
|
+
fontSize: '0.7rem',
|
|
175
|
+
fontWeight: 600,
|
|
176
|
+
}}
|
|
177
|
+
>
|
|
178
|
+
Public
|
|
179
|
+
</span>
|
|
180
|
+
)}
|
|
181
|
+
</p>
|
|
182
|
+
</div>
|
|
183
|
+
|
|
184
|
+
{/* Actions */}
|
|
185
|
+
<div style=\{{ display: 'flex', gap: '0.5rem', flexShrink: 0 }}>
|
|
186
|
+
<button
|
|
187
|
+
onClick={() => handleDownload(file.objectId)}
|
|
188
|
+
style=\{{
|
|
189
|
+
padding: '0.35rem 0.75rem',
|
|
190
|
+
fontSize: '0.825rem',
|
|
191
|
+
border: '1px solid var(--border, #e5e7eb)',
|
|
192
|
+
borderRadius: '6px',
|
|
193
|
+
background: 'transparent',
|
|
194
|
+
cursor: 'pointer',
|
|
195
|
+
}}
|
|
196
|
+
aria-label={`Download ${file.objectKey}`}
|
|
197
|
+
>
|
|
198
|
+
Download
|
|
199
|
+
</button>
|
|
200
|
+
<button
|
|
201
|
+
onClick={() => handleDelete(file.objectId)}
|
|
202
|
+
disabled={deletingId === file.objectId}
|
|
203
|
+
style=\{{
|
|
204
|
+
padding: '0.35rem 0.75rem',
|
|
205
|
+
fontSize: '0.825rem',
|
|
206
|
+
border: '1px solid #fecaca',
|
|
207
|
+
borderRadius: '6px',
|
|
208
|
+
background: 'transparent',
|
|
209
|
+
color: '#dc2626',
|
|
210
|
+
cursor: deletingId === file.objectId ? 'not-allowed' : 'pointer',
|
|
211
|
+
}}
|
|
212
|
+
aria-label={`Delete ${file.objectKey}`}
|
|
213
|
+
>
|
|
214
|
+
{deletingId === file.objectId ? '...' : 'Delete'}
|
|
215
|
+
</button>
|
|
216
|
+
</div>
|
|
217
|
+
</div>
|
|
218
|
+
))}
|
|
219
|
+
|
|
220
|
+
{nextCursor && (
|
|
221
|
+
<button
|
|
222
|
+
onClick={() => load(nextCursor)}
|
|
223
|
+
disabled={loading}
|
|
224
|
+
style=\{{
|
|
225
|
+
padding: '0.6rem 1.25rem',
|
|
226
|
+
marginTop: '0.5rem',
|
|
227
|
+
border: '1px solid var(--border, #e5e7eb)',
|
|
228
|
+
borderRadius: '6px',
|
|
229
|
+
background: 'transparent',
|
|
230
|
+
cursor: loading ? 'not-allowed' : 'pointer',
|
|
231
|
+
fontSize: '0.875rem',
|
|
232
|
+
}}
|
|
233
|
+
>
|
|
234
|
+
{loading ? 'Loading...' : 'Load more'}
|
|
235
|
+
</button>
|
|
236
|
+
)}
|
|
237
|
+
</div>
|
|
238
|
+
)}
|
|
239
|
+
</main>
|
|
240
|
+
);
|
|
241
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState, useCallback } from 'react';
|
|
4
|
+
import { useAuth, useAppBilling } from '@githat/nextjs';
|
|
5
|
+
import type { AppBillingStatus } from '@githat/nextjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* /billing — Subscription management for {{businessName}} end-users.
|
|
9
|
+
*
|
|
10
|
+
* Lets your users manage their subscription on your app. Backed by
|
|
11
|
+
* Stripe Connect — charges go to your connected Stripe account and
|
|
12
|
+
* GitHat takes a small platform fee.
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* 1. Complete Stripe Connect onboarding in the GitHat dashboard
|
|
16
|
+
* (Apps → your app → Billing → Connect Stripe).
|
|
17
|
+
* 2. This page shows users a "Manage subscription" button that
|
|
18
|
+
* opens the Stripe Customer Portal in their name.
|
|
19
|
+
*
|
|
20
|
+
* For new subscriptions, call createCheckout() from useAppBilling()
|
|
21
|
+
* with the relevant Stripe price IDs for your plans.
|
|
22
|
+
*/
|
|
23
|
+
export default function BillingPage() {
|
|
24
|
+
const { user, org } = useAuth();
|
|
25
|
+
const appId = process.env.NEXT_PUBLIC_GITHAT_APP_ID || '';
|
|
26
|
+
const orgId = org?.id || '';
|
|
27
|
+
|
|
28
|
+
const { getStatus, connectOnboarding, openPortal } = useAppBilling(appId, orgId);
|
|
29
|
+
|
|
30
|
+
const [status, setStatus] = useState<AppBillingStatus | null>(null);
|
|
31
|
+
const [loading, setLoading] = useState(true);
|
|
32
|
+
const [error, setError] = useState<string | null>(null);
|
|
33
|
+
const [portalLoading, setPortalLoading] = useState(false);
|
|
34
|
+
|
|
35
|
+
const load = useCallback(async () => {
|
|
36
|
+
if (!appId || !orgId) return;
|
|
37
|
+
setLoading(true);
|
|
38
|
+
setError(null);
|
|
39
|
+
try {
|
|
40
|
+
const data = await getStatus();
|
|
41
|
+
setStatus(data);
|
|
42
|
+
} catch (err: unknown) {
|
|
43
|
+
setError(err instanceof Error ? err.message : 'Failed to load billing info');
|
|
44
|
+
} finally {
|
|
45
|
+
setLoading(false);
|
|
46
|
+
}
|
|
47
|
+
}, [getStatus, appId, orgId]);
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (user) load();
|
|
51
|
+
}, [user, load]);
|
|
52
|
+
|
|
53
|
+
async function handlePortal() {
|
|
54
|
+
if (!user) return;
|
|
55
|
+
setPortalLoading(true);
|
|
56
|
+
setError(null);
|
|
57
|
+
try {
|
|
58
|
+
// In a real app, pass the Stripe customer ID for this user.
|
|
59
|
+
// You would look this up from your database or pass it as a prop.
|
|
60
|
+
const customerId = (user as Record<string, unknown>).stripeCustomerId as string | undefined;
|
|
61
|
+
if (!customerId) {
|
|
62
|
+
setError('No billing account found. Subscribe to a plan first.');
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const { url } = await openPortal({ customerId });
|
|
66
|
+
window.location.href = url;
|
|
67
|
+
} catch (err: unknown) {
|
|
68
|
+
setError(err instanceof Error ? err.message : 'Failed to open billing portal');
|
|
69
|
+
} finally {
|
|
70
|
+
setPortalLoading(false);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!user) {
|
|
75
|
+
return (
|
|
76
|
+
<div style={{ padding: '2rem', textAlign: 'center' }}>
|
|
77
|
+
<p>Sign in to manage your subscription.</p>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<div style={{ maxWidth: '640px', margin: '0 auto', padding: '2rem 1rem' }}>
|
|
84
|
+
<h1 style={{ fontSize: '1.5rem', fontWeight: 700, marginBottom: '0.5rem' }}>
|
|
85
|
+
Billing
|
|
86
|
+
</h1>
|
|
87
|
+
<p style={{ color: '#64748b', marginBottom: '2rem' }}>
|
|
88
|
+
Manage your {{businessName}} subscription.
|
|
89
|
+
</p>
|
|
90
|
+
|
|
91
|
+
{loading && <p style={{ color: '#64748b' }}>Loading…</p>}
|
|
92
|
+
|
|
93
|
+
{error && (
|
|
94
|
+
<div
|
|
95
|
+
style={{
|
|
96
|
+
background: 'rgba(239,68,68,0.1)',
|
|
97
|
+
border: '1px solid #ef4444',
|
|
98
|
+
borderRadius: '0.5rem',
|
|
99
|
+
padding: '0.75rem 1rem',
|
|
100
|
+
color: '#ef4444',
|
|
101
|
+
marginBottom: '1rem',
|
|
102
|
+
}}
|
|
103
|
+
>
|
|
104
|
+
{error}
|
|
105
|
+
</div>
|
|
106
|
+
)}
|
|
107
|
+
|
|
108
|
+
{!loading && status && (
|
|
109
|
+
<div
|
|
110
|
+
style={{
|
|
111
|
+
border: '1px solid #e2e8f0',
|
|
112
|
+
borderRadius: '0.75rem',
|
|
113
|
+
padding: '1.5rem',
|
|
114
|
+
background: '#f8fafc',
|
|
115
|
+
}}
|
|
116
|
+
>
|
|
117
|
+
<div style={{ marginBottom: '1rem' }}>
|
|
118
|
+
<div style={{ fontSize: '0.75rem', textTransform: 'uppercase', letterSpacing: '0.05em', color: '#64748b', marginBottom: '0.25rem' }}>
|
|
119
|
+
Connect status
|
|
120
|
+
</div>
|
|
121
|
+
<div style={{ fontWeight: 600 }}>
|
|
122
|
+
{status.connected ? (
|
|
123
|
+
status.chargesEnabled ? '✅ Active — charges enabled' : '⏳ Onboarding in progress'
|
|
124
|
+
) : '❌ Not connected'}
|
|
125
|
+
</div>
|
|
126
|
+
</div>
|
|
127
|
+
|
|
128
|
+
{status.connected && status.chargesEnabled && (
|
|
129
|
+
<button
|
|
130
|
+
onClick={handlePortal}
|
|
131
|
+
disabled={portalLoading}
|
|
132
|
+
style={{
|
|
133
|
+
background: '#7c3aed',
|
|
134
|
+
color: '#fff',
|
|
135
|
+
border: 'none',
|
|
136
|
+
borderRadius: '0.5rem',
|
|
137
|
+
padding: '0.6rem 1.25rem',
|
|
138
|
+
fontWeight: 600,
|
|
139
|
+
cursor: portalLoading ? 'default' : 'pointer',
|
|
140
|
+
opacity: portalLoading ? 0.7 : 1,
|
|
141
|
+
fontSize: '0.875rem',
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
{portalLoading ? 'Opening portal…' : 'Manage subscription →'}
|
|
145
|
+
</button>
|
|
146
|
+
)}
|
|
147
|
+
|
|
148
|
+
{!status.connected && (
|
|
149
|
+
<p style={{ fontSize: '0.875rem', color: '#64748b', marginTop: '0.5rem' }}>
|
|
150
|
+
The app owner needs to complete Stripe Connect onboarding before subscriptions
|
|
151
|
+
can be managed here.
|
|
152
|
+
</p>
|
|
153
|
+
)}
|
|
154
|
+
</div>
|
|
155
|
+
)}
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
}
|