|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useMemo, useState } from "react"; |
| 4 | +import { ScrollText, RefreshCw, AlertTriangle, ChevronDown, ChevronRight } from "lucide-react"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; |
| 7 | +import { Badge } from "@/components/ui/badge"; |
| 8 | +import { DataTable } from "@/components/data-table"; |
| 9 | +import { |
| 10 | + findActiveTenant, |
| 11 | + listAuditEvents, |
| 12 | + type AuditEvent, |
| 13 | + type ListAuditEventsParams, |
| 14 | +} from "@/lib/api"; |
| 15 | + |
| 16 | +const PAGE_SIZE = 100; |
| 17 | + |
| 18 | +/** Collapsible JSON viewer for the per-row `data` payload. */ |
| 19 | +function DataCell({ value }: { value: unknown }) { |
| 20 | + const [open, setOpen] = useState(false); |
| 21 | + const json = useMemo(() => { |
| 22 | + try { |
| 23 | + return JSON.stringify(value, null, 2); |
| 24 | + } catch { |
| 25 | + return String(value); |
| 26 | + } |
| 27 | + }, [value]); |
| 28 | + |
| 29 | + if (value === null || value === undefined || json === "{}" || json === "null") { |
| 30 | + return <span className="text-xs text-muted-foreground">—</span>; |
| 31 | + } |
| 32 | + |
| 33 | + return ( |
| 34 | + <div className="max-w-xl"> |
| 35 | + <button |
| 36 | + type="button" |
| 37 | + onClick={() => setOpen((o) => !o)} |
| 38 | + className="flex items-center gap-1 text-xs font-medium text-muted-foreground hover:text-foreground" |
| 39 | + data-testid="audit-data-toggle" |
| 40 | + > |
| 41 | + {open ? <ChevronDown className="h-3 w-3" /> : <ChevronRight className="h-3 w-3" />} |
| 42 | + {open ? "Hide" : "Show"} payload |
| 43 | + </button> |
| 44 | + {open && ( |
| 45 | + <pre |
| 46 | + data-testid="audit-data-payload" |
| 47 | + className="mt-2 max-h-60 overflow-auto rounded-md border bg-muted/30 p-2 text-[11px] leading-relaxed" |
| 48 | + > |
| 49 | + {json} |
| 50 | + </pre> |
| 51 | + )} |
| 52 | + </div> |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | +export default function AuditPage() { |
| 57 | + const [events, setEvents] = useState<AuditEvent[] | null>(null); |
| 58 | + const [loading, setLoading] = useState(true); |
| 59 | + const [error, setError] = useState<string | null>(null); |
| 60 | + const [tenantId, setTenantId] = useState<string | undefined>(undefined); |
| 61 | + const [filter, setFilter] = useState(""); |
| 62 | + const [offset, setOffset] = useState(0); |
| 63 | + |
| 64 | + async function load(currentTenant: string | undefined, params: ListAuditEventsParams) { |
| 65 | + setLoading(true); |
| 66 | + setError(null); |
| 67 | + try { |
| 68 | + const data = await listAuditEvents(params, currentTenant); |
| 69 | + setEvents(data); |
| 70 | + } catch (e) { |
| 71 | + setEvents(null); |
| 72 | + setError(e instanceof Error ? e.message : "Failed to load audit events"); |
| 73 | + } finally { |
| 74 | + setLoading(false); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + useEffect(() => { |
| 79 | + let cancelled = false; |
| 80 | + (async () => { |
| 81 | + const t = await findActiveTenant(); |
| 82 | + if (cancelled) return; |
| 83 | + setTenantId(t); |
| 84 | + await load(t, { limit: PAGE_SIZE, offset: 0 }); |
| 85 | + })(); |
| 86 | + return () => { |
| 87 | + cancelled = true; |
| 88 | + }; |
| 89 | + }, []); |
| 90 | + |
| 91 | + const handleFilterApply = () => { |
| 92 | + setOffset(0); |
| 93 | + load(tenantId, { |
| 94 | + limit: PAGE_SIZE, |
| 95 | + offset: 0, |
| 96 | + event_type: filter.trim() || undefined, |
| 97 | + }); |
| 98 | + }; |
| 99 | + |
| 100 | + const handlePage = (direction: "prev" | "next") => { |
| 101 | + const nextOffset = |
| 102 | + direction === "prev" ? Math.max(0, offset - PAGE_SIZE) : offset + PAGE_SIZE; |
| 103 | + setOffset(nextOffset); |
| 104 | + load(tenantId, { |
| 105 | + limit: PAGE_SIZE, |
| 106 | + offset: nextOffset, |
| 107 | + event_type: filter.trim() || undefined, |
| 108 | + }); |
| 109 | + }; |
| 110 | + |
| 111 | + const columns = [ |
| 112 | + { |
| 113 | + header: "Timestamp", |
| 114 | + accessor: (e: AuditEvent) => ( |
| 115 | + <span className="font-mono text-xs"> |
| 116 | + {new Date(e.timestamp).toLocaleString()} |
| 117 | + </span> |
| 118 | + ), |
| 119 | + }, |
| 120 | + { |
| 121 | + header: "Event type", |
| 122 | + accessor: (e: AuditEvent) => ( |
| 123 | + <Badge variant="secondary" className="font-mono text-[11px]"> |
| 124 | + {e.event_type} |
| 125 | + </Badge> |
| 126 | + ), |
| 127 | + }, |
| 128 | + { |
| 129 | + header: "Actor", |
| 130 | + accessor: (e: AuditEvent) => ( |
| 131 | + <span className="text-xs text-muted-foreground">{e.actor || "—"}</span> |
| 132 | + ), |
| 133 | + }, |
| 134 | + { |
| 135 | + header: "Resource", |
| 136 | + accessor: (e: AuditEvent) => ( |
| 137 | + <span className="font-mono text-[11px] break-all">{e.resource || "—"}</span> |
| 138 | + ), |
| 139 | + className: "max-w-xs", |
| 140 | + }, |
| 141 | + { |
| 142 | + header: "Data", |
| 143 | + accessor: (e: AuditEvent) => <DataCell value={e.data} />, |
| 144 | + }, |
| 145 | + ]; |
| 146 | + |
| 147 | + const pageNumber = Math.floor(offset / PAGE_SIZE) + 1; |
| 148 | + const hasNext = (events?.length ?? 0) === PAGE_SIZE; |
| 149 | + const hasPrev = offset > 0; |
| 150 | + |
| 151 | + return ( |
| 152 | + <div className="space-y-6 max-w-6xl mx-auto pb-12"> |
| 153 | + <div className="flex items-center justify-between"> |
| 154 | + <div className="space-y-1"> |
| 155 | + <h1 className="text-3xl font-bold tracking-tight flex items-center gap-2"> |
| 156 | + <ScrollText className="h-7 w-7 text-primary" /> Audit |
| 157 | + </h1> |
| 158 | + <p className="text-sm text-muted-foreground"> |
| 159 | + Tenant-scoped audit events: tenant CRUD, API key mint/revoke, config changes, etc. |
| 160 | + </p> |
| 161 | + </div> |
| 162 | + <Button |
| 163 | + variant="outline" |
| 164 | + onClick={() => load(tenantId, { limit: PAGE_SIZE, offset })} |
| 165 | + disabled={loading} |
| 166 | + className="shadow-sm" |
| 167 | + > |
| 168 | + <RefreshCw className={`mr-2 h-4 w-4 ${loading ? "animate-spin" : ""}`} /> Refresh |
| 169 | + </Button> |
| 170 | + </div> |
| 171 | + |
| 172 | + <Card> |
| 173 | + <CardHeader className="pb-3"> |
| 174 | + <CardTitle className="text-base">Filter</CardTitle> |
| 175 | + <CardDescription className="text-xs"> |
| 176 | + Filter by exact event type (e.g. <code className="font-mono">tenant_created</code>,{" "} |
| 177 | + <code className="font-mono">api_key_created</code>). |
| 178 | + </CardDescription> |
| 179 | + </CardHeader> |
| 180 | + <CardContent className="flex flex-wrap items-end gap-3"> |
| 181 | + <div className="space-y-1"> |
| 182 | + <label className="text-[10px] font-bold uppercase text-muted-foreground"> |
| 183 | + Event type |
| 184 | + </label> |
| 185 | + <input |
| 186 | + type="text" |
| 187 | + value={filter} |
| 188 | + data-testid="audit-event-type-filter" |
| 189 | + onChange={(e) => setFilter(e.target.value)} |
| 190 | + onKeyDown={(e) => { |
| 191 | + if (e.key === "Enter") handleFilterApply(); |
| 192 | + }} |
| 193 | + placeholder="tenant_created" |
| 194 | + className="h-9 w-56 rounded-md border border-input bg-background px-3 py-2 text-sm focus:ring-2 focus:ring-primary" |
| 195 | + /> |
| 196 | + </div> |
| 197 | + <Button onClick={handleFilterApply} disabled={loading} size="sm" className="h-9"> |
| 198 | + Apply |
| 199 | + </Button> |
| 200 | + {filter && ( |
| 201 | + <Button |
| 202 | + onClick={() => { |
| 203 | + setFilter(""); |
| 204 | + setOffset(0); |
| 205 | + load(tenantId, { limit: PAGE_SIZE, offset: 0 }); |
| 206 | + }} |
| 207 | + variant="ghost" |
| 208 | + size="sm" |
| 209 | + className="h-9" |
| 210 | + > |
| 211 | + Clear |
| 212 | + </Button> |
| 213 | + )} |
| 214 | + </CardContent> |
| 215 | + </Card> |
| 216 | + |
| 217 | + <Card> |
| 218 | + <CardHeader className="pb-3 flex flex-row items-center justify-between"> |
| 219 | + <div> |
| 220 | + <CardTitle className="text-base">Audit Events</CardTitle> |
| 221 | + <CardDescription className="text-xs"> |
| 222 | + Page {pageNumber} · {events?.length ?? 0} event(s) |
| 223 | + </CardDescription> |
| 224 | + </div> |
| 225 | + <div className="flex gap-2"> |
| 226 | + <Button |
| 227 | + variant="outline" |
| 228 | + size="sm" |
| 229 | + disabled={!hasPrev || loading} |
| 230 | + onClick={() => handlePage("prev")} |
| 231 | + data-testid="audit-prev-page" |
| 232 | + > |
| 233 | + Prev |
| 234 | + </Button> |
| 235 | + <Button |
| 236 | + variant="outline" |
| 237 | + size="sm" |
| 238 | + disabled={!hasNext || loading} |
| 239 | + onClick={() => handlePage("next")} |
| 240 | + data-testid="audit-next-page" |
| 241 | + > |
| 242 | + Next |
| 243 | + </Button> |
| 244 | + </div> |
| 245 | + </CardHeader> |
| 246 | + <CardContent> |
| 247 | + {error && ( |
| 248 | + <div className="mb-4 flex items-center gap-2 rounded-md border border-destructive/40 bg-destructive/10 px-3 py-2 text-sm text-destructive"> |
| 249 | + <AlertTriangle className="h-4 w-4" /> |
| 250 | + <span data-testid="audit-error">{error}</span> |
| 251 | + </div> |
| 252 | + )} |
| 253 | + {loading ? ( |
| 254 | + <div className="py-12 text-center"> |
| 255 | + <RefreshCw className="mx-auto h-8 w-8 animate-spin text-muted-foreground/40" /> |
| 256 | + <p className="mt-4 text-sm text-muted-foreground animate-pulse"> |
| 257 | + Loading audit events… |
| 258 | + </p> |
| 259 | + </div> |
| 260 | + ) : ( |
| 261 | + <DataTable |
| 262 | + columns={columns} |
| 263 | + data={events ?? []} |
| 264 | + emptyMessage="No audit events recorded yet" |
| 265 | + /> |
| 266 | + )} |
| 267 | + </CardContent> |
| 268 | + </Card> |
| 269 | + </div> |
| 270 | + ); |
| 271 | +} |
0 commit comments