Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 46s
52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
// src/hooks/queries/useActivityLogQuery.ts
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import * as apiClient from '../../services/apiClient';
|
|
|
|
interface ActivityLogEntry {
|
|
activity_log_id: number;
|
|
user_id: string;
|
|
action: string;
|
|
entity_type: string | null;
|
|
entity_id: number | null;
|
|
details: any;
|
|
ip_address: string | null;
|
|
user_agent: string | null;
|
|
created_at: string;
|
|
}
|
|
|
|
/**
|
|
* Query hook for fetching the admin activity log.
|
|
*
|
|
* The activity log contains a record of all administrative actions
|
|
* performed in the system. This data changes frequently as new
|
|
* actions are logged, so it has a shorter stale time.
|
|
*
|
|
* @param limit - Maximum number of entries to fetch (default: 20)
|
|
* @param offset - Number of entries to skip for pagination (default: 0)
|
|
* @returns Query result with activity log entries
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* const { data: activityLog, isLoading, error } = useActivityLogQuery(20, 0);
|
|
* ```
|
|
*/
|
|
export const useActivityLogQuery = (limit: number = 20, offset: number = 0) => {
|
|
return useQuery({
|
|
queryKey: ['activity-log', { limit, offset }],
|
|
queryFn: async (): Promise<ActivityLogEntry[]> => {
|
|
const response = await apiClient.fetchActivityLog(limit, offset);
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json().catch(() => ({
|
|
message: `Request failed with status ${response.status}`,
|
|
}));
|
|
throw new Error(error.message || 'Failed to fetch activity log');
|
|
}
|
|
|
|
return response.json();
|
|
},
|
|
// Activity log changes frequently, keep stale time short
|
|
staleTime: 1000 * 30, // 30 seconds
|
|
});
|
|
};
|