import Table from 'components/to-be-cleaned/Table' import AlertError from 'components/ui/AlertError' import { ReplicationPipelinesData } from 'data/replication/pipelines-query' import { ResponseError } from 'types' import ShimmeringLoader from 'ui-patterns/ShimmeringLoader' import RowMenu from './RowMenu' import PipelineStatus from './PipelineStatus' import { useParams } from 'common' import { useReplicationPipelineStatusQuery } from 'data/replication/pipeline-status-query' import { useState } from 'react' import { toast } from 'sonner' import { useStartPipelineMutation } from 'data/replication/start-pipeline-mutation' import { useStopPipelineMutation } from 'data/replication/stop-pipeline-mutation' import { useDeleteSinkMutation } from 'data/replication/delete-sink-mutation' import DeleteDestination from './DeleteDestination' import DestinationPanel from './DestinationPanel' export type Pipeline = ReplicationPipelinesData['pipelines'][0] interface DestinationRowProps { sourceId: number | undefined sinkId: number sinkName: string type: string pipeline: Pipeline | undefined error: ResponseError | null isLoading: boolean isError: boolean isSuccess: boolean } const DestinationRow = ({ sourceId, sinkId, sinkName, type, pipeline, error: pipelineError, isLoading: isPipelineLoading, isError: isPipelineError, isSuccess: isPipelineSuccess, }: DestinationRowProps) => { const { ref: projectRef } = useParams() const [refetchInterval, setRefetchInterval] = useState(false) const [showDeleteDestinationForm, setShowDeleteDestinationForm] = useState(false) const [showEditDestinationPanel, setShowEditDestinationPanel] = useState(false) const { data: pipelineStatusData, error: pipelineStatusError, isLoading: isPipelineStatusLoading, isError: isPipelineStatusError, isSuccess: isPipelineStatusSuccess, } = useReplicationPipelineStatusQuery( { projectRef, pipelineId: pipeline?.id, }, { refetchInterval } ) const [requestStatus, setRequestStatus] = useState< 'None' | 'EnableRequested' | 'DisableRequested' >('None') const { mutateAsync: startPipeline } = useStartPipelineMutation() const { mutateAsync: stopPipeline } = useStopPipelineMutation() const pipelineStatus = pipelineStatusData?.status if ( (requestStatus === 'EnableRequested' && pipelineStatus === 'Started') || (requestStatus === 'DisableRequested' && pipelineStatus === 'Stopped') ) { setRefetchInterval(false) setRequestStatus('None') } const onEnableClick = async () => { if (!projectRef) { console.error('Project ref is required') return } if (!pipeline) { toast.error('No pipeline found') return } try { await startPipeline({ projectRef, pipelineId: pipeline.id }) } catch (error) { toast.error('Failed to enable destination') } setRequestStatus('EnableRequested') setRefetchInterval(5000) } const onDisableClick = async () => { if (!projectRef) { console.error('Project ref is required') return } if (!pipeline) { toast.error('No pipeline found') return } try { await stopPipeline({ projectRef, pipelineId: pipeline.id }) } catch (error) { toast.error('Failed to disable destination') } setRequestStatus('DisableRequested') setRefetchInterval(5000) } const { mutateAsync: deleteSink } = useDeleteSinkMutation({}) const onDeleteClick = async () => { if (!projectRef) { console.error('Project ref is required') return } if (!pipeline) { toast.error('No pipeline found') return } try { await stopPipeline({ projectRef, pipelineId: pipeline.id }) // deleting the sink also deletes the pipeline because of cascade delete // so we don't need to call deletePipeline explicitly await deleteSink({ projectRef, sinkId }) } catch (error) { toast.error('Failed to delete destination') } } return ( <> {isPipelineError && ( )} {isPipelineSuccess && ( {isPipelineLoading ? : sinkName} {isPipelineLoading ? : type} {isPipelineLoading || !pipeline ? ( ) : ( )} {isPipelineLoading || !pipeline ? ( ) : ( pipeline.publication_name )} setShowDeleteDestinationForm(true)} onEditClick={() => setShowEditDestinationPanel(true)} > )} setShowEditDestinationPanel(false)} sourceId={sourceId} existingDestination={{ sourceId, sinkId, pipelineId: pipeline?.id, enabled: pipelineStatusData?.status === 'Started', }} /> ) } export default DestinationRow