"use client"; import { useTranslations } from "next-intl"; import { useState } from "react"; import { DataTable } from "@/components/admin/data-table"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import type { DataTableColumn, PaginatedResult } from "@/types/common"; interface AuditRow { id: number; username: string; action: string; target: string; targetId: number | null; diff: string | null; createdAt: string; } function DiffCell({ diff }: { diff: string | null }) { const t = useTranslations("pages.admin.logs"); const [open, setOpen] = useState(false); if (!diff) return {t("emptyValue")}; let parsed: Record; try { parsed = JSON.parse(diff); } catch { return {t("emptyValue")}; } const keys = Object.keys(parsed); if (keys.length === 0) return {t("emptyValue")}; const summary = keys.length === 1 ? `${keys[0]}: ${String(parsed[keys[0]].from ?? "∅")} → ${String(parsed[keys[0]].to ?? "∅")}` : t("audit.changesCount", { count: keys.length }); return ( setOpen(!open)} > {summary} {open && ( {keys.map((k) => ( {k}: {String(parsed[k].from ?? "∅")} → {String(parsed[k].to ?? "∅")} ))} )} ); } interface AuditTableProps { data: PaginatedResult; } export function AuditTable({ data }: AuditTableProps) { const t = useTranslations("pages.admin.logs"); const columns: DataTableColumn[] = [ { key: "id", label: t("colId"), sortable: true }, { key: "username", label: t("colUser"), sortable: true }, { key: "action", label: t("colAction"), sortable: true, render: (value) => { const v = String(value); let variant: "default" | "secondary" | "destructive" = "secondary"; if (v.includes("create") || v.includes("add")) variant = "default"; if (v.includes("delete") || v.includes("remove") || v.includes("ban")) variant = "destructive"; return {v}; }, }, { key: "target", label: t("colTarget"), sortable: true }, { key: "targetId", label: t("audit.colTargetId"), render: (value) => ( {value != null ? String(value) : t("emptyValue")} ), }, { key: "diff", label: t("audit.colDetails"), render: (_value, row) => , }, { key: "createdAt", label: t("audit.colDate"), sortable: true, render: (value) => ( {String(value) || t("audit.notAvailable")} ), }, ]; return ( ); }