bank-statement-api / befree-frontend /hooks /useOverwatchAPI.ts
3v324v23's picture
Merge Conflict solved | Build error solved
205f25f
Raw History Blame Contribute Delete
3.45 kB
import {
useQuery,
useMutation,
} from '@tanstack/react-query';
import { apiClient } from '@/services/api-client';
import type {
OverwatchDocumentsParams,
OverwatchEventsParams,
OverwatchInsights,
} from '@/types';
export const overwatchKeys = {
all: ['overwatch'] as const,
insights: () => [...overwatchKeys.all, 'insights'] as const,
logs: (filters?: Record<string, unknown>) =>
[...overwatchKeys.all, 'logs', filters ?? {}] as const,
documents: (params?: OverwatchDocumentsParams) =>
[...overwatchKeys.all, 'documents', params ?? {}] as const,
documentSummary: (documentId: string) =>
[...overwatchKeys.all, 'document', documentId, 'summary'] as const,
events: (params?: OverwatchEventsParams) =>
[...overwatchKeys.all, 'events', params ?? {}] as const,
};
// ── Legacy UAT hooks (kept for backward compatibility) ────────────────────────
export const useOverwatchInsights = () => {
return useQuery<OverwatchInsights>({
queryKey: overwatchKeys.insights(),
queryFn: () => apiClient.getOverwatchInsights(),
refetchInterval: 30 * 1000,
});
};
export const useOverwatchLogs = (params: {
log_type: 'audit' | 'classification';
username?: string;
action?: string;
category?: string;
engine?: string;
from_date?: string;
to_date?: string;
search?: string;
page?: number;
page_size?: number;
}) => {
return useQuery({
queryKey: overwatchKeys.logs(params),
queryFn: () => apiClient.getOverwatchLogs(params),
});
};
export const useEmailOverwatchReport = () => {
return useMutation({
mutationFn: (payload: {
email: string;
log_type: 'audit' | 'classification' | 'both';
from_date?: string;
to_date?: string;
username?: string;
category?: string;
engine?: string;
}) => apiClient.emailOverwatchReport(payload),
});
};
// ── Structured Event Store hooks ──────────────────────────────────────────────
/** Paginated list of documents with their processing health summary. */
export const useOverwatchDocuments = (params: OverwatchDocumentsParams) => {
return useQuery({
queryKey: overwatchKeys.documents(params),
queryFn: () => apiClient.getOverwatchDocuments(params),
staleTime: 10_000,
});
};
/**
* Full detail for a single document: extraction pipeline, classification
* summary, and counters. Returns `isError` on 404 (no events yet) or 422.
*/
export const useOverwatchDocumentSummary = (documentId: string) => {
return useQuery({
queryKey: overwatchKeys.documentSummary(documentId),
queryFn: () => apiClient.getOverwatchDocumentSummary(documentId),
enabled: documentId.length > 0,
retry: (failureCount, error: unknown) => {
// Don't retry on 404/422 β€” these are definitive responses
const status = (error as { response?: { status?: number } })?.response?.status;
if (status === 404 || status === 422) return false;
return failureCount < 2;
},
});
};
/** Paginated, filterable event log. */
export const useOverwatchEvents = (params: OverwatchEventsParams) => {
return useQuery({
queryKey: overwatchKeys.events(params),
queryFn: () => apiClient.getOverwatchEvents(params),
});
};