import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { webhooksApi, Webhook, WebhookDelivery, WebhookEvent, CreateWebhookRequest, UpdateWebhookRequest, DeliveryStatus, PaginatedDeliveries, } from '@/services/api'; // Query keys const WEBHOOKS_KEY = ['webhooks']; const WEBHOOK_KEY = (id: string) => ['webhooks', id]; const WEBHOOK_EVENTS_KEY = ['webhooks', 'events']; const WEBHOOK_DELIVERIES_KEY = (id: string) => ['webhooks', id, 'deliveries']; // List all webhooks export function useWebhooks() { return useQuery({ queryKey: WEBHOOKS_KEY, queryFn: webhooksApi.list, }); } // Get single webhook export function useWebhook(id: string) { return useQuery({ queryKey: WEBHOOK_KEY(id), queryFn: () => webhooksApi.get(id), enabled: !!id, }); } // Get available events export function useWebhookEvents() { return useQuery({ queryKey: WEBHOOK_EVENTS_KEY, queryFn: async () => { const response = await webhooksApi.getEvents(); return response.events; }, staleTime: 1000 * 60 * 60, // 1 hour - events don't change often }); } // Get webhook deliveries export function useWebhookDeliveries( webhookId: string, params?: { status?: DeliveryStatus; eventType?: string; page?: number; limit?: number } ) { return useQuery({ queryKey: [...WEBHOOK_DELIVERIES_KEY(webhookId), params], queryFn: () => webhooksApi.getDeliveries(webhookId, params), enabled: !!webhookId, }); } // Create webhook export function useCreateWebhook() { const queryClient = useQueryClient(); return useMutation({ mutationFn: (data: CreateWebhookRequest) => webhooksApi.create(data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: WEBHOOKS_KEY }); }, }); } // Update webhook export function useUpdateWebhook() { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ id, data }: { id: string; data: UpdateWebhookRequest }) => webhooksApi.update(id, data), onSuccess: (_, { id }) => { queryClient.invalidateQueries({ queryKey: WEBHOOKS_KEY }); queryClient.invalidateQueries({ queryKey: WEBHOOK_KEY(id) }); }, }); } // Delete webhook export function useDeleteWebhook() { const queryClient = useQueryClient(); return useMutation({ mutationFn: (id: string) => webhooksApi.delete(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: WEBHOOKS_KEY }); }, }); } // Toggle webhook active status export function useToggleWebhook() { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ id, isActive }: { id: string; isActive: boolean }) => webhooksApi.update(id, { isActive }), onSuccess: (_, { id }) => { queryClient.invalidateQueries({ queryKey: WEBHOOKS_KEY }); queryClient.invalidateQueries({ queryKey: WEBHOOK_KEY(id) }); }, }); } // Regenerate secret export function useRegenerateWebhookSecret() { const queryClient = useQueryClient(); return useMutation({ mutationFn: (id: string) => webhooksApi.regenerateSecret(id), onSuccess: (_, id) => { queryClient.invalidateQueries({ queryKey: WEBHOOK_KEY(id) }); }, }); } // Test webhook export function useTestWebhook() { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ id, payload }: { id: string; payload?: { eventType?: string; payload?: Record } }) => webhooksApi.test(id, payload), onSuccess: (_, { id }) => { queryClient.invalidateQueries({ queryKey: WEBHOOK_DELIVERIES_KEY(id) }); }, }); } // Retry delivery export function useRetryDelivery() { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ webhookId, deliveryId }: { webhookId: string; deliveryId: string }) => webhooksApi.retryDelivery(webhookId, deliveryId), onSuccess: (_, { webhookId }) => { queryClient.invalidateQueries({ queryKey: WEBHOOK_DELIVERIES_KEY(webhookId) }); queryClient.invalidateQueries({ queryKey: WEBHOOK_KEY(webhookId) }); }, }); }