Files
supabase/apps/studio/data/branches/branch-delete-mutation.ts
Han QiaoandCharis 77561e8e09 chore: replace branch id with branch ref (#38735)
* chore: remove unused mutation to disable branching

* chore: use branch ref in delete mutation

* chore: use branch ref in diff query

* chore: use branch ref in push mutation

* chore: use branch ref in detail query

* chore: use branch ref in update mutation

* Update apps/studio/data/branches/branch-query.ts

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>

---------

Co-authored-by: Charis <26616127+charislam@users.noreply.github.com>
2025-09-17 10:31:01 +08:00

64 lines
1.9 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { del, handleError } from 'data/fetchers'
import type { ResponseError } from 'types'
import { BranchesData } from './branches-query'
import { branchKeys } from './keys'
export type BranchDeleteVariables = {
branchRef: string
projectRef: string
}
export async function deleteBranch({ branchRef }: Pick<BranchDeleteVariables, 'branchRef'>) {
const { data, error } = await del('/v1/branches/{branch_id_or_ref}', {
params: { path: { branch_id_or_ref: branchRef } },
})
if (error) handleError(error)
return data
}
type BranchDeleteData = Awaited<ReturnType<typeof deleteBranch>>
export const useBranchDeleteMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<BranchDeleteData, ResponseError, BranchDeleteVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<BranchDeleteData, ResponseError, BranchDeleteVariables>(
(vars) => deleteBranch(vars),
{
async onSuccess(data, variables, context) {
const { branchRef, projectRef } = variables
setTimeout(() => {
queryClient.invalidateQueries(branchKeys.list(projectRef))
}, 5000)
const branches: BranchesData | undefined = queryClient.getQueryData(
branchKeys.list(projectRef)
)
if (branches) {
const updatedBranches = branches.filter((branch) => branch.project_ref !== branchRef)
queryClient.setQueryData(branchKeys.list(projectRef), updatedBranches)
}
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to delete branch: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}