Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.5 KB
Raw Blame
import { useQuery } from '@tanstack/react-query'

import { branchKeys } from './keys'
import type { components } from '@/data/api'
import { get, handleError } from '@/data/fetchers'
import { IS_PLATFORM } from '@/lib/constants'
import { EMPTY_ARR } from '@/lib/void'
import type { ResponseError, UseCustomQueryOptions } from '@/types'

export type BranchesVariables = {
  projectRef?: string
}

export type Branch = components['schemas']['BranchResponse_Output']

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 EMPTY_ARR
    } else {
      handleError(error)
    }
  }

  return Array.isArray(data) ? data : EMPTY_ARR
}

export type BranchesData = Awaited<ReturnType<typeof getBranches>>
export type BranchesError = ResponseError

export const useBranchesQuery = <TData = BranchesData>(
  { projectRef }: BranchesVariables,
  { enabled = true, ...options }: UseCustomQueryOptions<BranchesData, BranchesError, TData> = {}
) =>
  useQuery<BranchesData, BranchesError, TData>({
    queryKey: branchKeys.list(projectRef),
    queryFn: ({ signal }) => getBranches({ projectRef }, signal),
    enabled: IS_PLATFORM && enabled && Boolean(projectRef),
    ...options,
  })