diff --git a/app/admin/activity/page.tsx b/app/admin/activity/page.tsx index 1e1cdee..7798583 100644 --- a/app/admin/activity/page.tsx +++ b/app/admin/activity/page.tsx @@ -1,65 +1,20 @@ +import { ClipboardListIcon } from "lucide-react"; import { redirect } from "next/navigation"; import { connection } from "next/server"; -import { ClipboardListIcon } from "lucide-react"; import { AdminHeader } from "@/components/admin/admin-header"; import { AuditLogFilters } from "@/components/admin/audit-log-filters"; -import { Badge } from "@/components/ui/badge"; -import { - Empty, - EmptyDescription, - EmptyHeader, - EmptyMedia, - EmptyTitle, -} from "@/components/ui/empty"; -import { - Pagination, - PaginationContent, - PaginationItem, - PaginationNext, - PaginationPrevious, -} from "@/components/ui/pagination"; -import { - Table, - TableBody, - TableCell, - TableHead, - TableHeader, - TableRow, -} from "@/components/ui/table"; -import { - AUDIT_ACTION_LABELS, - AUDIT_TARGET_LABELS, - AUDIT_TARGET_TYPES, - isAuditTargetType, - listAuditActors, - listAuditLogs, - type AuditAction, -} from "@/lib/audit-log"; +import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle } from "@/components/ui/empty"; +import { Pagination, PaginationContent, PaginationItem, PaginationNext, PaginationPrevious } from "@/components/ui/pagination"; +import { listAuditActors, listAuditLogs } from "@/lib/audit-log"; import { getAdminSession } from "@/lib/auth/server"; -function pageHref( - page: number, - actorId?: string, - targetType?: string, -) { +function pageHref(page: number, actorId?: string) { const query = new URLSearchParams({ page: String(page) }); if (actorId) query.set("actor", actorId); - if (targetType) query.set("type", targetType); return `/admin/activity?${query}`; } -function eventSummary(metadata: Record) { - const label = metadata.sectionTitle - ?? metadata.guideName - ?? metadata.alias - ?? metadata.fileName - ?? metadata.accountLabel; - if (typeof label === "string") return label; - if (typeof metadata.error === "string") return metadata.error; - return null; -} - export default async function AuditLogPage({ searchParams, }: { @@ -67,22 +22,15 @@ export default async function AuditLogPage({ }) { await connection(); if (!(await getAdminSession())) redirect("/admin/login"); + const query = await searchParams; const rawPage = Array.isArray(query.page) ? query.page[0] : query.page; const parsedPage = Number(rawPage); - const page = Number.isSafeInteger(parsedPage) && parsedPage > 0 - ? parsedPage - : 1; + const page = Number.isSafeInteger(parsedPage) && parsedPage > 0 ? parsedPage : 1; const rawActorId = Array.isArray(query.actor) ? query.actor[0] : query.actor; - const actorId = rawActorId && rawActorId.length <= 128 - ? rawActorId - : undefined; - const rawTargetType = Array.isArray(query.type) ? query.type[0] : query.type; - const targetType = isAuditTargetType(rawTargetType) - ? rawTargetType - : undefined; + const actorId = rawActorId && rawActorId.length <= 128 ? rawActorId : undefined; const [{ events, pageCount, total }, actors] = await Promise.all([ - listAuditLogs({ page, pageSize: 50, actorId, targetType }), + listAuditLogs({ page, pageSize: 100, actorId }), listAuditActors(), ]); const dateTime = new Intl.DateTimeFormat("th-TH", { @@ -94,99 +42,47 @@ export default async function AuditLogPage({ return (
-
+
-

Admin audit trail

-

Activity

+

Admin audit log

+

Logs

- ประวัติการเปลี่ยนแปลงที่บันทึกสำเร็จทั้งหมด {total.toLocaleString("th-TH")} รายการ + บันทึกการทำงานของผู้ดูแล {total.toLocaleString("th-TH")} รายการ

- ({ - value, - label: AUDIT_TARGET_LABELS[value], - }))} - initialActorId={actorId} - initialTargetType={targetType} - /> + {events.length ? ( <> -
- - - - เวลา - ผู้ดูแล - การทำงาน - เป้าหมาย - รายละเอียด - - - - {events.map((event) => { - const action = event.action as AuditAction; - const summary = eventSummary(event.metadata); - return ( - - {dateTime.format(event.createdAt)} - {event.actorLabel} - -
- {AUDIT_ACTION_LABELS[action] ?? event.action} - {event.metadata.overwrite === true ? ( - Overwrite - ) : null} -
-
- -
- - {isAuditTargetType(event.targetType) - ? AUDIT_TARGET_LABELS[event.targetType] - : event.targetType} - - - {event.targetId} - -
-
- -
- {summary ? {summary} : null} - - {event.scope ? `${event.scope} · ` : ""} - {event.resultingVersion - ? `v${event.resultingVersion}` - : ""} - -
-
-
- ); - })} -
-
+
+ {events.map((event) => ( +
+
+ {event.author ?? "System"} + {event.text} + +
+ {event.details && Object.keys(event.details).length ? ( +
+ รายละเอียด +
+                        {JSON.stringify(event.details, null, 2)}
+                      
+
+ ) : null} +
+ ))}
{pageCount > 1 ? ( {page > 1 ? ( - + ) : null} {page < pageCount ? ( - + ) : null} @@ -196,13 +92,9 @@ export default async function AuditLogPage({ ) : ( - - - - ยังไม่มีกิจกรรม - - ลองเปลี่ยนตัวกรอง หรือกลับมาหลังจากมีการแก้ไขข้อมูล - + + ยังไม่มี Log + Log จะแสดงหลังจากผู้ดูแลแก้ไขข้อมูล )} diff --git a/components/admin/audit-log-filters.tsx b/components/admin/audit-log-filters.tsx index 8989d8b..1676a98 100644 --- a/components/admin/audit-log-filters.tsx +++ b/components/admin/audit-log-filters.tsx @@ -19,23 +19,17 @@ const ALL = "all"; export function AuditLogFilters({ actors, - targetTypes, initialActorId, - initialTargetType, }: { actors: Array<{ id: string; label: string }>; - targetTypes: Array<{ value: string; label: string }>; initialActorId?: string; - initialTargetType?: string; }) { const router = useRouter(); const [actorId, setActorId] = useState(initialActorId ?? ALL); - const [targetType, setTargetType] = useState(initialTargetType ?? ALL); function applyFilters() { const query = new URLSearchParams(); if (actorId !== ALL) query.set("actor", actorId); - if (targetType !== ALL) query.set("type", targetType); const suffix = query.toString(); router.push(suffix ? `/admin/activity?${suffix}` : "/admin/activity"); } @@ -60,24 +54,6 @@ export function AuditLogFilters({ - - ประเภทเป้าหมาย - -