import { useQuery } from '@tanstack/react-query' import { platformWebhooksKeys } from './keys' import { listWebhookEndpoints, type WebhookScopeParams } from './platform-webhooks-fetchers' import { toWebhookEndpoint } from './platform-webhooks-mappers' import type { WebhookEndpoint } from '@/components/interfaces/Platform/Webhooks/PlatformWebhooks.types' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' interface WebhookEndpointsVariables { scope: WebhookScopeParams limit?: number offset?: number } async function getWebhookEndpoints( { scope, limit, offset }: WebhookEndpointsVariables, signal?: AbortSignal ): Promise { const result = await listWebhookEndpoints(scope, { limit, offset }, signal) return (result?.data ?? []).map(toWebhookEndpoint) } export type WebhookEndpointsData = Awaited> export const useWebhookEndpointsQuery = ( variables: WebhookEndpointsVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => { const { scope, limit, offset } = variables return useQuery({ queryKey: platformWebhooksKeys.endpoints(scope, limit, offset), queryFn: ({ signal }) => getWebhookEndpoints({ scope, limit, offset }, signal), enabled, ...options, }) }