Swadhin Biswas Feature/ai code reviewer (#73) · 8/2/2026
105a287
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
---
import AdminLayout from "@/layouts/AdminLayout.astro";
import { getDatabase, schema } from "@/db";
import { desc, eq, like, or, sql } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import {
Users as UsersIcon,
Shield,
Ban,
CheckCircle,
Search,
ChevronLeft,
ChevronRight,
} from "lucide-react";
const user = Astro.locals.user;
// Only admins can access
if (!user?.isAdmin) {
return Astro.redirect("/");
}
const db = getDatabase() as NodePgDatabase<typeof schema>;
const searchQuery = Astro.url.searchParams.get("q") || "";
const page = parseInt(Astro.url.searchParams.get("page") || "1");
const limit = 20;
const offset = (page - 1) * limit;
// Build query conditions
let whereCondition = undefined;
if (searchQuery) {
whereCondition = or(
like(schema.users.username, `%${searchQuery}%`),
like(schema.users.email, `%${searchQuery}%`),
like(schema.users.displayName, `%${searchQuery}%`),
);
}
// Fetch users
const [users, totalResult] = await Promise.all([
db.query.users.findMany({
where: whereCondition,
orderBy: [desc(schema.users.createdAt)],
limit,
offset,
}),
db
.select({ count: sql`count(*)` })
.from(schema.users)
.where(whereCondition)
.limit(1),
]);
const total = Number(totalResult[0]?.count) || 0;
const totalPages = Math.ceil(total / limit);
// Stats
const [totalUsersResult, activeUsersResult, adminUsersResult] = await Promise.all([
db
.select({ count: sql`count(*)` })
.from(schema.users)
.limit(1),
db
.select({ count: sql`count(*)` })
.from(schema.users)
.where(eq(schema.users.isActive, true))
.limit(1),
db
.select({ count: sql`count(*)` })
.from(schema.users)
.where(eq(schema.users.isAdmin, true))
.limit(1),
]);
const totalUsers = Number(totalUsersResult[0]?.count) || 0;
const activeUsers = Number(activeUsersResult[0]?.count) || 0;
const adminUsers = Number(adminUsersResult[0]?.count) || 0;
---
<AdminLayout title="User Management">
<div class="min-h-screen bg-background p-6">
<div class="max-w-7xl mx-auto space-y-6">
<!-- Header -->
<div class="flex items-center justify-between">
<div>
<h1 class="text-2xl font-bold flex items-center gap-2">
<UsersIcon className="h-6 w-6" />
User Management
</h1>
<p class="text-muted-foreground">Manage all registered users</p>
</div>
<a href="/admin" class="text-muted-foreground hover:text-foreground">
← Back to Dashboard
</a>
</div>
<!-- Stats -->
<div class="grid gap-4 md:grid-cols-3">
<Card>
<CardContent className="p-4 flex items-center gap-4">
<div class="p-3 rounded-full bg-blue-500/10">
<UsersIcon className="h-5 w-5 text-blue-500" />
</div>
<div>
<p class="text-sm text-muted-foreground">Total Users</p>
<p class="text-2xl font-bold">{totalUsers}</p>
</div>
</CardContent>
</Card>
<Card>
<CardContent className="p-4 flex items-center gap-4">
<div class="p-3 rounded-full bg-green-500/10">
<CheckCircle className="h-5 w-5 text-green-500" />
</div>
<div>
<p class="text-sm text-muted-foreground">Active</p>
<p class="text-2xl font-bold">{activeUsers}</p>
</div>
</CardContent>
</Card>
<Card>
<CardContent className="p-4 flex items-center gap-4">
<div class="p-3 rounded-full bg-orange-500/10">
<Shield className="h-5 w-5 text-orange-500" />
</div>
<div>
<p class="text-sm text-muted-foreground">Admins</p>
<p class="text-2xl font-bold">{adminUsers}</p>
</div>
</CardContent>
</Card>
</div>
<!-- Search -->
<form action="/admin/users" method="GET" class="flex gap-2">
<div class="relative flex-1">
<Search
className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground"
/>
<Input
name="q"
value={searchQuery}
placeholder="Search by username, email, or name..."
className="pl-10"
/>
</div>
<Button type="submit">Search</Button>
</form>
<!-- Users Table -->
<Card>
<CardHeader>
<CardTitle>Users ({total})</CardTitle>
</CardHeader>
<CardContent>
<div class="overflow-x-auto">
<table class="w-full">
<thead>
<tr class="border-b">
<th
class="text-left p-3 text-sm font-medium text-muted-foreground"
>User</th
>
<th
class="text-left p-3 text-sm font-medium text-muted-foreground"
>Email</th
>
<th
class="text-left p-3 text-sm font-medium text-muted-foreground"
>Status</th
>
<th
class="text-left p-3 text-sm font-medium text-muted-foreground"
>Role</th
>
<th
class="text-left p-3 text-sm font-medium text-muted-foreground"
>Joined</th
>
<th
class="text-right p-3 text-sm font-medium text-muted-foreground"
>Actions</th
>
</tr>
</thead>
<tbody>
{
users.map((u: typeof users[number]) => (
<tr class="border-b hover:bg-muted/50" data-user-id={u.id}>
<td class="p-3">
<div class="flex items-center gap-3">
{u.avatarUrl ? (
<img
src={u.avatarUrl}
alt={u.username}
class="h-8 w-8 rounded-full"
/>
) : (
<div class="h-8 w-8 rounded-full bg-muted flex items-center justify-center font-medium">
{u.username.charAt(0).toUpperCase()}
</div>
)}
<div>
<a
href={`/${u.username}`}
class="font-medium hover:text-primary"
>
{u.displayName || u.username}
</a>
<div class="text-sm text-muted-foreground">
@{u.username}
</div>
</div>
</div>
</td>
<td class="p-3 text-sm">{u.email}</td>
<td class="p-3">
{u.isActive ? (
<span class="inline-flex items-center gap-1 text-xs bg-green-500/10 text-green-500 px-2 py-1 rounded">
<CheckCircle className="h-3 w-3" /> Active
</span>
) : (
<span class="inline-flex items-center gap-1 text-xs bg-red-500/10 text-red-500 px-2 py-1 rounded">
<Ban className="h-3 w-3" /> Banned
</span>
)}
</td>
<td class="p-3">
{u.isAdmin ? (
<span class="inline-flex items-center gap-1 text-xs bg-orange-500/10 text-orange-500 px-2 py-1 rounded">
<Shield className="h-3 w-3" /> Admin
</span>
) : (
<span class="text-sm text-muted-foreground">
User
</span>
)}
</td>
<td class="p-3 text-sm text-muted-foreground">
{new Date(u.createdAt).toLocaleDateString()}
</td>
<td class="p-3 text-right">
<div class="flex items-center justify-end gap-2">
{u.id !== user.id && (
<>
<button
class="toggle-admin-btn p-1.5 text-xs rounded hover:bg-muted"
data-user-id={u.id}
data-is-admin={u.isAdmin}
title={
u.isAdmin ? "Remove admin" : "Make admin"
}
>
<Shield className="h-4 w-4" />
</button>
<button
class="toggle-ban-btn p-1.5 text-xs rounded hover:bg-muted"
data-user-id={u.id}
data-is-active={u.isActive}
title={u.isActive ? "Ban user" : "Unban user"}
>
<Ban className="h-4 w-4" />
</button>
</>
)}
</div>
</td>
</tr>
))
}
</tbody>
</table>
</div>
{/* Pagination */}
{
totalPages > 1 && (
<div class="flex items-center justify-between mt-4 pt-4 border-t">
<div class="text-sm text-muted-foreground">
Showing {offset + 1} - {Math.min(offset + limit, total)} of{" "}
{total}
</div>
<div class="flex items-center gap-2">
{page > 1 && (
<a
href={`/admin/users?page=${page - 1}${searchQuery ? `&q=${searchQuery}` : ""}`}
>
<Button variant="outline" size="sm">
<ChevronLeft className="h-4 w-4" />
</Button>
</a>
)}
<span class="text-sm">
Page {page} of {totalPages}
</span>
{page < totalPages && (
<a
href={`/admin/users?page=${page + 1}${searchQuery ? `&q=${searchQuery}` : ""}`}
>
<Button variant="outline" size="sm">
<ChevronRight className="h-4 w-4" />
</Button>
</a>
)}
</div>
</div>
)
}
</CardContent>
</Card>
</div>
</div>
</AdminLayout>
<script>
// Toggle admin
document.querySelectorAll(".toggle-admin-btn").forEach((element) => {
const btn = element as HTMLButtonElement;
btn.addEventListener("click", async () => {
const userId = btn.dataset.userId;
const isAdmin = btn.dataset.isAdmin === "true";
const action = isAdmin ? "Remove admin role?" : "Grant admin role?";
if (!confirm(action)) return;
try {
const response = await fetch(`/api/admin/users/${userId}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ isAdmin: !isAdmin }),
});
if (!response.ok) throw new Error("Failed");
window.location.reload();
} catch (e) {
alert("Failed to update user");
}
});
});
// Toggle ban
document.querySelectorAll(".toggle-ban-btn").forEach((element) => {
const btn = element as HTMLButtonElement;
btn.addEventListener("click", async () => {
const userId = btn.dataset.userId;
const isActive = btn.dataset.isActive === "true";
const action = isActive ? "Ban this user?" : "Unban this user?";
if (!confirm(action)) return;
try {
const response = await fetch(`/api/admin/users/${userId}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ isActive: !isActive }),
});
if (!response.ok) throw new Error("Failed");
window.location.reload();
} catch (e) {
alert("Failed to update user");
}
});
});
</script>