import { useParams } from 'common' import { useMemo } from 'react' import { toast } from 'sonner' import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from 'ui' import { PipelineStatusName } from './Replication.constants' import { ReplicationPipelineTableStatus } from '@/data/replication/pipeline-replication-status-query' import { useRollbackTablesMutation } from '@/data/replication/rollback-tables-mutation' interface BatchRestartDialogProps { open: boolean onOpenChange: (open: boolean) => void mode: 'all' | 'errored' totalTables: number erroredTablesCount: number tables: ReplicationPipelineTableStatus[] pipelineStatusName?: PipelineStatusName onRestartStart?: (tableIds: number[]) => void onRestartComplete?: (tableIds: number[]) => void } export const BatchRestartDialog = ({ open, onOpenChange, mode, totalTables, erroredTablesCount, tables, pipelineStatusName, onRestartStart, onRestartComplete, }: BatchRestartDialogProps) => { const { ref: projectRef, pipelineId: _pipelineId } = useParams() const pipelineId = Number(_pipelineId) // Calculate which table IDs will be restarted based on mode (memoized) const affectedTableIds = useMemo(() => { if (mode === 'all') { return tables.map((t) => t.table_id) } else { return tables .filter( (t) => t.state.name === 'error' && 'retry_policy' in t.state && t.state.retry_policy?.policy === 'manual_retry' ) .map((t) => t.table_id) } }, [mode, tables]) const { mutate: rollbackTables, isPending: isResetting } = useRollbackTablesMutation({ onSuccess: (data) => { const count = data.tables.length toast.success( `Restarting replication for ${count} table${count > 1 ? 's' : ''}. Pipeline will restart automatically.` ) }, onSettled: () => { onRestartComplete?.(affectedTableIds) onOpenChange(false) }, onError: (error) => { toast.error(`Failed to restart replication: ${error.message}`) }, }) const handleReset = () => { if (!projectRef) return toast.error('Project ref is required') onRestartStart?.(affectedTableIds) rollbackTables({ projectRef, pipelineId, target: mode === 'all' ? { type: 'all_tables' } : { type: 'all_errored_tables' }, rollbackType: 'full', pipelineStatusName, }) } const dialogContent = mode === 'all' ? { title: 'Restart all tables', description: (

This will restart replication for all {totalTables === 0 ? '' : totalTables} table{totalTables > 1 ? 's' : ''} in this pipeline from scratch:

), action: 'Restart all tables', } : { title: 'Restart failed tables', description: (

This will restart replication for{' '} all {erroredTablesCount} failed tables from scratch:

), action: 'Restart failed tables', } return ( {dialogContent.title} {dialogContent.description} Cancel {isResetting ? 'Restarting replication...' : dialogContent.action} ) }