import { count, desc, eq, gt, inArray, or } from "drizzle-orm"; import { Activity, ArrowRight, Ban as BanIcon, Database, Newspaper, Users, } from "lucide-react"; import { getTranslations } from "next-intl/server"; import { StatusCard } from "@/components/admin/dashboard"; import Link from "@/components/link"; import { unixNow } from "@/lib/bans"; import { Ban, db, StaffActivities, User, WebsiteArticles } from "@/lib/db"; import { formatDate } from "@/lib/format-date"; type RankCount = { rank: number; count: number }; export default async function AdminDashboard() { const t = await getTranslations("pages.admin.dashboard"); const now = unixNow(); let users = -1; let online = 0; let articles = 0; let bans = 0; try { const [usersRow, onlineRow, articlesRow, bansRow] = await Promise.all([ db.select({ total: count() }).from(User), db.select({ total: count() }).from(User).where(eq(User.online, "1")), db.select({ total: count() }).from(WebsiteArticles), db .select({ total: count() }) .from(Ban) .where(or(eq(Ban.banExpire, 0), gt(Ban.banExpire, now))), ]); users = usersRow[0]?.total ?? 0; online = onlineRow[0]?.total ?? 0; articles = articlesRow[0]?.total ?? 0; bans = bansRow[0]?.total ?? 0; } catch {} const dbOk = users >= 0; const rankGroups = await db .select({ rank: User.rank, total: count() }) .from(User) .groupBy(User.rank) .orderBy(desc(User.rank)) .catch(() => [] as Array<{ rank: number; total: number }>); const rankCounts: RankCount[] = rankGroups .map((g) => ({ rank: g.rank, count: Number(g.total) })) .sort((a, b) => b.count - a.count); const maxRankCount = rankCounts.reduce((m, r) => Math.max(m, r.count), 0); const activity = await db .select() .from(StaffActivities) .orderBy(desc(StaffActivities.createdAt)) .limit(8) .catch(() => [] as (typeof StaffActivities.$inferSelect)[]); const staffIds = Array.from( new Set(activity.map((a) => Number(a.userId))), ).filter((n) => n > 0); let nameById = new Map(); if (staffIds.length) { const u = await db .select({ id: User.id, username: User.username }) .from(User) .where(inArray(User.id, staffIds)) .catch(() => []); nameById = new Map(u.map((x) => [x.id, x.username])); } return (

{t("title")}

{t("subtitle")}

} /> } /> 0 ? "warn" : "neutral"} icon={} /> } />

{t("usersByRank")}

{rankCounts.length === 0 ? (

{dbOk ? t("noAccountsToBreakDown") : t("databaseDown")}

) : (
{rankCounts.map((r) => { const pct = maxRankCount > 0 ? Math.round((r.count / maxRankCount) * 100) : 0; return (
{t("rankLabel", { rank: r.rank })}
0 ? 4 : 0)}%`, }} />
{r.count}
); })}
)}

{t("recentStaffActivity")}

{t("viewAll")}
{activity.length === 0 ? (

{t("noStaffActivityLogged")}

) : (
{activity.map((a, i) => (
0 ? "border-t border-[var(--admin-border)]" : ""}`} >
{nameById.get(Number(a.userId)) ?? `#${a.userId}`}
{a.description}
{a.action}
{formatDate(a.createdAt)}
))}
)}
); }