c_agri_trade/project_code/frontend/src/features/tenant/AuditLogView.tsx

190 lines
8.3 KiB
TypeScript

import React, { useEffect, useState } from 'react';
import { tenantStore } from '../../stores/tenantStore';
import { apiClient } from '../../services/api';
import { StatusPill } from '../../components/ui/StatusPill';
import { Button } from '../../components/ui/Button';
import { Eye } from 'lucide-react';
/**
* Immutable Transaction Audit Log Viewer Screen.
*
* Fulfills FR-TENANTSECURITYAUDIT-003 (UC-017 / SCREEN-TENANT-004).
* Styled per DESIGN.md: AgriTrade Mercantile (Tactile Corporate Minimalism).
*/
export interface AuditEvent {
audit_id: string;
tenant_id: string;
actor_id: string;
entity_type: string;
entity_id: string;
action: 'CREATE' | 'UPDATE' | 'DELETE' | 'STATUS_CHANGE';
timestamp: string;
correlation_id?: string;
field_diff?: Record<string, any>;
}
export const AuditLogView: React.FC = () => {
const [activeTenantId, setActiveTenantId] = useState<string | null>(tenantStore.getState().activeTenantId);
const [activeBusinessName, setActiveBusinessName] = useState<string | null>(tenantStore.getState().activeBusinessName);
const [events, setEvents] = useState<AuditEvent[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
// Diff inspection drawer
const [selectedEvent, setSelectedEvent] = useState<AuditEvent | null>(null);
useEffect(() => {
const unsub = tenantStore.subscribe(() => {
const state = tenantStore.getState();
setActiveTenantId(state.activeTenantId);
setActiveBusinessName(state.activeBusinessName);
});
return () => unsub();
}, []);
useEffect(() => {
if (activeTenantId) {
fetchAuditLogs(activeTenantId);
}
}, [activeTenantId]);
const fetchAuditLogs = async (tenantId: string) => {
setLoading(true);
setError(null);
const resp = await apiClient<{ events: AuditEvent[] }>(`/api/v1/audit/events?tenant_id=${encodeURIComponent(tenantId)}`);
setLoading(false);
if (resp.status === 200 && resp.data?.events) {
setEvents(resp.data.events);
} else {
setError(resp.error || 'Failed to fetch audit log trail.');
}
};
const getActionStatus = (action: string): 'normal' | 'watch' | 'danger' | 'neutral' => {
switch (action) {
case 'CREATE': return 'normal';
case 'UPDATE': return 'watch';
case 'DELETE': return 'danger';
case 'STATUS_CHANGE': return 'watch';
default: return 'neutral';
}
};
return (
<div className="space-y-6" data-testid="audit-log-view">
<div className="flex items-center justify-between">
<div>
<h2 className="text-xl font-bold tracking-tight text-text-primary">Transaction Audit Trail</h2>
<p className="mt-0.5 text-xs text-text-muted font-mono">
Immutable, append-only mutation ledger for <strong>{activeBusinessName || 'Active Enterprise'}</strong> (FR-TENANTSECURITYAUDIT-003)
</p>
</div>
<span className="inline-flex items-center px-2.5 py-1 rounded-full text-[10px] font-mono font-medium bg-status-normal-bg text-status-normal-text border border-status-normal-text/20">
Append-Only Statutory Ledger
</span>
</div>
{error && (
<div className="bg-status-watch-bg border-l-[3px] border-accent-ochre p-3 rounded text-status-watch-text text-xs font-mono">
{error}
</div>
)}
{loading && (
<div className="text-xs text-text-muted font-mono">
Refreshing audit log ledger...
</div>
)}
{/* Audit Log Table adhering to DESIGN.md */}
<div className="bg-card-surface border border-card-border rounded shadow-none overflow-hidden">
<table className="min-w-full divide-y divide-card-border">
<thead className="bg-surface-low">
<tr>
<th className="py-3 px-4 text-left text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Timestamp (UTC)</th>
<th className="py-3 px-4 text-left text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Action</th>
<th className="py-3 px-4 text-left text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Entity Type</th>
<th className="py-3 px-4 text-left text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Entity ID</th>
<th className="py-3 px-4 text-left text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Actor Subject</th>
<th className="py-3 px-4 text-right text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Payload Diff</th>
</tr>
</thead>
<tbody className="divide-y divide-card-border bg-white text-xs">
{events.length === 0 ? (
<tr>
<td colSpan={6} className="py-8 text-center text-xs text-text-muted font-mono">
No governed audit events recorded for this enterprise.
</td>
</tr>
) : (
events.map((e) => (
<tr key={e.audit_id} className="hover:bg-surface-bright transition-colors">
<td className="py-3.5 px-4 font-mono text-text-muted">
{new Date(e.timestamp).toISOString().replace('T', ' ').slice(0, 19)}
</td>
<td className="py-3.5 px-4">
<StatusPill
status={getActionStatus(e.action)}
label={e.action}
/>
</td>
<td className="py-3.5 px-4 font-medium text-text-primary">{e.entity_type}</td>
<td className="py-3.5 px-4 font-mono text-text-muted">{e.entity_id}</td>
<td className="py-3.5 px-4 font-mono text-text-muted">{e.actor_id}</td>
<td className="py-3.5 px-4 text-right font-mono">
<button
onClick={() => setSelectedEvent(e)}
className="inline-flex items-center space-x-1 text-xs text-text-primary hover:text-accent-ochre font-medium underline underline-offset-2 transition"
>
<Eye className="w-3 h-3" />
<span>Inspect</span>
</button>
</td>
</tr>
))
)}
</tbody>
</table>
</div>
{/* JSON Diff Inspection Modal */}
{selectedEvent && (
<div className="fixed z-50 inset-0 overflow-y-auto">
<div className="flex items-center justify-center min-h-screen px-4 text-center sm:p-0">
<div className="fixed inset-0 bg-sidebar-surface/40 transition-opacity" onClick={() => setSelectedEvent(null)} />
<div className="inline-block bg-card-surface rounded border border-card-border p-6 text-left overflow-hidden shadow-tactile transform transition-all sm:max-w-xl sm:w-full z-10">
<div className="flex items-center justify-between pb-3 border-b border-card-border">
<div>
<h3 className="text-sm font-bold text-text-primary tracking-tight">
Mutation Diff: {selectedEvent.entity_type}
</h3>
<p className="text-[11px] text-text-muted font-mono mt-0.5">
Actor: <span className="font-semibold text-text-primary">{selectedEvent.actor_id}</span> | {selectedEvent.timestamp}
</p>
</div>
<StatusPill status={getActionStatus(selectedEvent.action)} label={selectedEvent.action} />
</div>
<div className="mt-4 bg-surface-low rounded border border-card-border p-3 overflow-x-auto text-text-primary font-mono text-xs">
<pre className="text-[11px] leading-relaxed">{JSON.stringify(selectedEvent.field_diff, null, 2)}</pre>
</div>
<div className="mt-4 pt-3 border-t border-card-border flex justify-end">
<Button
type="button"
variant="secondary"
size="sm"
onClick={() => setSelectedEvent(null)}
className="font-mono"
>
Close Inspection
</Button>
</div>
</div>
</div>
</div>
)}
</div>
);
};