mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
When pausing/creating/restoring/cloning a project, we are not clearing the react-query cache for the free project limit check and therefore still displaying potentially wrong states to the user. PR addresses this by clearing the free project limit react query cache on relevant project operations.
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { useMutation } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { PostgresEngine, ReleaseChannel } from './new-project.constants'
|
|
import { handleError, post } from '@/data/fetchers'
|
|
import { useInvalidateFreeProjectLimitChecks } from '@/data/organizations/free-project-limit-check-query'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { UseCustomMutationOptions } from '@/types'
|
|
|
|
export type ProjectRestoreVariables = {
|
|
ref: string
|
|
postgresEngine?: Exclude<PostgresEngine, '13' | '14'>
|
|
releaseChannel?: ReleaseChannel
|
|
}
|
|
|
|
export async function restoreProject({
|
|
ref,
|
|
postgresEngine,
|
|
releaseChannel,
|
|
}: ProjectRestoreVariables) {
|
|
const { data, error } = await post('/platform/projects/{ref}/restore', {
|
|
params: { path: { ref } },
|
|
body: {
|
|
postgres_engine: postgresEngine,
|
|
release_channel: releaseChannel,
|
|
},
|
|
})
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type ProjectRestoreData = Awaited<ReturnType<typeof restoreProject>>
|
|
|
|
export const useProjectRestoreMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<ProjectRestoreData, StudioError, ProjectRestoreVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const { invalidateFreeProjectLimitChecks } = useInvalidateFreeProjectLimitChecks()
|
|
|
|
return useMutation<ProjectRestoreData, StudioError, ProjectRestoreVariables>({
|
|
mutationFn: (vars) => restoreProject(vars),
|
|
async onSuccess(data, variables, context) {
|
|
await invalidateFreeProjectLimitChecks()
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to restore project: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|