import { useQuery } from '@tanstack/react-query' import { platformWebhooksKeys } from './keys' import { getWebhookDelivery, type WebhookScopeParams } from './platform-webhooks-fetchers' import { toWebhookDelivery } from './platform-webhooks-mappers' import type { WebhookDelivery } from '@/components/interfaces/Platform/Webhooks/PlatformWebhooks.types' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' interface WebhookDeliveryVariables { scope: WebhookScopeParams endpointId: string id: string } async function fetchWebhookDelivery( { scope, endpointId, id }: WebhookDeliveryVariables, signal?: AbortSignal ): Promise { const result = await getWebhookDelivery(scope, id, signal) return result?.data ? toWebhookDelivery(result.data, endpointId) : null } export type WebhookDeliveryData = Awaited> export const useWebhookDeliveryQuery = ( variables: WebhookDeliveryVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => { const { scope, endpointId, id } = variables return useQuery({ queryKey: platformWebhooksKeys.delivery(scope, endpointId, id), queryFn: ({ signal }) => fetchWebhookDelivery({ scope, endpointId, id }, signal), enabled: enabled && Boolean(id), ...options, }) }