create-githat-app 1.8.6 → 1.8.7

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.
@@ -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
+ }