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> export type BranchError = ResponseError export const useBranchQuery = ( { projectRef, id }: BranchVariables, { enabled = true, ...options }: UseQueryOptions = {} ) => useQuery( branchKeys.detail(projectRef, id), ({ signal }) => getBranch({ id }, signal), { enabled: IS_PLATFORM && enabled && typeof id !== 'undefined', ...options, } )