The table caches accessor values per row, so a code looked up before the companies list arrived stayed blank; render it in the cell instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
132 lines
6.9 KiB
TypeScript
132 lines
6.9 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import Link from "next/link";
|
|
import type { ColumnDef } from "@tanstack/react-table";
|
|
import { Badge } from "@/app/components/ui/badge";
|
|
import { FilterField, FilterPanel, FilterSearch, FilterSelect } from "@/app/components/ui/filter-panel";
|
|
import { DataTable } from "@/app/components/ui/data-table";
|
|
import { DatePicker } from "@/app/components/ui/date-picker";
|
|
import { PageHeader } from "@/app/components/ui/page-header";
|
|
import { EmptyState, ErrorState } from "@/app/components/ui/states";
|
|
import { RequireModule } from "@/app/lib/RequireModule";
|
|
import { useListFilters } from "@/app/lib/hooks/useListFilters";
|
|
import { useResetPageOnCompany } from "@/app/lib/hooks/useResetPageOnCompany";
|
|
import { useFitPageSize, MIN_PAGE_SIZE } from "@/app/lib/hooks/useFitPageSize";
|
|
import { ROUTE_MODULES } from "@/app/lib/routeModules";
|
|
import { formatEmailDate as formatDate } from "@/app/lib/services/adminEmails";
|
|
import { searchAuditLog, type AuditEvent } from "@/app/lib/services/auditLog";
|
|
import { useCompanyScope } from "@/app/lib/CompanyScope";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
|
|
const DEFAULTS = { actor: "", module: "", action: "", outcome: "", from: "", to: "" };
|
|
// The backend stores the handler's @AdminModule code, so the filter lists those: the route modules plus the lead edit/export codes. Audit-log reads are never recorded.
|
|
const MODULES = [...Object.values(ROUTE_MODULES).filter((m) => m !== "BO_AUDIT_LOG"), "BO_LEADS_EDIT", "BO_LEADS_EXPORT"];
|
|
|
|
function AuditLogContent() {
|
|
const t = useT();
|
|
const { companyId, showCompanyColumn, codeOf } = useCompanyScope();
|
|
const { tableRef, pageSize } = useFitPageSize();
|
|
const { filters, page, set, setPage, clear, activeCount } = useListFilters(DEFAULTS);
|
|
useResetPageOnCompany(setPage);
|
|
const [data, setData] = useState<{ items: AuditEvent[]; total: number }>({ items: [], total: 0 });
|
|
const [loading, setLoading] = useState(true);
|
|
const [failed, setFailed] = useState(false);
|
|
const [reloadKey, setReloadKey] = useState(0);
|
|
const refetch = useCallback(() => setReloadKey((k) => k + 1), []);
|
|
|
|
useEffect(() => {
|
|
if (pageSize == null) return;
|
|
let cancelled = false;
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect -- resets loading/error before the filter/page/reloadKey-triggered fetch below, which is the actual synchronization.
|
|
setLoading(true);
|
|
setFailed(false);
|
|
searchAuditLog({ ...filters, page, pageSize, companyId })
|
|
.then((result) => { if (!cancelled) setData(result); })
|
|
.catch(() => { if (!cancelled) setFailed(true); })
|
|
.finally(() => { if (!cancelled) setLoading(false); });
|
|
return () => { cancelled = true; };
|
|
}, [filters, page, reloadKey, pageSize, companyId]);
|
|
|
|
const moduleOptions = useMemo(() => MODULES.map((m) => ({ value: m, label: t(m) })), [t]);
|
|
const outcomeOptions = useMemo(() => [{ value: "SUCCESS", label: t("AuditSuccess") }, { value: "FAILURE", label: t("AuditFailure") }], [t]);
|
|
const cut = (text: string | null | undefined, max = "max-w-40") => <span className={`block truncate ${max}`} title={text ?? undefined}>{text}</span>;
|
|
const columns = useMemo<ColumnDef<AuditEvent, unknown>[]>(
|
|
() => [
|
|
...(showCompanyColumn ? [{ id: "company", cell: ({ row }) => codeOf(row.original.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<AuditEvent, unknown>] : []),
|
|
{ id: "time", header: t("LsTime"), enableSorting: false, cell: ({ row }) => <span className="whitespace-nowrap">{formatDate(row.original.occurredAt)}</span> },
|
|
{ id: "actor", header: t("AuditWho"), enableSorting: false, cell: ({ row }) => cut(row.original.actor, "max-w-32") },
|
|
{ id: "module", header: t("AuditModule"), enableSorting: false, cell: ({ row }) => (row.original.module ? t(row.original.module) : "—") },
|
|
{
|
|
id: "action",
|
|
header: t("AuditAction"),
|
|
enableSorting: false,
|
|
cell: ({ row }) => (
|
|
<Link href={`/audit-log/${row.original.id}`} title={row.original.action} className="block max-w-48 truncate font-medium text-primary underline-offset-4 hover:underline focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none">
|
|
{row.original.action}
|
|
</Link>
|
|
),
|
|
},
|
|
{ id: "target", header: t("AuditTarget"), enableSorting: false, cell: ({ row }) => cut(row.original.targetId, "max-w-28") },
|
|
{
|
|
id: "outcome",
|
|
header: t("AuditOutcome"),
|
|
enableSorting: false,
|
|
cell: ({ row }) => (
|
|
<Badge variant={row.original.outcome === "SUCCESS" ? "positive" : "negative"}>{row.original.outcome} {row.original.httpStatus}</Badge>
|
|
),
|
|
},
|
|
{ accessorKey: "clientIp", header: t("LsIp"), enableSorting: false },
|
|
],
|
|
[t, showCompanyColumn, codeOf],
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<PageHeader title={t("BO_AUDIT_LOG")} />
|
|
{failed && <ErrorState message={t("AuditLoadFailed")} onRetry={refetch} className="mb-4" />}
|
|
<FilterPanel activeCount={activeCount} onClear={clear}>
|
|
<FilterField label={t("AuditWho")}>
|
|
<FilterSearch className="w-full" aria-label={t("AuditWho")} value={filters.actor} onChange={(actor) => set({ actor })} />
|
|
</FilterField>
|
|
<FilterField label={t("AuditModule")}>
|
|
<FilterSelect className="w-full" aria-label={t("AuditModule")} allLabel={t("AuditAllModules")} options={moduleOptions} value={filters.module} onChange={(module) => set({ module })} />
|
|
</FilterField>
|
|
<FilterField label={t("AuditAction")}>
|
|
<FilterSearch className="w-full" aria-label={t("AuditAction")} value={filters.action} onChange={(action) => set({ action })} />
|
|
</FilterField>
|
|
<FilterField label={t("AuditOutcome")}>
|
|
<FilterSelect className="w-full" aria-label={t("AuditOutcome")} allLabel={t("AuditAllOutcomes")} options={outcomeOptions} value={filters.outcome} onChange={(outcome) => set({ outcome })} />
|
|
</FilterField>
|
|
<FilterField label={t("LsFrom")}>
|
|
<DatePicker aria-label={t("LsFrom")} value={filters.from} onChange={(from) => set({ from })} max={filters.to || undefined} />
|
|
</FilterField>
|
|
<FilterField label={t("LsTo")}>
|
|
<DatePicker aria-label={t("LsTo")} value={filters.to} onChange={(to) => set({ to })} min={filters.from || undefined} />
|
|
</FilterField>
|
|
</FilterPanel>
|
|
<DataTable
|
|
label={t("BO_AUDIT_LOG")}
|
|
columns={columns}
|
|
data={data.items}
|
|
total={data.total}
|
|
page={page}
|
|
pageSize={pageSize ?? MIN_PAGE_SIZE}
|
|
tableRef={tableRef}
|
|
onPageChange={setPage}
|
|
isLoading={loading}
|
|
rowKey={(r) => String(r.id)}
|
|
empty={<EmptyState title={activeCount > 0 ? t("AuditNoMatch") : t("AuditNoEvents")} />}
|
|
/>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default function AuditLogPage() {
|
|
return (
|
|
<RequireModule pathname="/audit-log">
|
|
<AuditLogContent />
|
|
</RequireModule>
|
|
);
|
|
}
|