import { useState, useEffect } from 'react'; import { supabase } from '@/lib/supabaseClient'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'; import { Badge } from '@/components/ui/badge'; import { Loader2 } from 'lucide-react'; import { format } from 'date-fns'; import { handleError } from '@/lib/errorHandler'; import { AuditLogEntry } from '@/types/database'; interface ProfileChangeField { field_name: string; old_value: string | null; new_value: string | null; } interface ProfileAuditLogWithChanges extends Omit { profile_change_fields?: ProfileChangeField[]; } export function ProfileAuditLog(): React.JSX.Element { const [logs, setLogs] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { void fetchAuditLogs(); }, []); const fetchAuditLogs = async (): Promise => { try { const { data, error } = await supabase .from('profile_audit_log') .select(` *, profiles!user_id(username, display_name), profile_change_fields( field_name, old_value, new_value ) `) .order('created_at', { ascending: false }) .limit(50); if (error) throw error; setLogs((data || []) as ProfileAuditLogWithChanges[]); } catch (error: unknown) { handleError(error, { action: 'Load audit logs' }); } finally { setLoading(false); } }; if (loading) { return ( ); } return ( Profile Audit Log User Action Changes Date {logs.map((log) => ( {(log as { profiles?: { display_name?: string; username?: string } }).profiles?.display_name || (log as { profiles?: { username?: string } }).profiles?.username || 'Unknown'} {log.action} {log.profile_change_fields && log.profile_change_fields.length > 0 ? (
{log.profile_change_fields.map((change, idx) => (
{change.field_name}:{' '} {change.old_value || 'null'} {' → '} {change.new_value || 'null'}
))}
) : ( No changes )}
{format(new Date(log.created_at), 'PPpp')}
))}
); }