import { useEffect, useState } from 'react'; import type { ReactElement } from 'react'; import { Loader2 } from 'lucide-react'; import { Modal, SecondaryButton } from '@/components/shared'; import type { AuditLog } from '@/types/audit-log'; // Helper function to format date const formatDate = (dateString: string | null): string => { if (!dateString) return 'N/A'; const date = new Date(dateString); return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', }); }; // Helper function to format JSON const formatJSON = (obj: Record | null): string => { if (!obj) return 'N/A'; return JSON.stringify(obj, null, 2); }; interface ViewAuditLogModalProps { isOpen: boolean; onClose: () => void; auditLogId: string | null; onLoadAuditLog: (id: string) => Promise; } export const ViewAuditLogModal = ({ isOpen, onClose, auditLogId, onLoadAuditLog, }: ViewAuditLogModalProps): ReactElement | null => { const [auditLog, setAuditLog] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); // Load audit log data when modal opens useEffect(() => { if (isOpen && auditLogId) { const loadAuditLog = async (): Promise => { try { setIsLoading(true); setError(null); const data = await onLoadAuditLog(auditLogId); setAuditLog(data); } catch (err: any) { setError(err?.response?.data?.error?.message || 'Failed to load audit log details'); } finally { setIsLoading(false); } }; loadAuditLog(); } else { setAuditLog(null); setError(null); } }, [isOpen, auditLogId, onLoadAuditLog]); return ( Close } >
{isLoading && (
)} {error && (

{error}

)} {!isLoading && !error && auditLog && (
{/* Basic Information */}

Basic Information

{auditLog.action}

{auditLog.resource_type}

{auditLog.resource_id || 'N/A'}

{auditLog.request_method || 'N/A'}

{auditLog.request_path || 'N/A'}

{auditLog.response_status || 'N/A'}

{/* User & Tenant Information */}

User & Tenant Information

{auditLog.user && ( <>

{auditLog.user.first_name} {auditLog.user.last_name}

{auditLog.user.email}

{auditLog.user.id}

)} {auditLog.tenant && ( <>

{auditLog.tenant.name}

{auditLog.tenant.id}

)} {!auditLog.user && !auditLog.tenant && (

No user or tenant information available

)}
{/* Request & Response Information */} {(auditLog.request_body || auditLog.response_body) && (

Request & Response

{auditLog.request_body && (
                        {formatJSON(auditLog.request_body)}
                      
)} {auditLog.response_body && (
                        {formatJSON(auditLog.response_body)}
                      
)}
)} {/* Changes & Metadata */} {(auditLog.changes || auditLog.metadata) && (

Changes & Metadata

{auditLog.changes && (
                        {formatJSON(auditLog.changes)}
                      
)} {auditLog.metadata && (
                        {formatJSON(auditLog.metadata)}
                      
)}
)} {/* Additional Information */}

Additional Information

{auditLog.ip_address || 'N/A'}

{auditLog.user_agent || 'N/A'}

{auditLog.correlation_id || 'N/A'}

{/* Timestamps */}

Timestamps

{formatDate(auditLog.created_at)}

{formatDate(auditLog.updated_at)}

)}
); };