Files
supabase/apps/studio/data/branches/branch-create-mutation.ts
3f3ed433fc Remove enable branching modal (#36458)
* remove enable branching modal

* re-add github linker

* abstract away enable and disable

* toggle fixes

* update logic to lean on connection status

* update form logic

* sheet layout

* gitless flag

* fix side panel size

* copy changes

* remove import

* add cost

* allow connection details on create

* Fix TS issues

* Fix TS issues

* Couple of clean ups

* Revert hardcode in useFlag

* Fix TS

* layout issues and github check

* refactor

* refactor to use new field

* cleanup

* style

* clarification with github integration

* replace branch dropdown button

* update repo picker

* remove modal

* change defaults

* disable if not gitless and no connection

* fixes

* prevent editing on child branch

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
Co-authored-by: Kevin Grüneberg <k.grueneberg1994@gmail.com>
2025-07-02 21:58:45 +10:00

69 lines
1.7 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { branchKeys } from './keys'
export type BranchCreateVariables = {
is_default?: boolean
projectRef: string
branchName: string
gitBranch?: string
region?: string
}
export async function createBranch({
is_default,
projectRef,
branchName,
gitBranch,
region,
}: BranchCreateVariables) {
const { data, error } = await post('/v1/projects/{ref}/branches', {
params: {
path: { ref: projectRef },
},
body: {
is_default,
branch_name: branchName,
git_branch: gitBranch,
region: region,
},
})
if (error) handleError(error)
return data
}
type BranchCreateData = Awaited<ReturnType<typeof createBranch>>
export const useBranchCreateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<BranchCreateData, ResponseError, BranchCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<BranchCreateData, ResponseError, BranchCreateVariables>(
(vars) => createBranch(vars),
{
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries(branchKeys.list(projectRef))
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to create branch: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}