Files
supabase/apps/studio/data/projects/project-upgrade-mutation.ts
T
Joshen LimandAlaister Young 9aabe4a40f Add number of database connections charts in database reports (#29637)
* Add number of database connections charts in database reports

* Fix type issue

* Fix more type issues

* Remove UI for requires_manual_intervention

* Update apps/studio/data/projects/project-upgrade-mutation.ts

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>

---------

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
2024-10-04 16:55:01 +08:00

49 lines
1.4 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import { toast } from 'sonner'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
export type ProjectUpgradeVariables = {
ref: string
target_version: number
}
export async function upgradeProject({ ref, target_version }: ProjectUpgradeVariables) {
const { data, error } = await post('/v1/projects/{ref}/upgrade', {
params: { path: { ref } },
// @ts-expect-error release_channel param is optional on API end, but not typed properly from generated types
body: { target_version: target_version.toString() },
})
if (error) handleError(error)
return data
}
type ProjectUpgradeData = Awaited<ReturnType<typeof upgradeProject>>
export const useProjectUpgradeMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<ProjectUpgradeData, ResponseError, ProjectUpgradeVariables>,
'mutationFn'
> = {}) => {
return useMutation<ProjectUpgradeData, ResponseError, ProjectUpgradeVariables>(
(vars) => upgradeProject(vars),
{
async onSuccess(data, variables, context) {
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to upgrade project: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}