-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAdminUsersManager.tsx
More file actions
378 lines (360 loc) · 11.9 KB
/
AdminUsersManager.tsx
File metadata and controls
378 lines (360 loc) · 11.9 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
'use client';
import { useState, useEffect } from 'react';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '@/components/ui/dialog';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
import { Badge } from '@/components/ui/badge';
import { useToast } from '@/hooks/use-toast';
import {
Plus,
Trash2,
User,
Calendar,
UserCheck,
AlertTriangle,
Shield,
Crown,
} from 'lucide-react';
interface AdminUser {
id: number;
github_username: string;
added_by: string;
added_at: string;
is_active: boolean;
notes?: string;
}
export default function AdminUsersManager() {
const [users, setUsers] = useState<AdminUser[]>([]);
const [loading, setLoading] = useState(true);
const [dialogOpen, setDialogOpen] = useState(false);
const [newUser, setNewUser] = useState({
github_username: '',
notes: '',
});
const { toast } = useToast();
const API_BASE = process.env.NEXT_PUBLIC_API_BASE || 'http://localhost:8000/api';
useEffect(() => {
fetchUsers();
}, []);
const fetchUsers = async () => {
try {
const response = await fetch(`${API_BASE}/admin/users`, {
credentials: 'include',
});
if (response.ok) {
const data = await response.json();
setUsers(data);
} else {
throw new Error('Failed to fetch admin users');
}
} catch (error) {
console.error('Error fetching admin users:', error);
toast({
title: 'Error',
description: 'Failed to fetch admin users',
variant: 'destructive',
});
} finally {
setLoading(false);
}
};
const createUser = async () => {
if (!newUser.github_username.trim()) {
toast({
title: 'Validation Error',
description: 'GitHub username is required',
variant: 'destructive',
});
return;
}
try {
const response = await fetch(`${API_BASE}/admin/users`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
body: JSON.stringify({
github_username: newUser.github_username.trim(),
notes: newUser.notes.trim() || null,
}),
});
if (response.ok) {
const createdUser = await response.json();
setUsers((prev) => [...prev, createdUser]);
setNewUser({ github_username: '', notes: '' });
setDialogOpen(false);
toast({
title: 'Success',
description: `Admin user @${createdUser.github_username} created successfully`,
});
} else {
const error = await response.json();
throw new Error(error.detail || 'Failed to create admin user');
}
} catch (error) {
console.error('Error creating admin user:', error);
toast({
title: 'Error',
description:
error instanceof Error
? error.message
: 'Failed to create admin user',
variant: 'destructive',
});
}
};
const removeUser = async (username: string) => {
const isConfirmed = confirm(
`Remove admin access for @${username}?\n\n` +
`This user will lose access to the administration panel and all admin features.\n\n` +
`Are you sure you want to continue?`
);
if (!isConfirmed) {
return;
}
try {
const response = await fetch(`${API_BASE}/admin/users/${username}`, {
method: 'DELETE',
credentials: 'include',
});
if (response.ok) {
setUsers((prev) =>
prev.filter((user) => user.github_username !== username)
);
toast({
title: 'Success',
description: `Admin user @${username} removed successfully`,
});
} else {
const error = await response.json();
throw new Error(error.detail || 'Failed to remove admin user');
}
} catch (error) {
console.error('Error removing admin user:', error);
toast({
title: 'Error',
description:
error instanceof Error
? error.message
: 'Failed to remove admin user',
variant: 'destructive',
});
}
};
const formatDate = (dateString: string) => {
return new Date(dateString).toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
});
};
if (loading) {
return (
<Card>
<CardContent className="flex items-center justify-center py-8">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-gray-900"></div>
</CardContent>
</Card>
);
}
return (
<Card>
<CardHeader>
<div className="flex justify-between items-start">
<div>
<CardTitle className="flex items-center gap-2">
<UserCheck className="w-5 h-5" />
Admin Users
<Badge variant="secondary" className="ml-2 text-xs">
Administrative Access
</Badge>
</CardTitle>
<CardDescription>
Manage users with administrative privileges. Admin users can
access this panel and manage system configurations.
</CardDescription>
</div>
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<DialogTrigger asChild>
<Button>
<Plus className="w-4 h-4 mr-2" />
Add Admin User
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Add New Admin User</DialogTitle>
<DialogDescription className="space-y-2">
<p>
Grant administrative access to a GitHub user. They will be
able to access this admin panel and manage system settings.
</p>
<div className="p-3 bg-muted/50 border rounded-md">
<div className="flex items-start gap-2">
<Shield className="w-4 h-4 text-orange-500 mt-0.5 flex-shrink-0" />
<div className="text-sm">
<p className="font-medium">
Administrative privileges include:
</p>
<p className="text-muted-foreground text-xs mt-1">
Binary configurations • Environment settings • Run
management • User administration
</p>
</div>
</div>
</div>
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
<div>
<Label htmlFor="username">GitHub Username</Label>
<Input
id="username"
placeholder="e.g., octocat"
value={newUser.github_username}
onChange={(e) =>
setNewUser((prev) => ({
...prev,
github_username: e.target.value,
}))
}
/>
</div>
<div>
<Label htmlFor="notes">Notes (Optional)</Label>
<Textarea
id="notes"
placeholder="e.g., Core team member, temporary access, etc."
value={newUser.notes}
onChange={(e) =>
setNewUser((prev) => ({ ...prev, notes: e.target.value }))
}
rows={3}
/>
</div>
<div className="flex gap-2 pt-4">
<Button onClick={createUser} className="flex-1">
<Plus className="w-4 h-4 mr-2" />
Add Admin User
</Button>
<Button
variant="outline"
onClick={() => setDialogOpen(false)}
>
Cancel
</Button>
</div>
</div>
</DialogContent>
</Dialog>
</div>
</CardHeader>
<CardContent>
{users.length === 0 ? (
<div className="text-center py-8 text-muted-foreground">
<User className="w-12 h-12 mx-auto mb-4 opacity-50" />
<p className="text-lg font-medium">No admin users found</p>
<p className="text-sm">Add your first admin user to get started</p>
</div>
) : (
<div className="rounded-md border">
<Table>
<TableHeader>
<TableRow>
<TableHead>GitHub User</TableHead>
<TableHead>Added By</TableHead>
<TableHead>Added Date</TableHead>
<TableHead>Status</TableHead>
<TableHead>Notes</TableHead>
<TableHead className="w-24">Actions</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{users.map((user) => (
<TableRow key={user.id}>
<TableCell>
<div className="flex items-center gap-2">
<User className="w-4 h-4 text-muted-foreground" />
<span className="font-medium">
@{user.github_username}
</span>
</div>
</TableCell>
<TableCell className="text-muted-foreground">
@{user.added_by}
</TableCell>
<TableCell>
<div className="flex items-center gap-2 text-muted-foreground">
<Calendar className="w-4 h-4" />
{formatDate(user.added_at)}
</div>
</TableCell>
<TableCell>
<Badge variant={user.is_active ? 'default' : 'secondary'}>
{user.is_active ? 'Active' : 'Inactive'}
</Badge>
</TableCell>
<TableCell className="max-w-xs">
{user.notes ? (
<span className="text-sm text-muted-foreground line-clamp-2">
{user.notes}
</span>
) : (
<span className="text-sm text-muted-foreground italic">
No notes
</span>
)}
</TableCell>
<TableCell>
<Button
variant="ghost"
size="sm"
onClick={() => removeUser(user.github_username)}
className="text-destructive hover:text-destructive hover:bg-destructive/10"
>
<Trash2 className="w-4 h-4" />
</Button>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
)}
{users.length > 0 && (
<div className="mt-4 text-sm text-muted-foreground">
<p>
💡 <strong>Tip:</strong> Admin users can log in with their GitHub
account and access this administration panel.
</p>
</div>
)}
</CardContent>
</Card>
);
}