Files
supabase/apps/studio/data/projects/project-pause-mutation.ts
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

49 lines
1.5 KiB
TypeScript

import { useMutation } from '@tanstack/react-query'
import { toast } from 'sonner'
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 ProjectPauseVariables = {
ref: string
}
export async function pauseProject({ ref }: ProjectPauseVariables) {
const { data, error } = await post('/platform/projects/{ref}/pause', {
params: { path: { ref } },
})
if (error) handleError(error)
return data
}
type ProjectPauseData = Awaited<ReturnType<typeof pauseProject>>
export const useProjectPauseMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<ProjectPauseData, StudioError, ProjectPauseVariables>,
'mutationFn'
> = {}) => {
const { invalidateFreeProjectLimitChecks } = useInvalidateFreeProjectLimitChecks()
return useMutation<ProjectPauseData, StudioError, ProjectPauseVariables>({
mutationFn: (vars) => pauseProject(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 pause project: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}