import { ChevronDown, RotateCcw, Undo2 } from 'lucide-react'
import { useState } from 'react'
import { toast } from 'sonner'
import { useParams } from 'common'
import { RollbackType, useRollbackTableMutation } from 'data/replication/rollback-table-mutation'
import { useStartPipelineMutation } from 'data/replication/start-pipeline-mutation'
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from 'ui'
const RETRY_OPTIONS = [
{
type: 'individual' as RollbackType,
icon: ,
title: 'Rollback to previous state',
description: 'Restart from the last working state (may not be enough in some cases)',
},
{
type: 'full' as RollbackType,
icon: ,
title: 'Reset from scratch',
description: 'Completely restart the table replication',
},
] as const
interface RetryOptionsDropdownProps {
tableId: number
tableName: string
}
export const RetryOptionsDropdown = ({ tableId, tableName }: RetryOptionsDropdownProps) => {
const { ref: projectRef, pipelineId: _pipelineId } = useParams()
const [isOpen, setIsOpen] = useState(false)
const { mutate: rollbackTable, isLoading: isRollingBack } = useRollbackTableMutation({
onSuccess: (_, vars) => {
const { projectRef, pipelineId } = vars
toast.success(`Table "${tableName}" rolled back successfully and pipeline is being restarted`)
startPipeline({ projectRef, pipelineId })
},
onError: (error, vars) => {
const { rollbackType } = vars
toast.error(
`Failed to ${rollbackType === 'full' ? 'reset' : 'rollback'} table: ${error.message}`
)
},
})
const { mutate: startPipeline, isLoading: isRestartingPipeline } = useStartPipelineMutation({
onSuccess: () => {
toast.success('Pipeline restarted successfully')
setIsOpen(false)
},
onError: (error) => {
toast.error(`Failed to restart pipeline: ${error.message}`)
setIsOpen(false)
},
})
const isLoading = isRollingBack || isRestartingPipeline
const handleRollback = async (rollbackType: RollbackType) => {
if (!projectRef) return toast.error('Project ref is required')
if (!_pipelineId) return toast.error('Pipeline ID is required')
const pipelineId = Number(_pipelineId)
rollbackTable({
projectRef,
pipelineId,
tableId,
rollbackType,
})
}
return (
}
aria-label={`Rollback ${tableName}`}
>
Rollback
{RETRY_OPTIONS.map((option) => (
handleRollback(option.type)}
>
{option.icon}
{option.title}
{option.description}
))}
)
}