Files
supabase/apps/studio/components/interfaces/Compute/Compute.utils.ts
T
Charis df742bdb3b refactor(studio): rename ResponseError to StudioError (#52707)
## What

Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.

## Why

Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.

## Notes for reviewers

- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
2026-11-03 11:09:30 -05:00

79 lines
2.9 KiB
TypeScript

import { COMPUTE_INSTANCE_NAME_WORDS, RUNTIMES, type RuntimeMeta } from './Compute.constants'
import type {
ComputeInstance,
ComputeInstanceAccess,
ComputeInstanceBuildState,
} from './Compute.types'
import { StudioError } from '@/lib/errors/studio-errors'
export interface ComputeInstanceFilters {
search: string
state: ComputeInstanceBuildState | 'all'
access: ComputeInstanceAccess | 'all'
}
export const filterComputeInstances = (
instances: ComputeInstance[],
filters: ComputeInstanceFilters
): ComputeInstance[] => {
const search = filters.search.trim().toLowerCase()
return instances.filter((instance) => {
const matchesSearch = instance.name.toLowerCase().includes(search)
const matchesState = filters.state === 'all' || instance.buildState === filters.state
const matchesAccess = filters.access === 'all' || instance.access === filters.access
return matchesSearch && matchesState && matchesAccess
})
}
export interface Page<T> {
items: T[]
currentPage: number
totalPages: number
startIndex: number
}
// Clamps the requested page so filtering down to fewer results never strands an empty page.
export const getPage = <T>(items: T[], requestedPage: number, pageSize: number): Page<T> => {
const totalPages = Math.max(1, Math.ceil(items.length / pageSize))
const currentPage = Math.min(Math.max(1, requestedPage), totalPages)
const startIndex = (currentPage - 1) * pageSize
return {
items: items.slice(startIndex, startIndex + pageSize),
currentPage,
totalPages,
startIndex,
}
}
export const getRuntimeMeta = (runtime: string | undefined): RuntimeMeta | undefined =>
runtime === undefined ? undefined : RUNTIMES[runtime]
export const formatRuntime = (runtime: string | undefined): string =>
getRuntimeMeta(runtime)?.label ?? runtime ?? 'Custom'
// The API reports size as e.g. "2gb-1vcpu"; render the parts when they parse, the raw value if not.
export const formatSize = (size: string): string => {
const match = size.match(/^(\d+)gb-(\d+)vcpu$/)
if (!match) return size
return `${match[1]} GB · ${match[2]} vCPU`
}
export const formatResources = (instance: ComputeInstance): string =>
`${formatSize(instance.size)} · ${instance.declaredInstances} inst`
// Suggests a friendly, already-valid starting name so the deploy dialog isn't blank.
export const generateComputeInstanceName = (): string => {
const word =
COMPUTE_INSTANCE_NAME_WORDS[Math.floor(Math.random() * COMPUTE_INSTANCE_NAME_WORDS.length)]
const number = Math.floor(Math.random() * 900000) + 100000
return `compute-${word}-${number}`
}
// A project outside the alpha allow-list gets a 404, not a 403.
export const isComputeUnavailable = (error: Error | null): boolean =>
error instanceof StudioError && error.code === 404
// An enrolled project still answers 403 when the caller lacks the compute permission.
export const isComputeForbidden = (error: Error | null): boolean =>
error instanceof StudioError && error.code === 403