Files
supabase/apps/studio/data/replication/use-table-reset.ts
Ivan Vasilov 0d5be306ef chore: Bump React Query to v5 (#40174)
* Bump the deps, refactor deprecated code.

* Migrate keepPreviousData usage.

* Migrate all uses of InfiniteQuery.

* Fix refetchInterval in queries.

* Migrate all use of isLoading to isPending in mutations.

* Fix accessing location in claim-project.

* Fix a bug in duplicate query keys.

* Migrate all queries to use isPending.

* Revert "Fix accessing location in claim-project."

This reverts commit 2a07df64b5.

* Revert the rss.xml file to master.
2025-12-10 10:10:29 +01:00

71 lines
2.0 KiB
TypeScript

import { useState } from 'react'
import { toast } from 'sonner'
import { useRestartPipelineHelper } from './restart-pipeline-helper'
import { RollbackType, useRollbackTableMutation } from './rollback-table-mutation'
interface UseTableResetParams {
tableName: string
onSuccess?: () => void
onError?: (error: Error) => void
}
/**
* Custom hook that encapsulates the logic for resetting a table and restarting the pipeline.
* Provides unified error handling and loading states for table reset operations.
*/
export const useTableReset = ({ tableName, onSuccess, onError }: UseTableResetParams) => {
const [isRestartingPipeline, setIsRestartingPipeline] = useState(false)
const { restartPipeline } = useRestartPipelineHelper()
const { mutate: rollbackTable, isPending: isRollingBack } = useRollbackTableMutation({
onSuccess: async (_, vars) => {
const { projectRef, pipelineId } = vars
toast.success(`Table "${tableName}" reset successfully and pipeline is being restarted`)
setIsRestartingPipeline(true)
try {
await restartPipeline({ projectRef, pipelineId })
toast.success('Pipeline restarted successfully')
onSuccess?.()
} catch (error: any) {
const errorMessage = `Failed to restart pipeline: ${error.message}`
toast.error(errorMessage)
onError?.(new Error(errorMessage))
} finally {
setIsRestartingPipeline(false)
}
},
onError: (error) => {
const errorMessage = `Failed to reset table: ${error.message}`
toast.error(errorMessage)
onError?.(new Error(errorMessage))
},
})
const resetTable = ({
projectRef,
pipelineId,
tableId,
rollbackType = 'full' as RollbackType,
}: {
projectRef: string
pipelineId: number
tableId: number
rollbackType?: RollbackType
}) => {
rollbackTable({
projectRef,
pipelineId,
tableId,
rollbackType,
})
}
return {
resetTable,
isRollingBack,
isRestartingPipeline,
isResetting: isRollingBack || isRestartingPipeline,
}
}