feat: add CSV export to audit log
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { useState, useMemo, useCallback } from 'react';
|
||||
import {
|
||||
Badge, DateRangePicker, Input, Select, MonoText, CodeBlock, DataTable,
|
||||
Badge, Button, DateRangePicker, Input, Select, MonoText, CodeBlock, DataTable,
|
||||
} from '@cameleer/design-system';
|
||||
import type { Column } from '@cameleer/design-system';
|
||||
import { Download } from 'lucide-react';
|
||||
import { useAuditLog, type AuditEvent } from '../../api/queries/admin/audit';
|
||||
import styles from './AuditLogPage.module.css';
|
||||
import tableStyles from '../../styles/table-section.module.css';
|
||||
@@ -17,6 +18,21 @@ const CATEGORIES = [
|
||||
{ value: 'AGENT', label: 'AGENT' },
|
||||
];
|
||||
|
||||
function exportCsv(events: AuditEvent[]) {
|
||||
const headers = ['Timestamp', 'User', 'Category', 'Action', 'Target', 'Result', 'Details'];
|
||||
const rows = events.map(e => [
|
||||
e.timestamp, e.username, e.category, e.action, e.target, e.result, e.details ?? '',
|
||||
]);
|
||||
const csv = [headers, ...rows].map(r => r.map(c => `"${String(c).replace(/"/g, '""')}"`).join(',')).join('\n');
|
||||
const blob = new Blob([csv], { type: 'text/csv' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `cameleer-audit-${new Date().toISOString().slice(0, 16).replace(':', '-')}.csv`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
function formatTimestamp(iso: string): string {
|
||||
return new Date(iso).toLocaleString('en-GB', {
|
||||
year: 'numeric', month: '2-digit', day: '2-digit',
|
||||
@@ -126,6 +142,9 @@ export default function AuditLogPage() {
|
||||
{totalCount} events
|
||||
</span>
|
||||
<Badge label="AUTO" color="success" />
|
||||
<Button variant="ghost" size="sm" onClick={() => exportCsv(data?.items ?? [])}>
|
||||
<Download size={14} /> Export CSV
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<DataTable
|
||||
|
||||
Reference in New Issue
Block a user