import { useMutation, useQueryClient } from '@tanstack/react-query' import { toast } from 'sonner' import { platformWebhooksKeys } from './keys' import { createWebhookEndpoint, type WebhookScopeParams } from './platform-webhooks-fetchers' import { toWebhookEndpoint, toWebhookEndpointAttributes } from './platform-webhooks-mappers' import type { UpsertWebhookEndpointInput, WebhookEndpoint, } from '@/components/interfaces/Platform/Webhooks/PlatformWebhooks.types' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomMutationOptions } from '@/types' export type WebhookEndpointCreateVariables = { scope: WebhookScopeParams input: UpsertWebhookEndpointInput } async function createEndpoint({ scope, input, }: WebhookEndpointCreateVariables): Promise { const attributes = toWebhookEndpointAttributes(input) if (!attributes.signing_secret) throw new Error('A signing secret is required') const result = await createWebhookEndpoint(scope, { ...attributes, signing_secret: attributes.signing_secret, }) if (!result?.data) throw new Error('Endpoint was created but no data was returned') return toWebhookEndpoint(result.data) } type WebhookEndpointCreateData = Awaited> export const useWebhookEndpointCreateMutation = ({ onSuccess, onError, ...options }: Omit< UseCustomMutationOptions, 'mutationFn' > = {}) => { const queryClient = useQueryClient() return useMutation({ mutationFn: createEndpoint, 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 create webhook endpoint: ${data.message}`) } else { onError(data, variables, context) } }, ...options, }) }