import { useMutation, useQueryClient } from '@tanstack/react-query' import { toast } from 'sonner' import { platformWebhooksKeys } from './keys' import { deleteWebhookEndpoint, type WebhookScopeParams } from './platform-webhooks-fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomMutationOptions } from '@/types' export type WebhookEndpointDeleteVariables = { scope: WebhookScopeParams id: string } async function deleteEndpoint({ scope, id }: WebhookEndpointDeleteVariables) { return deleteWebhookEndpoint(scope, id) } type WebhookEndpointDeleteData = Awaited> export const useWebhookEndpointDeleteMutation = ({ onSuccess, onError, ...options }: Omit< UseCustomMutationOptions, 'mutationFn' > = {}) => { const queryClient = useQueryClient() return useMutation({ mutationFn: deleteEndpoint, async onSuccess(data, variables, context) { await queryClient.invalidateQueries({ queryKey: platformWebhooksKeys.endpoints(variables.scope), }) await onSuccess?.(data, variables, context) }, async onError(data, variables, context) { if (onError === undefined) { toast.error(`Failed to delete webhook endpoint: ${data.message}`) } else { onError(data, variables, context) } }, ...options, }) }