import { ChevronDown, ChevronUp, Copy } from "lucide-react"; import * as React from "react"; import { type CommonBadgeVariant, getCommonBadgeClasses, } from "../../../ui/badge"; import { getCommonButtonClasses } from "../../../ui/button"; import { commonStickyTableHeaderClass, commonTableBodyClass, commonTableCellClass, commonTableClass, commonTableHeadClass, commonTableHeaderClass, commonTableRowClass, commonTableShellClass, commonTableViewportClass, commonTableWrapperClass, } from "../../../ui/table"; import type { CommonAuditLog } from "../../audit"; import { formatAuditDateParts, formatAuditValue, parseAuditDetails, resolveAuditAction, resolveAuditActor, resolveAuditTarget, } from "../../audit"; type AuditTranslate = ( key: string, fallback: string, vars?: Record, ) => string; type AuditLogTableProps = { logs: CommonAuditLog[]; t: AuditTranslate; loading: boolean; hasNextPage: boolean; isFetchingNextPage: boolean; onLoadMore: () => void; className?: string; }; function cx(...classNames: Array) { return classNames.filter(Boolean).join(" "); } function statusVariant(status: string): CommonBadgeVariant { return status === "success" || status === "ok" ? "success" : "warning"; } export function AuditLogTable({ logs, t, loading, hasNextPage, isFetchingNextPage, onLoadMore, className, }: AuditLogTableProps) { const [expandedRows, setExpandedRows] = React.useState< Record >({}); const handleCopy = (value: string) => { if (!value) { return; } navigator.clipboard.writeText(value); }; return (
{loading && logs.length === 0 ? ( ) : logs.length === 0 ? ( ) : ( logs.map((row, index) => { const details = parseAuditDetails(row.details); const actorLabel = resolveAuditActor(row, details); const actionLabel = resolveAuditAction(row, details); const targetLabel = resolveAuditTarget(details); const rowKey = `${row.event_id}-${row.timestamp}-${index}`; const expanded = Boolean(expandedRows[rowKey]); const { date, time } = formatAuditDateParts(row.timestamp); return ( {expanded ? ( ) : null} ); }) )}
{t("ui.common.audit.table.time", "Time")} {t("ui.common.audit.table.user_id", "User ID")} {t("ui.common.audit.table.action", "Action")} {t("ui.common.audit.table.client_id", "Client ID")} {t("ui.common.audit.table.status", "Status")}
{t("msg.common.audit.loading", "Loading audit logs...")}
{t("msg.common.audit.empty", "No audit logs found.")}
{date}
{time}
{actorLabel} {actorLabel !== "-" ? ( ) : null}
{actionLabel}
{targetLabel} {targetLabel !== "-" ? ( ) : null}
{row.status}
{t( "ui.common.audit.details.request", "Request", )}
{t( "ui.common.audit.details.request_id", "Request ID · {{value}}", { value: formatAuditValue( details.request_id, ), }, )}
{t( "ui.common.audit.details.event_id", "Event ID · {{value}}", { value: formatAuditValue(row.event_id), }, )}
{t( "ui.common.audit.details.ip", "IP · {{value}}", { value: formatAuditValue(row.ip_address), }, )}
{t( "ui.common.audit.details.method", "Method · {{value}}", { value: formatAuditValue(details.method), }, )}
{t( "ui.common.audit.details.path", "Path · {{value}}", { value: formatAuditValue(details.path), }, )}
{t( "ui.common.audit.details.latency", "Latency · {{value}}", { value: details.latency_ms !== undefined ? `${details.latency_ms}ms` : "-", }, )}
{t("ui.common.audit.details.actor", "Actor")}
{t( "ui.common.audit.details.actor_id", "User ID · {{value}}", { value: actorLabel }, )}
{t( "ui.common.audit.details.tenant", "Tenant · {{value}}", { value: formatAuditValue( details.tenant_id, ), }, )}
{t( "ui.common.audit.details.device", "Device · {{value}}", { value: formatAuditValue(row.device_id), }, )}
{t( "ui.common.audit.details.target", "Client ID · {{value}}", { value: targetLabel }, )}
{t( "ui.common.audit.details.result", "Result", )}
{t( "ui.common.audit.details.error", "Error · {{value}}", { value: formatAuditValue(details.error), }, )}
{t( "ui.common.audit.details.before", "Before · {{value}}", { value: formatAuditValue(details.before), }, )}
{t( "ui.common.audit.details.after", "After · {{value}}", { value: formatAuditValue(details.after), }, )}
{hasNextPage ? ( ) : ( {t("msg.common.audit.end", "End of audit feed")} )}
); }