审计导出功能--初版
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { auditExportColumns } from "./auditExportColumns";
|
||||
import { formatAuditRows } from "./auditExportFormatter";
|
||||
import type { AuditEvent } from "..";
|
||||
|
||||
const BOM = "\ufeff";
|
||||
|
||||
const escapeCsv = (val: string) => {
|
||||
const safe = (val ?? "").replace(/"/g, '""');
|
||||
return `"${safe}"`;
|
||||
};
|
||||
|
||||
const buildCsv = (rows: Record<string, string>[]) => {
|
||||
const header = auditExportColumns.map((c) => escapeCsv(c.label)).join(",");
|
||||
const body = rows.map((row) => auditExportColumns.map((c) => escapeCsv(row[c.key] || "")).join(",")).join("\n");
|
||||
return `${BOM}${header}\n${body}`;
|
||||
};
|
||||
|
||||
export interface AuditExportOptions {
|
||||
fileName: string;
|
||||
}
|
||||
|
||||
export const exportAuditCsv = (events: AuditEvent[], options: AuditExportOptions) => {
|
||||
const rows = formatAuditRows(events);
|
||||
const csv = buildCsv(rows);
|
||||
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
|
||||
const link = document.createElement("a");
|
||||
link.href = URL.createObjectURL(blob);
|
||||
link.download = options.fileName.endsWith(".csv") ? options.fileName : `${options.fileName}.csv`;
|
||||
link.click();
|
||||
URL.revokeObjectURL(link.href);
|
||||
};
|
||||
Reference in New Issue
Block a user