-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAdminReview.tsx
More file actions
475 lines (429 loc) · 16.5 KB
/
Copy pathAdminReview.tsx
File metadata and controls
475 lines (429 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import { useState, useEffect, useCallback } from "react";
import { Link } from "react-router-dom";
import {
Container,
Title,
Text,
Card,
Group,
Stack,
Button,
Badge,
Loader,
Alert,
Textarea,
Modal,
Tabs,
SegmentedControl,
} from "@mantine/core";
import { CheckCircle, XCircle, AlertTriangle, User, Calendar, ArrowLeft, Eye, Flower2, Clock, History } from "lucide-react";
import { useAuth } from "@/contexts";
import { getPendingEngines, getReviewHistory, reviewEngine, type PendingEngine, type ReviewedEngine } from "@/lib/api";
import { PageLayout } from "@/components/PageLayout";
import type { EngineDefinition } from "@/types/engine";
import classes from "./AdminReview.module.css";
export function AdminReview() {
const { isAuthenticated, user, isLoading: authLoading } = useAuth();
const [activeTab, setActiveTab] = useState<string | null>("pending");
// Pending state
const [pendingEngines, setPendingEngines] = useState<PendingEngine[]>([]);
const [pendingLoading, setPendingLoading] = useState(true);
// History state
const [historyEngines, setHistoryEngines] = useState<ReviewedEngine[]>([]);
const [historyLoading, setHistoryLoading] = useState(false);
const [historyFilter, setHistoryFilter] = useState<"all" | "approved" | "rejected">("all");
const [historyLoaded, setHistoryLoaded] = useState(false);
// Shared state
const [error, setError] = useState<string | null>(null);
const [rejectingEngine, setRejectingEngine] = useState<PendingEngine | null>(null);
const [rejectReason, setRejectReason] = useState("");
const [actionLoading, setActionLoading] = useState<string | null>(null);
const loadPendingEngines = useCallback(async () => {
setPendingLoading(true);
setError(null);
const { data, error: apiError } = await getPendingEngines();
if (apiError) {
setError(apiError);
} else if (data) {
setPendingEngines(data.engines);
}
setPendingLoading(false);
}, []);
const loadHistoryEngines = useCallback(async (filter: "all" | "approved" | "rejected") => {
setHistoryLoading(true);
setError(null);
const { data, error: apiError } = await getReviewHistory(filter);
if (apiError) {
setError(apiError);
} else if (data) {
setHistoryEngines(data.engines);
}
setHistoryLoading(false);
setHistoryLoaded(true);
}, []);
useEffect(() => {
if (isAuthenticated && user?.isAdmin) {
loadPendingEngines();
}
}, [isAuthenticated, user?.isAdmin, loadPendingEngines]);
// Load history when tab changes or filter changes
useEffect(() => {
if (activeTab === "history" && isAuthenticated && user?.isAdmin) {
loadHistoryEngines(historyFilter);
}
}, [activeTab, historyFilter, isAuthenticated, user?.isAdmin, loadHistoryEngines]);
const handleApprove = async (engineId: string) => {
setActionLoading(engineId);
const { error: apiError } = await reviewEngine(engineId, "approve");
if (apiError) {
setError(apiError);
} else {
setPendingEngines((prev) => prev.filter((e) => e.id !== engineId));
// Refresh history if it was loaded
if (historyLoaded) {
loadHistoryEngines(historyFilter);
}
}
setActionLoading(null);
};
const handleReject = async () => {
if (!rejectingEngine) return;
setActionLoading(rejectingEngine.id);
const { error: apiError } = await reviewEngine(rejectingEngine.id, "reject", rejectReason || undefined);
if (apiError) {
setError(apiError);
} else {
setPendingEngines((prev) => prev.filter((e) => e.id !== rejectingEngine.id));
setRejectingEngine(null);
setRejectReason("");
// Refresh history if it was loaded
if (historyLoaded) {
loadHistoryEngines(historyFilter);
}
}
setActionLoading(null);
};
// Auth loading
if (authLoading) {
return (
<PageLayout>
<Container size="md" py="xl">
<Stack align="center" gap="md">
<Loader size="lg" />
<Text c="dimmed">Loading...</Text>
</Stack>
</Container>
</PageLayout>
);
}
// Not authenticated
if (!isAuthenticated) {
return (
<PageLayout>
<Container size="md" py="xl">
<Alert icon={<AlertTriangle size={20} />} color="yellow" title="Sign In Required">
Please sign in to access the admin panel.
</Alert>
</Container>
</PageLayout>
);
}
// Not admin
if (!user?.isAdmin) {
return (
<PageLayout>
<Container size="md" py="xl">
<Alert icon={<AlertTriangle size={20} />} color="red" title="Access Denied">
You do not have permission to access this page.
</Alert>
</Container>
</PageLayout>
);
}
return (
<PageLayout>
<Container w="100%" fluid size="lg" py="xl" className={classes.container}>
<Group justify="space-between" mb="xl" w="100%">
<Group gap="md">
<Button component={Link} to="/" variant="subtle" leftSection={<ArrowLeft size={16} />}>
Back
</Button>
<Title order={2}>
<Group gap="xs">
<Flower2 size={28} />
Admin Review
</Group>
</Title>
</Group>
{pendingEngines.length > 0 && (
<Badge size="lg" color="violet">
{pendingEngines.length} pending
</Badge>
)}
</Group>
{error && (
<Alert icon={<AlertTriangle size={20} />} color="red" mb="md" withCloseButton onClose={() => setError(null)}>
{error}
</Alert>
)}
<Tabs value={activeTab} onChange={setActiveTab}>
<Tabs.List mb="md">
<Tabs.Tab value="pending" leftSection={<Clock size={16} />}>
Pending Review
{pendingEngines.length > 0 && (
<Badge size="sm" color="yellow" ml="xs">
{pendingEngines.length}
</Badge>
)}
</Tabs.Tab>
<Tabs.Tab value="history" leftSection={<History size={16} />}>
History
</Tabs.Tab>
</Tabs.List>
<Tabs.Panel value="pending">
{pendingLoading ? (
<Stack align="center" py="xl">
<Loader size="lg" />
<Text c="dimmed">Loading pending engines...</Text>
</Stack>
) : pendingEngines.length === 0 ? (
<Card withBorder p="xl">
<Stack align="center" gap="md">
<CheckCircle size={48} opacity={0.3} />
<Text c="dimmed" size="lg">
No engines pending review
</Text>
<Text c="dimmed" size="sm">
All caught up! 🎉
</Text>
</Stack>
</Card>
) : (
<Stack gap="md">
{pendingEngines.map((engine) => {
const def = engine.definition as EngineDefinition;
return (
<Card key={engine.id} withBorder className={classes.engineCard}>
<Group justify="space-between" align="flex-start" wrap="nowrap">
<Stack gap="xs" style={{ flex: 1 }}>
<Group gap="xs">
<Text fw={700} size="lg">
{def.name}
</Text>
<Badge color="yellow" size="sm">
Pending Review
</Badge>
</Group>
{def.description && (
<Text c="dimmed" lineClamp={2}>
{def.description}
</Text>
)}
<Group gap="lg" mt="xs">
<Group gap={4}>
<User size={14} opacity={0.6} />
<Text size="sm" c="dimmed">
{engine.user.displayName || engine.user.email}
</Text>
</Group>
<Group gap={4}>
<Calendar size={14} opacity={0.6} />
<Text size="sm" c="dimmed">
Submitted {new Date(engine.submittedAt).toLocaleDateString()}
</Text>
</Group>
</Group>
<Group gap="xs" mt="sm">
<Text size="sm" c="dimmed">
{def.nodes.length} hexes • Roll: {def.roll}
</Text>
</Group>
</Stack>
<Stack gap="xs">
<Button
component={Link}
to={`/admin/review/${engine.id}`}
variant="light"
leftSection={<Eye size={16} />}
>
Preview
</Button>
<Button
color="green"
leftSection={<CheckCircle size={16} />}
onClick={() => handleApprove(engine.id)}
loading={actionLoading === engine.id}
disabled={actionLoading !== null}
>
Approve
</Button>
<Button
color="red"
variant="light"
leftSection={<XCircle size={16} />}
onClick={() => setRejectingEngine(engine)}
disabled={actionLoading !== null}
>
Reject
</Button>
</Stack>
</Group>
</Card>
);
})}
</Stack>
)}
</Tabs.Panel>
<Tabs.Panel value="history">
<Group mb="md">
<SegmentedControl
value={historyFilter}
onChange={(value) => setHistoryFilter(value as "all" | "approved" | "rejected")}
data={[
{ label: "All", value: "all" },
{ label: "Approved", value: "approved" },
{ label: "Rejected", value: "rejected" },
]}
/>
</Group>
{historyLoading ? (
<Stack align="center" py="xl">
<Loader size="lg" />
<Text c="dimmed">Loading review history...</Text>
</Stack>
) : historyEngines.length === 0 ? (
<Card withBorder p="xl">
<Stack align="center" gap="md">
<History size={48} opacity={0.3} />
<Text c="dimmed" size="lg">
No review history
</Text>
<Text c="dimmed" size="sm">
{historyFilter === "all" ? "No engines have been reviewed yet" : `No ${historyFilter} engines found`}
</Text>
</Stack>
</Card>
) : (
<Stack gap="md">
{historyEngines.map((engine) => {
const def = engine.definition as EngineDefinition;
const isApproved = engine.visibility === "public";
return (
<Card key={engine.id} withBorder className={classes.engineCard}>
<Group justify="space-between" align="flex-start" wrap="nowrap">
<Stack gap="xs" style={{ flex: 1 }}>
<Group gap="xs">
<Text fw={700} size="lg">
{def.name}
</Text>
<Badge
color={isApproved ? "green" : "red"}
size="sm"
leftSection={isApproved ? <CheckCircle size={12} /> : <XCircle size={12} />}
>
{isApproved ? "Approved" : "Rejected"}
</Badge>
</Group>
{def.description && (
<Text c="dimmed" lineClamp={2}>
{def.description}
</Text>
)}
{!isApproved && engine.rejectionReason && (
<Alert color="red" variant="light" p="xs">
<Text size="sm">
<strong>Reason:</strong> {engine.rejectionReason}
</Text>
</Alert>
)}
<Group gap="lg" mt="xs">
<Group gap={4}>
<User size={14} opacity={0.6} />
<Text size="sm" c="dimmed">
{engine.user.displayName || engine.user.email}
</Text>
</Group>
<Group gap={4}>
<Calendar size={14} opacity={0.6} />
<Text size="sm" c="dimmed">
Reviewed {new Date(engine.reviewedAt).toLocaleDateString()}
</Text>
</Group>
{engine.reviewer && (
<Text size="sm" c="dimmed">
by {engine.reviewer.displayName || engine.reviewer.email}
</Text>
)}
</Group>
<Group gap="xs" mt="sm">
<Text size="sm" c="dimmed">
{def.nodes.length} hexes • Roll: {def.roll}
{isApproved && engine.useCount > 0 && ` • ${engine.useCount} uses`}
</Text>
</Group>
</Stack>
<Stack gap="xs">
{isApproved && (
<Button component={Link} to={`/gallery`} variant="light" size="sm">
View in Garden
</Button>
)}
</Stack>
</Group>
</Card>
);
})}
</Stack>
)}
</Tabs.Panel>
</Tabs>
{/* Reject Modal */}
<Modal
opened={!!rejectingEngine}
onClose={() => {
setRejectingEngine(null);
setRejectReason("");
}}
title="Reject Engine"
>
<Stack gap="md">
<Text>
Are you sure you want to reject{" "}
<strong>{rejectingEngine ? (rejectingEngine.definition as EngineDefinition).name : ""}</strong>?
</Text>
<Text size="sm" c="dimmed">
The engine will be returned to private status. You can optionally provide a reason.
</Text>
<Textarea
label="Rejection Reason (optional)"
placeholder="Enter feedback for the creator..."
value={rejectReason}
onChange={(e) => setRejectReason(e.currentTarget.value)}
rows={3}
/>
<Group justify="flex-end" gap="sm">
<Button
variant="subtle"
onClick={() => {
setRejectingEngine(null);
setRejectReason("");
}}
>
Cancel
</Button>
<Button
color="red"
leftSection={<XCircle size={16} />}
onClick={handleReject}
loading={actionLoading === rejectingEngine?.id}
>
Reject Engine
</Button>
</Group>
</Stack>
</Modal>
</Container>
</PageLayout>
);
}
export default AdminReview;