/** * Entity History hooks — undo/restore/trash functionality. */ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { apiGet, apiPost, apiClient } from './client'; export interface EntityHistoryEntry { id: string; entity_type: string; entity_id: string; action: 'create' | 'update' | 'delete'; snapshot_before: Record | null; snapshot_after: Record | null; changes: Record | null; user_id: string | null; created_at: string; } export interface EntityHistoryList { items: EntityHistoryEntry[]; total: number; } export interface TrashItem { history_id: string; entity_type: string; entity_id: string; snapshot_before: Record | null; deleted_at: string | null; user_id: string | null; } export interface TrashListResponse { items: TrashItem[]; total: number; limit: number; offset: number; } export interface BulkRestoreResultItem { history_id: string; success: boolean; entity_type: string | null; entity_id: string | null; error: string | null; } export interface BulkRestoreResponse { total: number; succeeded: number; failed: number; results: BulkRestoreResultItem[]; partial_success: boolean; } export function useEntityHistory(entityType?: string, entityId?: string, limit = 50) { return useQuery({ queryKey: ['entityHistory', entityType, entityId, limit], queryFn: () => apiGet(`/entity-history/${entityType}/${entityId}?limit=${limit}`), enabled: !!entityType && !!entityId, }); } export function useTrashList(entityType?: string, limit = 50, offset = 0) { const params = new URLSearchParams(); params.set('limit', String(limit)); params.set('offset', String(offset)); if (entityType) { params.set('entity_type', entityType); } return useQuery({ queryKey: ['trash', entityType ?? 'all', limit, offset], queryFn: () => apiGet(`/entity-history/trash?${params.toString()}`), }); } export function useRestoreFromHistory() { const queryClient = useQueryClient(); return useMutation({ mutationFn: (historyId: string) => apiClient.post('/entity-history/restore', { history_id: historyId }).then(r => r.data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['entityHistory'] }); queryClient.invalidateQueries({ queryKey: ['trash'] }); queryClient.invalidateQueries({ queryKey: ['contacts'] }); queryClient.invalidateQueries({ queryKey: ['contact'] }); }, }); } export function useBulkRestore() { const queryClient = useQueryClient(); return useMutation({ mutationFn: (historyIds: string[]) => apiPost('/entity-history/bulk-restore', { history_ids: historyIds }), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['entityHistory'] }); queryClient.invalidateQueries({ queryKey: ['trash'] }); queryClient.invalidateQueries({ queryKey: ['contacts'] }); queryClient.invalidateQueries({ queryKey: ['contact'] }); }, }); } export function useUndoLastAction() { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ entityType, entityId }: { entityType: string; entityId: string }) => apiPost(`/entity-history/undo/${entityType}/${entityId}`, {}), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['entityHistory'] }); queryClient.invalidateQueries({ queryKey: ['trash'] }); queryClient.invalidateQueries({ queryKey: ['contacts'] }); queryClient.invalidateQueries({ queryKey: ['contact'] }); }, }); }