Files

40 lines
1.1 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 = {
projectRef?: string
id?: string
}
export async function getBranch({ id }: BranchVariables, signal?: AbortSignal) {
if (!id) throw new Error('id is required')
const { data, error } = await get(`/v1/branches/{branch_id}`, {
params: { path: { branch_id: id } },
signal,
})
if (error) handleError(error)
return data
}
export type BranchData = Awaited<ReturnType<typeof getBranch>>
export type BranchError = ResponseError
export const useBranchQuery = <TData = BranchData>(
{ projectRef, id }: BranchVariables,
{ enabled = true, ...options }: UseQueryOptions<BranchData, BranchError, TData> = {}
) =>
useQuery<BranchData, BranchError, TData>(
branchKeys.detail(projectRef, id),
({ signal }) => getBranch({ id }, signal),
{
enabled: IS_PLATFORM && enabled && typeof id !== 'undefined',
...options,
}
)