-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdashboard.tsx
More file actions
479 lines (448 loc) · 15.5 KB
/
dashboard.tsx
File metadata and controls
479 lines (448 loc) · 15.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
476
477
478
479
import { useMemo } from "react";
import { useLink, useList } from "@refinedev/core";
import {
Bar,
BarChart,
Cell,
Pie,
PieChart,
ResponsiveContainer,
Tooltip,
XAxis,
YAxis,
} from "recharts";
import {
BookOpen,
Building2,
GraduationCap,
Layers,
ShieldCheck,
Users,
} from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Separator } from "@/components/ui/separator";
import type { Department, Subject, User } from "@/types";
type ClassListItem = {
id: number;
name: string;
createdAt?: string;
subject?: {
name: string;
};
teacher?: {
name: string;
};
};
const roleColors = ["#f97316", "#0ea5e9", "#22c55e", "#a855f7"];
const EMPTY_USERS: User[] = [];
const EMPTY_SUBJECTS: Subject[] = [];
const EMPTY_DEPARTMENTS: Department[] = [];
const EMPTY_CLASSES: ClassListItem[] = [];
const Dashboard = () => {
const Link = useLink();
const { query: usersQuery } = useList<User>({
resource: "users",
pagination: { mode: "off" },
});
const { query: subjectsQuery } = useList<Subject>({
resource: "subjects",
pagination: { mode: "off" },
});
const { query: departmentsQuery } = useList<Department>({
resource: "departments",
pagination: { mode: "off" },
});
const { query: classesQuery } = useList<ClassListItem>({
resource: "classes",
pagination: { mode: "off" },
});
const users = usersQuery.data?.data ?? EMPTY_USERS;
const subjects = subjectsQuery.data?.data ?? EMPTY_SUBJECTS;
const departments = departmentsQuery.data?.data ?? EMPTY_DEPARTMENTS;
const classes = classesQuery.data?.data ?? EMPTY_CLASSES;
const usersByRole = useMemo(() => {
const counts = users.reduce<Record<string, number>>((acc, user) => {
const role = user.role ?? "unknown";
acc[role] = (acc[role] || 0) + 1;
return acc;
}, {});
return Object.entries(counts).map(([role, total]) => ({ role, total }));
}, [users]);
const subjectsByDepartment = useMemo(() => {
const counts = subjects.reduce<Record<string, number>>((acc, subject) => {
const departmentName =
(subject as { department?: { name?: string } }).department?.name ??
"Unassigned";
acc[departmentName] = (acc[departmentName] || 0) + 1;
return acc;
}, {});
return Object.entries(counts).map(([departmentName, totalSubjects]) => ({
departmentName,
totalSubjects,
}));
}, [subjects]);
const classesBySubject = useMemo(() => {
const counts = classes.reduce<Record<string, number>>((acc, classItem) => {
const subjectName = classItem.subject?.name ?? "Unassigned";
acc[subjectName] = (acc[subjectName] || 0) + 1;
return acc;
}, {});
return Object.entries(counts).map(([subjectName, totalClasses]) => ({
subjectName,
totalClasses,
}));
}, [classes]);
const newestClasses = useMemo(() => {
return [...classes]
.sort((a, b) => {
const aTime = a.createdAt ? new Date(a.createdAt).getTime() : 0;
const bTime = b.createdAt ? new Date(b.createdAt).getTime() : 0;
return bTime - aTime;
})
.slice(0, 5);
}, [classes]);
const newestTeachers = useMemo(() => {
return users
.filter((user) => user.role === "teacher")
.sort((a, b) => {
const aTime = a.createdAt ? new Date(a.createdAt).getTime() : 0;
const bTime = b.createdAt ? new Date(b.createdAt).getTime() : 0;
return bTime - aTime;
})
.slice(0, 5);
}, [users]);
const topDepartments = useMemo(() => {
return [...subjectsByDepartment]
.sort((a, b) => b.totalSubjects - a.totalSubjects)
.slice(0, 5)
.map((item, index) => ({
...item,
departmentId: index,
}));
}, [subjectsByDepartment]);
const topSubjects = useMemo(() => {
return [...classesBySubject]
.sort((a, b) => b.totalClasses - a.totalClasses)
.slice(0, 5)
.map((item, index) => ({
...item,
subjectId: index,
}));
}, [classesBySubject]);
const kpis = [
{
label: "Total Users",
value: users.length,
icon: Users,
accent: "text-blue-600",
},
{
label: "Teachers",
value: users.filter((user) => user.role === "teacher").length,
icon: GraduationCap,
accent: "text-emerald-600",
},
{
label: "Admins",
value: users.filter((user) => user.role === "admin").length,
icon: ShieldCheck,
accent: "text-amber-600",
},
{
label: "Subjects",
value: subjects.length,
icon: BookOpen,
accent: "text-purple-600",
},
{
label: "Departments",
value: departments.length,
icon: Building2,
accent: "text-cyan-600",
},
{
label: "Classes",
value: classes.length,
icon: Layers,
accent: "text-rose-600",
},
];
return (
<div className="space-y-6">
<div>
<h1 className="page-title">Dashboard</h1>
<p className="text-muted-foreground">
A quick snapshot of the latest activity and key metrics.
</p>
</div>
<Card className="hover:shadow-md transition-shadow">
<CardHeader>
<CardTitle>Overview</CardTitle>
</CardHeader>
<CardContent>
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-6">
{kpis.map((kpi) => (
<div
key={kpi.label}
className="rounded-lg border border-border bg-muted/20 p-4 hover:border-primary/40 hover:bg-muted/40 transition-colors"
>
<div className="flex items-center justify-between">
<p className="text-xs font-semibold text-muted-foreground">
{kpi.label}
</p>
<kpi.icon className={`h-4 w-4 ${kpi.accent}`} />
</div>
<div className="mt-2 text-2xl font-semibold">{kpi.value}</div>
</div>
))}
</div>
</CardContent>
</Card>
<div className="grid gap-6 lg:grid-cols-3">
<Card className="lg:col-span-2 hover:shadow-md transition-shadow">
<CardHeader>
<CardTitle>Users by Role</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<div className="h-72">
<ResponsiveContainer width="100%" height="100%">
<PieChart>
<Pie
dataKey="total"
nameKey="role"
data={usersByRole}
innerRadius={60}
outerRadius={100}
paddingAngle={3}
>
{usersByRole.map((entry, index) => (
<Cell
key={`${entry.role}-${index}`}
fill={roleColors[index % roleColors.length]}
/>
))}
</Pie>
<Tooltip />
</PieChart>
</ResponsiveContainer>
</div>
<div className="flex flex-wrap gap-2">
{usersByRole.map((entry, index) => (
<span
key={entry.role}
className="inline-flex items-center gap-2 rounded-full bg-muted px-3 py-1 text-xs font-medium"
>
<span
className="h-2 w-2 rounded-full"
style={{
backgroundColor:
roleColors[index % roleColors.length],
}}
/>
{entry.role} · {entry.total}
</span>
))}
</div>
</CardContent>
</Card>
<div className="grid gap-4">
<Card className="hover:shadow-md transition-shadow">
<CardHeader>
<CardTitle>New Classes (last 5)</CardTitle>
</CardHeader>
<CardContent>
<div className="text-3xl font-semibold">
{newestClasses.length}
</div>
<p className="text-sm text-muted-foreground">
Most recent classes added
</p>
</CardContent>
</Card>
<Card className="hover:shadow-md transition-shadow">
<CardHeader>
<CardTitle>New Teachers (last 5)</CardTitle>
</CardHeader>
<CardContent>
<div className="text-3xl font-semibold">
{newestTeachers.length}
</div>
<p className="text-sm text-muted-foreground">
Most recent teachers added
</p>
</CardContent>
</Card>
</div>
</div>
<Card className="hover:shadow-md transition-shadow">
<CardHeader>
<CardTitle>Insights</CardTitle>
</CardHeader>
<CardContent className="grid gap-6 lg:grid-cols-2">
<div className="space-y-3">
<h3 className="text-sm font-semibold text-muted-foreground">
Subjects per Department
</h3>
<div className="h-80">
<ResponsiveContainer width="100%" height="100%">
<BarChart data={subjectsByDepartment}>
<XAxis dataKey="departmentName" tick={{ fontSize: 12 }} />
<YAxis allowDecimals={false} />
<Tooltip />
<Bar
dataKey="totalSubjects"
fill="#f97316"
radius={[6, 6, 0, 0]}
/>
</BarChart>
</ResponsiveContainer>
</div>
</div>
<div className="space-y-3">
<h3 className="text-sm font-semibold text-muted-foreground">
Classes per Subject
</h3>
<div className="h-80">
<ResponsiveContainer width="100%" height="100%">
<BarChart data={classesBySubject}>
<XAxis dataKey="subjectName" tick={{ fontSize: 12 }} />
<YAxis allowDecimals={false} />
<Tooltip />
<Bar
dataKey="totalClasses"
fill="#0ea5e9"
radius={[6, 6, 0, 0]}
/>
</BarChart>
</ResponsiveContainer>
</div>
</div>
</CardContent>
</Card>
<div className="grid gap-6 lg:grid-cols-2">
<Card className="hover:shadow-md transition-shadow">
<CardHeader>
<CardTitle>Newest Classes</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
{newestClasses.length === 0 && (
<p className="text-sm text-muted-foreground">
No recent classes.
</p>
)}
{newestClasses.map((item, index) => (
<Link
key={item.id}
to={`/classes/show/${item.id}`}
className="flex items-center justify-between rounded-md border border-transparent px-3 py-2 transition-colors hover:border-primary/30 hover:bg-muted/40"
>
<div className="flex items-center gap-3">
<span className="text-xs font-semibold text-muted-foreground">
#{index + 1}
</span>
<div>
<p className="text-sm font-medium">{item.name}</p>
<p className="text-xs text-muted-foreground">
{item.subject?.name ?? "No subject"} ·{" "}
{item.teacher?.name ?? "No teacher"}
</p>
</div>
</div>
<Badge variant="secondary">New</Badge>
</Link>
))}
</CardContent>
</Card>
<Card className="hover:shadow-md transition-shadow">
<CardHeader>
<CardTitle>Newest Teachers</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
{newestTeachers.length === 0 && (
<p className="text-sm text-muted-foreground">
No recent teachers.
</p>
)}
{newestTeachers.map((teacher, index) => (
<Link
key={teacher.id}
to={`/users/show/${teacher.id}`}
className="flex items-center justify-between rounded-md border border-transparent px-3 py-2 transition-colors hover:border-primary/30 hover:bg-muted/40"
>
<div className="flex items-center gap-3">
<span className="text-xs font-semibold text-muted-foreground">
#{index + 1}
</span>
<div>
<p className="text-sm font-medium">{teacher.name}</p>
<p className="text-xs text-muted-foreground">
{teacher.email}
</p>
</div>
</div>
<Badge variant="secondary">New</Badge>
</Link>
))}
</CardContent>
</Card>
</div>
<div className="grid gap-6 lg:grid-cols-2">
<Card className="hover:shadow-md transition-shadow">
<CardHeader>
<CardTitle>Departments with Most Subjects</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
{topDepartments.map((dept, index) => (
<div
key={dept.departmentId}
className="flex items-center justify-between rounded-md border border-transparent px-3 py-2 transition-colors hover:border-primary/30 hover:bg-muted/40"
>
<div className="flex items-center gap-3">
<span className="text-xs font-semibold text-muted-foreground">
#{index + 1}
</span>
<div>
<p className="text-sm font-medium">{dept.departmentName}</p>
<p className="text-xs text-muted-foreground">
{dept.totalSubjects} subjects
</p>
</div>
</div>
<Badge>{dept.totalSubjects}</Badge>
</div>
))}
</CardContent>
</Card>
<Card className="hover:shadow-md transition-shadow">
<CardHeader>
<CardTitle>Subjects with Most Classes</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
{topSubjects.map((subject, index) => (
<div
key={subject.subjectId}
className="flex items-center justify-between rounded-md border border-transparent px-3 py-2 transition-colors hover:border-primary/30 hover:bg-muted/40"
>
<div className="flex items-center gap-3">
<span className="text-xs font-semibold text-muted-foreground">
#{index + 1}
</span>
<div>
<p className="text-sm font-medium">{subject.subjectName}</p>
<p className="text-xs text-muted-foreground">
{subject.totalClasses} classes
</p>
</div>
</div>
<Badge>{subject.totalClasses}</Badge>
</div>
))}
</CardContent>
</Card>
</div>
<Separator />
</div>
);
};
export default Dashboard;