refactor: implement audit fetch trigger in OrganizationSettings to enhance data loading efficiency and improve filter responsiveness

This commit is contained in:
Usman Baig
2026-02-05 14:32:33 +01:00
parent e581d5212f
commit de22c2da63
3 changed files with 11 additions and 35 deletions

View File

@@ -2,7 +2,7 @@
* Audit log API client (org-scoped; requires org admin role)
*/
import { API_URL } from './client'
import apiRequest from './client'
export interface AuditLogEntry {
id: string
@@ -29,35 +29,6 @@ export interface GetAuditLogResponse {
total: number
}
async function auditFetch<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
const url = `${API_URL}${endpoint}`
const headers: HeadersInit = {
'Content-Type': 'application/json',
...options.headers,
}
const response = await fetch(url, {
...options,
headers,
credentials: 'include',
})
if (!response.ok) {
const errorBody = await response.json().catch(() => ({
error: 'Unknown error',
message: `HTTP ${response.status}: ${response.statusText}`,
}))
throw new Error(errorBody.message || errorBody.error || 'Request failed')
}
return response.json()
}
/**
* Fetches paginated audit log entries for the current org (org from JWT; admin-only on backend).
* Normalizes response so entries is always an array (backend may return null when empty).
*/
export async function getAuditLog(params: GetAuditLogParams = {}): Promise<GetAuditLogResponse> {
const search = new URLSearchParams()
if (params.limit != null) search.set('limit', String(params.limit))
@@ -68,7 +39,7 @@ export async function getAuditLog(params: GetAuditLogParams = {}): Promise<GetAu
if (params.end_date) search.set('end_date', params.end_date)
const qs = search.toString()
const url = qs ? `/api/audit?${qs}` : '/api/audit'
const data = await auditFetch<GetAuditLogResponse>(url, { method: 'GET' })
const data = await apiRequest<GetAuditLogResponse>(url, { method: 'GET' })
return {
entries: Array.isArray(data?.entries) ? data.entries : [],
total: typeof data?.total === 'number' ? data.total : 0,