Files
62fe0fca4a fix(studio): guard branches and read-replicas queries against non-array 200 bodies (#50347)
## Summary
- `getBranches()` (`apps/studio/data/branches/branches-query.ts`) and
`getReadReplicas()` (`apps/studio/data/read-replicas/replicas-query.ts`)
cast the raw API response body to an array with no runtime check.
- When the endpoint returns a defined-but-non-array 200 body, the `??
[]` fallback in each consumer doesn't catch it (the value isn't
nullish), and the first `.find`/`.filter` call throws, crashing the
whole page via `globalErrorBoundary`.
- This is the same known class of bug already fixed elsewhere in the
codebase (e.g. `apps/studio/data/lint/lint-query.ts`, and the
`api-keys`/`oauthApps`/`secrets` fetchers) — applies the same
`Array.isArray(data) ? data : EMPTY_ARR` guard.

## Evidence (Sentry, past week)
- [SUPABASE-APP-KA2](https://supabase.sentry.io/issues/7722780387/) —
`(m??[]).find is not a function` in `ActivityStats.tsx`
(`branchesData.find`), full-page crash on `/dashboard/project/[ref]`.
- [SUPABASE-APP-KAE](https://supabase.sentry.io/issues/7729679561/) —
`u.filter is not a function` in `AWSPrivateLinkForm.tsx`
(`databases.filter`), full-page crash on
`/project/[ref]/settings/integrations`.

## Test plan
- [ ] Existing query hook tests still pass
- [ ] Manually verified `Array.isArray` guard mirrors the established
`lint-query.ts` pattern

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01RUrmUfMBpPqkgerh9onNTM

---
_Generated by [Claude
Code](https://claude.ai/code/session_01RUrmUfMBpPqkgerh9onNTM)_

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Ali Waseem <waseema393@gmail.com>
2026-09-14 19:16:41 +00:00

48 lines
1.5 KiB
TypeScript

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,
})