import { useQuery } from '@tanstack/react-query' import { replicationKeys } from './keys' import { pipelineIdSchema } from './pipeline-id' import { checkReplicationFeatureFlagRetry } from './utils' import { get, handleError } from '@/data/fetchers' import type { StudioError } from '@/lib/errors/studio-errors' import type { UseCustomQueryOptions } from '@/types' type ReplicationPipelinesParams = { projectRef?: string } async function fetchReplicationPipelines( { projectRef }: ReplicationPipelinesParams, signal?: AbortSignal ) { if (!projectRef) throw new Error('projectRef is required') const { data, error } = await get('/platform/replication/{ref}/pipelines', { params: { path: { ref: projectRef } }, signal, }) if (error) handleError(error) return { ...data, pipelines: data.pipelines.map((pipeline) => ({ ...pipeline, id: pipelineIdSchema.parse(pipeline.id), })), } } export type ReplicationPipelinesData = Awaited> export type Pipeline = ReplicationPipelinesData['pipelines'][0] export const useReplicationPipelinesQuery = ( { projectRef }: ReplicationPipelinesParams, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: replicationKeys.pipelines(projectRef), queryFn: ({ signal }) => fetchReplicationPipelines({ projectRef }, signal), enabled: enabled && typeof projectRef !== 'undefined', refetchOnWindowFocus: false, retry: checkReplicationFeatureFlagRetry, ...options, })