Files
supabase/apps/studio/data/replication/stop-pipeline-mutation.ts
Riccardo Busetti 1a3ba9f15a ref(etl): Rename ETL Replication to Replication and update docs (#40769)
* ref(etl): Rename ETL Replication to Replication and update docs

* Fix

* Fix

* Fix

* Fix

* Fix
2025-11-25 20:45:50 +08:00

58 lines
1.7 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { handleError, post } from 'data/fetchers'
import type { ResponseError, UseCustomMutationOptions } from 'types'
import { replicationKeys } from './keys'
export type StopPipelineParams = {
projectRef: string
pipelineId: number
}
async function stopPipeline({ projectRef, pipelineId }: StopPipelineParams, signal?: AbortSignal) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await post('/platform/replication/{ref}/pipelines/{pipeline_id}/stop', {
params: { path: { ref: projectRef, pipeline_id: pipelineId } },
signal,
})
if (error) {
handleError(error)
}
return data
}
type StartPipelineData = Awaited<ReturnType<typeof stopPipeline>>
export const useStopPipelineMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<StartPipelineData, ResponseError, StopPipelineParams>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<StartPipelineData, ResponseError, StopPipelineParams>({
mutationFn: (vars) => stopPipeline(vars),
async onSuccess(data, variables, context) {
const { projectRef, pipelineId } = variables
await queryClient.invalidateQueries({
queryKey: replicationKeys.pipelinesStatus(projectRef, pipelineId),
})
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to stop pipeline: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}