Files
K-Dog (Kevin) 0246fb34b5 fix: clear free project limit cache on project operations (#54108)
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.
2026-11-26 16:16:45 +08:00

75 lines
2.1 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { projectKeys } from './keys'
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 ProjectCloneVariables = {
projectRef: string
newProjectName: string
newDbPass: string
cloneBackupId?: number
recoveryTimeTarget?: number
}
export async function triggerClone({
projectRef,
newProjectName,
cloneBackupId,
newDbPass,
recoveryTimeTarget,
}: ProjectCloneVariables) {
if (!projectRef) throw new Error('Project ref is required')
const { data, error } = await post('/platform/database/{ref}/clone', {
params: { path: { ref: projectRef } },
body: {
cloneBackupId,
newProjectName,
newDbPass,
recoveryTimeTarget,
},
})
if (error) handleError(error)
return data
}
type ProjectCloneData = Awaited<ReturnType<typeof triggerClone>>
export const useProjectCloneMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<ProjectCloneData, StudioError, ProjectCloneVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
const { invalidateFreeProjectLimitChecks } = useInvalidateFreeProjectLimitChecks()
return useMutation<ProjectCloneData, StudioError, ProjectCloneVariables>({
mutationFn: (vars) => triggerClone(vars),
async onSuccess(data, variables, context) {
await Promise.all([
queryClient.invalidateQueries({
queryKey: projectKeys.listCloneBackups(variables.projectRef),
}),
invalidateFreeProjectLimitChecks(),
])
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
console.error(data)
toast.error(`Failed to trigger clone: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}