import React, { useEffect, useState } from 'react'; import { auditLogsApi } from '../api/client'; import { format } from 'date-fns'; function DiffView({ changes }) { const { before, after } = changes || {}; if (!before && after) { return Created: {JSON.stringify(after, null, 2)}; } if (before && !after) { return Deleted record; } if (before && after) { const keys = Object.keys({ ...before, ...after }).filter( (k) => JSON.stringify(before[k]) !== JSON.stringify(after[k]) ); if (keys.length === 0) return No field changes detected; return (
{keys.map((k) => (
{k}
{String(before[k] ?? '')}
{String(after[k] ?? '')}
))}
); } return null; } const ACTION_BADGE = { CREATE: 'bg-green-100 text-green-800', UPDATE: 'bg-yellow-100 text-yellow-800', DELETE: 'bg-red-100 text-red-800', }; export default function AuditLogSection({ tableName, recordId, limit = 50 }) { const [logs, setLogs] = useState([]); const [total, setTotal] = useState(0); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [expanded, setExpanded] = useState(null); useEffect(() => { setLoading(true); auditLogsApi .list({ tableName, recordId, limit }) .then(({ logs, total }) => { setLogs(logs); setTotal(total); }) .catch((e) => setError(e.message)) .finally(() => setLoading(false)); }, [tableName, recordId, limit]); return (

Modification History {total > 0 && ( ({total} entries) )}

{loading &&

Loading history…

} {error &&

{error}

} {!loading && !error && logs.length === 0 && (

No modification history yet.

)} {logs.length > 0 && (
{logs.slice(0, limit).map((log) => (
{expanded === log.id && (
)}
))}
)} {total > limit && (

Showing {limit} of {total} entries — open the daily status page to see the full history.

)}
); }