mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-25 08:31:13 -05:00
feat: Implement API and cache improvements
This commit is contained in:
55
src/hooks/admin/useAuditLogs.ts
Normal file
55
src/hooks/admin/useAuditLogs.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { queryKeys } from '@/lib/queryKeys';
|
||||
|
||||
interface AuditLogFilters {
|
||||
userId?: string;
|
||||
action?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for querying audit logs with proper caching
|
||||
* Provides: paginated audit log queries with filtering
|
||||
*/
|
||||
export function useAuditLogs(filters: AuditLogFilters = {}) {
|
||||
const { userId, action, page = 1, pageSize = 50 } = filters;
|
||||
|
||||
return useQuery({
|
||||
queryKey: queryKeys.admin.auditLogs(userId),
|
||||
queryFn: async () => {
|
||||
let query = supabase
|
||||
.from('profile_audit_log')
|
||||
.select('*', { count: 'exact' })
|
||||
.order('created_at', { ascending: false });
|
||||
|
||||
if (userId) {
|
||||
query = query.eq('user_id', userId);
|
||||
}
|
||||
|
||||
if (action) {
|
||||
query = query.eq('action', action);
|
||||
}
|
||||
|
||||
// Apply pagination
|
||||
const startIndex = (page - 1) * pageSize;
|
||||
const endIndex = startIndex + pageSize - 1;
|
||||
query = query.range(startIndex, endIndex);
|
||||
|
||||
const { data, error, count } = await query;
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
return {
|
||||
logs: data || [],
|
||||
total: count || 0,
|
||||
page,
|
||||
pageSize,
|
||||
totalPages: Math.ceil((count || 0) / pageSize),
|
||||
};
|
||||
},
|
||||
staleTime: 2 * 60 * 1000, // 2 minutes
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user