mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 09:25:06 +03:00
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import { useMutation } from '@tanstack/react-query'
|
|
import type { components } from 'api-types'
|
|
import { toast } from 'sonner'
|
|
|
|
import { handleError, post } from '@/data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
export type ProjectRestartVariables = {
|
|
ref: string
|
|
identifier?: string
|
|
}
|
|
|
|
type RestartProjectBody = components['schemas']['RestartProjectBody']
|
|
|
|
export async function restartProject({ ref, identifier }: ProjectRestartVariables) {
|
|
const payload: RestartProjectBody = {}
|
|
if (identifier !== undefined) payload.database_identifier = identifier
|
|
|
|
const { data, error } = await post('/platform/projects/{ref}/restart', {
|
|
params: { path: { ref } },
|
|
body: payload,
|
|
})
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type ProjectRestartData = Awaited<ReturnType<typeof restartProject>>
|
|
|
|
export const useProjectRestartMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<ProjectRestartData, ResponseError, ProjectRestartVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
return useMutation<ProjectRestartData, ResponseError, ProjectRestartVariables>({
|
|
mutationFn: (vars) => restartProject(vars),
|
|
async onSuccess(data, variables, context) {
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to restart project: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|