import { useQuery } from '@tanstack/react-query' import { branchKeys } from './keys' import { get, handleError } from '@/data/fetchers' import { IS_PLATFORM } from '@/lib/constants' import type { ResponseError, UseCustomQueryOptions } from '@/types' 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> export type BranchError = ResponseError export const useBranchQuery = ( { projectRef, branchRef }: BranchVariables, { enabled = true, ...options }: UseCustomQueryOptions = {} ) => useQuery({ queryKey: branchKeys.detail(projectRef, branchRef), queryFn: ({ signal }) => getBranch({ branchRef }, signal), enabled: IS_PLATFORM && enabled && Boolean(branchRef), ...options, })