import { useParams } from 'common' import { toast } from 'sonner' import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from 'ui' import { getRestartRequestStatus } from './Pipeline.utils' import type { PipelineStatusName } from './Replication.constants' import { RestartCostEstimate } from './RestartCostEstimate' import { shouldCopyTable, type ReplicationTableIdentity } from './TableSyncCopy.utils' import { useRollbackTablesMutation } from '@/data/replication/rollback-tables-mutation' import type { TableSyncCopyConfig } from '@/data/replication/types' import { PipelineStatusRequestStatus, usePipelineRequestStatus, } from '@/state/replication-pipeline-request-status' interface RestartTableDialogProps { pipelineStatusName?: PipelineStatusName open: boolean onOpenChange: (open: boolean) => void table: ReplicationTableIdentity tableSyncCopy?: TableSyncCopyConfig | null sourceId?: number publicationName?: string onResetStart?: (tableId: number) => void onResetComplete?: (tableId: number) => void } export const RestartTableDialog = ({ open, onOpenChange, table, tableSyncCopy, sourceId, publicationName, pipelineStatusName, onResetStart, onResetComplete, }: RestartTableDialogProps) => { const { ref: projectRef, pipelineId } = useParams() const { runWithRequestStatus } = usePipelineRequestStatus() const restartRequestStatus = getRestartRequestStatus(pipelineStatusName) const tableName = `${table.schema}.${table.name}` const willCopyTable = shouldCopyTable(tableSyncCopy, table.id) const { mutateAsync: rollbackTables, isPending: isResetting } = useRollbackTablesMutation({ onSuccess: () => { toast.success(`Replication progress reset for "${tableName}"`) onOpenChange(false) }, onError: (error) => { toast.error(`Failed to reset table: ${error.message}`) }, }) const handleReset = async () => { if (!projectRef) return toast.error('Project ref is required') if (!pipelineId) return toast.error('Pipeline ID is required') onResetStart?.(table.id) try { await runWithRequestStatus(pipelineId, restartRequestStatus, () => rollbackTables({ projectRef, pipelineId, target: { type: 'single_table', table_id: table.id }, }) ) } finally { onResetComplete?.(table.id) } } const resetDescription = willCopyTable ? "This clears the table's saved replication progress. When replication resumes, its destination data is deleted and existing source rows are copied again." : "This clears the table's saved replication progress. When replication resumes, its destination data is deleted. Initial sync is skipped, so only new changes are replicated." const shouldRestartPipeline = restartRequestStatus !== PipelineStatusRequestStatus.None const consequence = shouldRestartPipeline ? `${resetDescription} The pipeline restarts automatically. Other tables' unfinished initial syncs also start over.` : `${resetDescription} A stopped pipeline stays stopped until you start it.` return ( Reset {tableName} {consequence} Cancel {isResetting ? 'Resetting…' : 'Reset table'} ) }