import { getTranslations } from "next-intl/server"; import { ScrollText } from "lucide-react"; import { StatusCard } from "@/components/admin/dashboard"; import { prisma } from "@/lib/prisma"; import { buildStaffActivityWhere } from "@/lib/admin/log-filters"; export const dynamic = "force-dynamic"; const PER_PAGE = 25; function fromDate(d: Date | null | undefined): string { return d ? d.toISOString().slice(0, 16).replace("T", " ") : "โ€”"; } export default async function AdminStaffActivities({ searchParams, }: { searchParams: Promise<{ q?: string; page?: string; staffId?: string; action?: string; authorization?: string }>; }) { const t = await getTranslations("pages.admin.logs"); const sp = await searchParams; const q = (sp.q ?? "").trim(); const page = Math.max(1, Number(sp.page ?? "1") || 1); const staffId = sp.staffId ? Number(sp.staffId) : null; const action = sp.action ?? null; const authorizationOnly = sp.authorization === "1"; const whereClause = buildStaffActivityWhere({ q, staffId, action, authorizationOnly }); const activities = await prisma.staffActivities .findMany({ where: whereClause, select: { id: true, userId: true, action: true, description: true, targetType: true, targetId: true, ipAddress: true, createdAt: true, }, orderBy: { createdAt: "desc" }, skip: (page - 1) * PER_PAGE, take: PER_PAGE, }) .catch(() => []); const staffIds = Array.from(new Set(activities.map((a) => Number(a.userId)))).filter((n) => n > 0); let nameById = new Map(); if (staffIds.length) { const users = await prisma.user .findMany({ where: { id: { in: staffIds } }, select: { id: true, username: true } }) .catch(() => []); nameById = new Map(users.map((x) => [x.id, x.username])); } const total = await prisma.staffActivities.count({ where: whereClause }).catch(() => 0); const pages = Math.max(1, Math.ceil(total / PER_PAGE)); const onlineCount = activities.filter( (a) => a.createdAt && new Date(a.createdAt).getTime() > Date.now() - 60000, ).length; return (

{t("title")}

{t("subtitle")}

0 ? "ok" : "neutral"} icon="๐ŸŸข" />
{(q || staffId || action || authorizationOnly) && ( {t("clearFilters")} )}

{t("activities", { count: total })}

{activities.length === 0 ? (
{t("noResults")}
) : (
{activities.map((a) => ( ))}
{t("colWhen")} {t("colStaff")} {t("colAction")} {t("colDescription")} {t("colTarget")} {t("colIp")}
{fromDate(a.createdAt)} {t("staffWithId", { id: Number(a.userId), name: nameById.get(Number(a.userId)) ?? "unknown", })} {a.action} {a.description} {a.targetType && a.targetId ? ( {a.targetType === "user" ? t("userPrefix") : ""}#{String(a.targetId)} ) : ( โ€” )} {a.ipAddress || "โ€”"}
)}

{t("pagination", { page, pages, total })} {page < pages ? ( <> {" ยท "} {t("next")} โ†’ ) : null}

); }