Files
supabase/apps/studio/data/branches/branch-query.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

40 lines
1.2 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { get, handleError } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
import type { ResponseError } from 'types'
import { branchKeys } from './keys'
export type BranchVariables = {
branchRef?: string
projectRef?: string
}
export async function getBranch({ branchRef }: BranchVariables, signal?: AbortSignal) {
if (!branchRef) throw new Error('branchRef is required')
const { data, error } = await get(`/v1/branches/{branch_id_or_ref}`, {
params: { path: { branch_id_or_ref: branchRef } },
signal,
})
if (error) handleError(error)
return data
}
export type BranchData = Awaited<ReturnType<typeof getBranch>>
export type BranchError = ResponseError
export const useBranchQuery = <TData = BranchData>(
{ projectRef, branchRef }: BranchVariables,
{ enabled = true, ...options }: UseQueryOptions<BranchData, BranchError, TData> = {}
) =>
useQuery<BranchData, BranchError, TData>(
branchKeys.detail(projectRef, branchRef),
({ signal }) => getBranch({ branchRef }, signal),
{
enabled: IS_PLATFORM && enabled && Boolean(branchRef),
...options,
}
)