Files
supabase/apps/studio/data/branches/branch-restore-mutation.ts
Andrew ValleteauandAli Waseem 38176b82a3 feat(studio): setup soft-delete and easy branch restore (#40353)
* wip: branch deletion grace period

* fix: allow to force delete pending

* fix: update for staging

* fix: typo

* fix: revert soft-delete by default

* chore: revert branch-delete-mutation changes

* fix: revert platform.d.ts changes

* fix: use INVALID status as distinguisher

* fix: hide unusable options

* fix: apply review suggestion

Co-authored-by: Ali Waseem <waseema393@gmail.com>

* updated formatting

* fix: lint

* fix: react v5 update

---------

Co-authored-by: Ali Waseem <waseema393@gmail.com>
2025-12-18 12:14:52 +08:00

51 lines
1.5 KiB
TypeScript

import { type UseMutationOptions, useMutation, useQueryClient } from '@tanstack/react-query'
import { handleError, post } from 'data/fetchers'
import { toast } from 'sonner'
import type { ResponseError } from 'types'
import { branchKeys } from './keys'
export type BranchRestoreVariables = {
branchRef: string
projectRef: string
}
export async function restoreBranch({ branchRef }: BranchRestoreVariables) {
const { data, error } = await post('/v1/branches/{branch_id_or_ref}/restore', {
params: {
path: { branch_id_or_ref: branchRef },
},
})
if (error) handleError(error)
return data
}
type BranchRestoreData = Awaited<ReturnType<typeof restoreBranch>>
export const useBranchRestoreMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<BranchRestoreData, ResponseError, BranchRestoreVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<BranchRestoreData, ResponseError, BranchRestoreVariables>({
mutationFn: (vars) => restoreBranch(vars),
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries({ queryKey: branchKeys.list(projectRef) })
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to restore branch: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}