|
| 1 | +import { Head, Link, useForm } from '@inertiajs/react'; |
| 2 | +import AppLayout from '@/Layouts/AppLayout'; |
| 3 | +import type { PageProps } from '@/types'; |
| 4 | +import type { Paginator } from '@/types/inventory'; |
| 5 | +import type { AuditLog } from '@/types/audit'; |
| 6 | + |
| 7 | +interface Props extends PageProps { |
| 8 | + logs: Paginator<AuditLog>; |
| 9 | + filters: { |
| 10 | + event?: string; |
| 11 | + user_id?: string; |
| 12 | + auditable_type?: string; |
| 13 | + tenant_id?: string; |
| 14 | + date_from?: string; |
| 15 | + date_to?: string; |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +const EVENT_COLORS: Record<string, string> = { |
| 20 | + created: 'bg-green-100 text-green-700', |
| 21 | + updated: 'bg-blue-100 text-blue-700', |
| 22 | + deleted: 'bg-red-100 text-red-700', |
| 23 | + login: 'bg-slate-100 text-slate-700', |
| 24 | + logout: 'bg-slate-100 text-slate-700', |
| 25 | +}; |
| 26 | + |
| 27 | +function shortType(type: string | null): string { |
| 28 | + if (!type) return '—'; |
| 29 | + return type.split('\\').pop() ?? type; |
| 30 | +} |
| 31 | + |
| 32 | +export default function AuditLogsIndex({ logs, filters }: Props) { |
| 33 | + const { data, setData, get } = useForm({ |
| 34 | + event: filters.event ?? '', |
| 35 | + auditable_type: filters.auditable_type ?? '', |
| 36 | + date_from: filters.date_from ?? '', |
| 37 | + date_to: filters.date_to ?? '', |
| 38 | + }); |
| 39 | + |
| 40 | + function applyFilter(e: React.FormEvent) { |
| 41 | + e.preventDefault(); |
| 42 | + get('/audit-logs'); |
| 43 | + } |
| 44 | + |
| 45 | + const hasFilters = Object.values(filters).some(Boolean); |
| 46 | + |
| 47 | + return ( |
| 48 | + <AppLayout> |
| 49 | + <Head title="Audit Log" /> |
| 50 | + <div className="space-y-6"> |
| 51 | + <div> |
| 52 | + <h1 className="text-2xl font-semibold text-slate-900">Audit Log</h1> |
| 53 | + <p className="text-sm text-slate-500 mt-1">System-wide activity trail — {logs.total} entries</p> |
| 54 | + </div> |
| 55 | + |
| 56 | + <form onSubmit={applyFilter} className="flex flex-wrap items-end gap-3 rounded-lg border border-slate-200 bg-white p-4 shadow-sm"> |
| 57 | + <div> |
| 58 | + <label className="block text-xs font-medium text-slate-500 mb-1">Event</label> |
| 59 | + <select value={data.event} onChange={(e) => setData('event', e.target.value)} |
| 60 | + className="rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none"> |
| 61 | + <option value="">All events</option> |
| 62 | + <option value="created">Created</option> |
| 63 | + <option value="updated">Updated</option> |
| 64 | + <option value="deleted">Deleted</option> |
| 65 | + <option value="login">Login</option> |
| 66 | + </select> |
| 67 | + </div> |
| 68 | + <div> |
| 69 | + <label className="block text-xs font-medium text-slate-500 mb-1">Model Type</label> |
| 70 | + <input type="text" placeholder="e.g. Invoice" value={data.auditable_type} |
| 71 | + onChange={(e) => setData('auditable_type', e.target.value)} |
| 72 | + className="rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none w-36" /> |
| 73 | + </div> |
| 74 | + <div> |
| 75 | + <label className="block text-xs font-medium text-slate-500 mb-1">From</label> |
| 76 | + <input type="date" value={data.date_from} onChange={(e) => setData('date_from', e.target.value)} |
| 77 | + className="rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none" /> |
| 78 | + </div> |
| 79 | + <div> |
| 80 | + <label className="block text-xs font-medium text-slate-500 mb-1">To</label> |
| 81 | + <input type="date" value={data.date_to} onChange={(e) => setData('date_to', e.target.value)} |
| 82 | + className="rounded-md border border-slate-300 px-3 py-1.5 text-sm focus:border-indigo-500 focus:outline-none" /> |
| 83 | + </div> |
| 84 | + <button type="submit" |
| 85 | + className="rounded-md bg-indigo-600 px-4 py-1.5 text-sm font-medium text-white hover:bg-indigo-700"> |
| 86 | + Filter |
| 87 | + </button> |
| 88 | + {hasFilters && ( |
| 89 | + <Link href="/audit-logs" className="text-sm text-slate-500 hover:text-slate-700">Clear</Link> |
| 90 | + )} |
| 91 | + </form> |
| 92 | + |
| 93 | + <div className="rounded-lg border border-slate-200 bg-white shadow-sm overflow-hidden"> |
| 94 | + <table className="w-full text-sm"> |
| 95 | + <thead className="bg-slate-50 text-xs text-slate-500 uppercase border-b border-slate-200"> |
| 96 | + <tr> |
| 97 | + <th className="px-4 py-2 text-left font-medium">Time</th> |
| 98 | + <th className="px-4 py-2 text-left font-medium">User</th> |
| 99 | + <th className="px-4 py-2 text-left font-medium">Event</th> |
| 100 | + <th className="px-4 py-2 text-left font-medium">Model</th> |
| 101 | + <th className="px-4 py-2 text-left font-medium">ID</th> |
| 102 | + <th className="px-4 py-2 text-left font-medium">Changes</th> |
| 103 | + <th className="px-4 py-2 text-left font-medium">IP</th> |
| 104 | + </tr> |
| 105 | + </thead> |
| 106 | + <tbody className="divide-y divide-slate-50"> |
| 107 | + {logs.data.length === 0 ? ( |
| 108 | + <tr><td colSpan={7} className="px-4 py-8 text-center text-slate-400">No audit entries found.</td></tr> |
| 109 | + ) : logs.data.map((entry) => ( |
| 110 | + <tr key={entry.id} className="hover:bg-slate-50 align-top"> |
| 111 | + <td className="px-4 py-3 text-slate-500 whitespace-nowrap text-xs"> |
| 112 | + {new Date(entry.created_at).toLocaleString()} |
| 113 | + </td> |
| 114 | + <td className="px-4 py-3 font-medium text-slate-800"> |
| 115 | + {entry.user?.name ?? <span className="text-slate-400">System</span>} |
| 116 | + </td> |
| 117 | + <td className="px-4 py-3"> |
| 118 | + <span className={`inline-flex rounded-full px-2 py-0.5 text-xs font-medium ${EVENT_COLORS[entry.event] ?? 'bg-slate-100 text-slate-600'}`}> |
| 119 | + {entry.event} |
| 120 | + </span> |
| 121 | + </td> |
| 122 | + <td className="px-4 py-3 text-slate-700">{shortType(entry.auditable_type)}</td> |
| 123 | + <td className="px-4 py-3 text-slate-500"> |
| 124 | + {entry.auditable_id ? `#${entry.auditable_id}` : '—'} |
| 125 | + </td> |
| 126 | + <td className="px-4 py-3 max-w-xs"> |
| 127 | + {(entry.new_values && Object.keys(entry.new_values).length > 0) ? ( |
| 128 | + <details className="text-xs"> |
| 129 | + <summary className="cursor-pointer text-indigo-600 hover:underline">View changes</summary> |
| 130 | + <pre className="mt-1 bg-slate-50 rounded p-2 text-slate-600 overflow-x-auto text-xs max-h-32"> |
| 131 | + {JSON.stringify({ old: entry.old_values, new: entry.new_values }, null, 2)} |
| 132 | + </pre> |
| 133 | + </details> |
| 134 | + ) : '—'} |
| 135 | + </td> |
| 136 | + <td className="px-4 py-3 text-xs text-slate-400">{entry.ip_address ?? '—'}</td> |
| 137 | + </tr> |
| 138 | + ))} |
| 139 | + </tbody> |
| 140 | + </table> |
| 141 | + </div> |
| 142 | + |
| 143 | + {logs.last_page > 1 && ( |
| 144 | + <div className="flex justify-center gap-2"> |
| 145 | + {logs.prev_page_url && ( |
| 146 | + <Link href={logs.prev_page_url} className="rounded-md border border-slate-300 px-3 py-1.5 text-sm text-slate-600 hover:bg-slate-50"> |
| 147 | + ← Previous |
| 148 | + </Link> |
| 149 | + )} |
| 150 | + <span className="px-3 py-1.5 text-sm text-slate-500">Page {logs.current_page} of {logs.last_page}</span> |
| 151 | + {logs.next_page_url && ( |
| 152 | + <Link href={logs.next_page_url} className="rounded-md border border-slate-300 px-3 py-1.5 text-sm text-slate-600 hover:bg-slate-50"> |
| 153 | + Next → |
| 154 | + </Link> |
| 155 | + )} |
| 156 | + </div> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + </AppLayout> |
| 160 | + ); |
| 161 | +} |
0 commit comments