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

46 lines
1.4 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import type { components } from 'data/api'
import { get, handleError } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
import type { ResponseError } from 'types'
import { branchKeys } from './keys'
export type BranchesVariables = {
projectRef?: string
}
export type Branch = components['schemas']['BranchResponse']
export async function getBranches({ projectRef }: BranchesVariables, signal?: AbortSignal) {
if (!projectRef) throw new Error('Project ref is required')
const { data, error } = await get(`/v1/projects/{ref}/branches`, {
params: { path: { ref: projectRef } },
signal,
})
if (error) {
if ((error as ResponseError).message === 'Preview branching is not enabled for this project.') {
return []
} else {
handleError(error)
}
}
return data
}
export type BranchesData = Awaited<ReturnType<typeof getBranches>>
export type BranchesError = ResponseError
export const useBranchesQuery = <TData = BranchesData>(
{ projectRef }: BranchesVariables,
{ enabled = true, ...options }: UseQueryOptions<BranchesData, BranchesError, TData> = {}
) =>
useQuery<BranchesData, BranchesError, TData>(
branchKeys.list(projectRef),
({ signal }) => getBranches({ projectRef }, signal),
{ enabled: IS_PLATFORM && enabled && Boolean(projectRef), ...options }
)