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> export const useProjectRestartMutation = ({ onSuccess, onError, ...options }: Omit< UseCustomMutationOptions, 'mutationFn' > = {}) => { return useMutation({ 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, }) }