mirror of
https://github.com/supabase/supabase.git
synced 2026-10-18 23:59:08 +03:00
* ref: Unify /v1 api requests, remove API_ADMIN_URL constant * remove custom types --------- Co-authored-by: Alaister Young <a@alaisteryoung.com>
69 lines
2.3 KiB
TypeScript
69 lines
2.3 KiB
TypeScript
import { DatabaseUpgradeStatus } from '@supabase/shared-types/out/events'
|
|
import { useQuery, useQueryClient, UseQueryOptions } from '@tanstack/react-query'
|
|
import { get, handleError } from 'data/fetchers'
|
|
import { PROJECT_STATUS } from 'lib/constants'
|
|
import { configKeys } from './keys'
|
|
|
|
export type ProjectUpgradingStatusVariables = {
|
|
projectRef?: string
|
|
projectStatus?: string
|
|
}
|
|
|
|
export async function getProjectUpgradingStatus(
|
|
{ projectRef }: ProjectUpgradingStatusVariables,
|
|
signal?: AbortSignal
|
|
) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
|
|
const { data, error } = await get(`/v1/projects/{ref}/upgrade/status`, {
|
|
params: { path: { ref: projectRef } },
|
|
signal,
|
|
})
|
|
if (error) handleError(error)
|
|
|
|
return data
|
|
}
|
|
|
|
export type ProjectUpgradingStatusData = Awaited<ReturnType<typeof getProjectUpgradingStatus>>
|
|
export type ProjectUpgradingStatusError = unknown
|
|
|
|
export const useProjectUpgradingStatusQuery = <TData = ProjectUpgradingStatusData>(
|
|
{ projectRef, projectStatus }: ProjectUpgradingStatusVariables,
|
|
{
|
|
enabled = true,
|
|
...options
|
|
}: UseQueryOptions<ProjectUpgradingStatusData, ProjectUpgradingStatusError, TData> = {}
|
|
) => {
|
|
const client = useQueryClient()
|
|
|
|
return useQuery<ProjectUpgradingStatusData, ProjectUpgradingStatusError, TData>(
|
|
configKeys.upgradeStatus(projectRef),
|
|
({ signal }) => getProjectUpgradingStatus({ projectRef }, signal),
|
|
{
|
|
enabled: enabled && typeof projectRef !== 'undefined',
|
|
refetchInterval(data) {
|
|
const response = data as unknown as ProjectUpgradingStatusData
|
|
if (!response) return false
|
|
|
|
const interval =
|
|
// Transited to UPGRADING state via client, but job not yet picked up
|
|
(projectStatus === PROJECT_STATUS.UPGRADING &&
|
|
response.databaseUpgradeStatus?.status !== DatabaseUpgradeStatus.Upgrading) ||
|
|
// Project currently getting upgraded
|
|
response.databaseUpgradeStatus?.status === DatabaseUpgradeStatus.Upgrading
|
|
? 5000
|
|
: false
|
|
|
|
return interval
|
|
},
|
|
onSuccess(data) {
|
|
const response = data as unknown as ProjectUpgradingStatusData
|
|
if (response.databaseUpgradeStatus?.status === DatabaseUpgradeStatus.Upgraded) {
|
|
client.invalidateQueries(configKeys.upgradeEligibility(projectRef))
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|