"use client"; import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { getAdminAuditLogs } from "@/api/admin-audit"; import { AdminListPaginationFooter } from "@/components/admin/admin-list-pagination-footer"; import { AdminTableExportButton } from "@/components/admin/admin-table-export-button"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { useAdminDateTimeFormatter } from "@/hooks/use-admin-datetime-formatter"; import { LotteryApiBizError } from "@/types/api/errors"; import type { AdminAuditLogListData } from "@/types/api/admin-audit"; export function AuditLogsConsole(): React.ReactElement { const { t } = useTranslation(["audit", "common"]); const formatTs = useAdminDateTimeFormatter(); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [err, setErr] = useState(null); const [page, setPage] = useState(1); const [perPage, setPerPage] = useState(25); const [moduleCode, setModuleCode] = useState(""); const [actionCode, setActionCode] = useState(""); const [operatorType, setOperatorType] = useState(""); const [appliedModule, setAppliedModule] = useState(""); const [appliedAction, setAppliedAction] = useState(""); const [appliedOpType, setAppliedOpType] = useState(""); const load = useCallback(async () => { setLoading(true); setErr(null); try { const d = await getAdminAuditLogs({ page, per_page: perPage, module_code: appliedModule.trim() || undefined, action_code: appliedAction.trim() || undefined, operator_type: appliedOpType.trim() || undefined, }); setData(d); } catch (e) { setErr(e instanceof LotteryApiBizError ? e.message : t("errors.loadFailed", { ns: "common" })); setData(null); } finally { setLoading(false); } }, [page, perPage, appliedModule, appliedAction, appliedOpType, t]); useEffect(() => { queueMicrotask(() => { void load(); }); }, [load]); const meta = data?.meta; return ( {t("title")}
setModuleCode(e.target.value)} placeholder={t("exactMatch")} className="w-full" />
setActionCode(e.target.value)} placeholder={t("exactMatch")} className="w-full" />
setOperatorType(e.target.value)} placeholder={t("operatorTypePlaceholder")} className="w-full" />
{err ?

{err}

: null} {loading && !data ? (

{t("states.loading", { ns: "common" })}

) : null} {data ? ( <>
{t("table.id", { ns: "common" })} {t("operator")} {t("module")} {t("action")} {t("target")} {t("time")} {data.items.length === 0 ? ( {t("empty")} ) : ( data.items.map((row) => ( {row.id} {row.operator_type}:{row.operator_id} {row.module_code} {row.action_code} {row.target_type ?? "—"} {row.target_id ?? ""} {formatTs(row.created_at)} )) )}
{meta ? ( { setPerPage(n); setPage(1); }} onPageChange={setPage} /> ) : null} ) : null}
); }