Files
supabase/apps/studio/data/config/project-upgrade-eligibility-query.ts
T
Joshen Lim cab0585533 Fe 1799/consolidate to useselectedprojectquery and (#37684)
* Replace all usage of useProjectContext with useSelectedProjectQuery

* Replace all usage of useSelectedProject with useSelectedProjectQuery

* Replace all usage of useProjectByRef with useProjectByRefQuery

* Replace all usage of useSelectedOrganization with useSelectedOrganizationQuery

* Deprecate useSelectedProject, useSelectedOrganization, and useProjectByRef hooks

* Deprecate ProjecContext
2025-08-06 10:53:10 +07:00

56 lines
2.0 KiB
TypeScript

import { useQuery, UseQueryOptions } from '@tanstack/react-query'
import { components } from 'api-types'
import { IS_PLATFORM } from 'common'
import { get, handleError } from 'data/fetchers'
import { useProjectByRefQuery } from 'hooks/misc/useSelectedProject'
import { PROJECT_STATUS } from 'lib/constants/infrastructure'
import type { ResponseError } from 'types'
import { configKeys } from './keys'
export type ProjectUpgradeTargetVersion = { postgres_version: string; release_channel: string }
export type ProjectUpgradeEligibilityVariables = { projectRef?: string }
export type ProjectUpgradeEligibilityResponse =
components['schemas']['ProjectUpgradeEligibilityResponse']
export async function getProjectUpgradeEligibility(
{ projectRef }: ProjectUpgradeEligibilityVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await get('/v1/projects/{ref}/upgrade/eligibility', {
params: { path: { ref: projectRef } },
signal,
})
if (error) handleError(error)
return data
}
export type ProjectUpgradeEligibilityData = Awaited<ReturnType<typeof getProjectUpgradeEligibility>>
export type ProjectUpgradeEligibilityError = ResponseError
export const useProjectUpgradeEligibilityQuery = <TData = ProjectUpgradeEligibilityData>(
{ projectRef }: ProjectUpgradeEligibilityVariables,
{
enabled = true,
...options
}: UseQueryOptions<ProjectUpgradeEligibilityData, ProjectUpgradeEligibilityError, TData> = {}
) => {
const { data: project } = useProjectByRefQuery(projectRef)
return useQuery<ProjectUpgradeEligibilityData, ProjectUpgradeEligibilityError, TData>(
configKeys.upgradeEligibility(projectRef),
({ signal }) => getProjectUpgradeEligibility({ projectRef }, signal),
{
enabled:
enabled &&
project !== undefined &&
project.status === PROJECT_STATUS.ACTIVE_HEALTHY &&
typeof projectRef !== 'undefined' &&
IS_PLATFORM,
...options,
}
)
}