mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +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.
69 lines
2.2 KiB
TypeScript
69 lines
2.2 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { projectKeys } from './keys'
|
|
import { useInvalidateProjectsInfiniteQuery } from './org-projects-infinite-query'
|
|
import { del, handleError } from '@/data/fetchers'
|
|
import { useInvalidateFreeProjectLimitChecks } from '@/data/organizations/free-project-limit-check-query'
|
|
import { organizationKeys } from '@/data/organizations/keys'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { UseCustomMutationOptions } from '@/types'
|
|
|
|
export type ProjectDeleteVariables = {
|
|
projectRef: string
|
|
organizationSlug?: string
|
|
}
|
|
|
|
export async function deleteProject({ projectRef }: ProjectDeleteVariables) {
|
|
const { data, error } = await del('/platform/projects/{ref}', {
|
|
params: { path: { ref: projectRef } },
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type ProjectDeleteData = Awaited<ReturnType<typeof deleteProject>>
|
|
|
|
export const useProjectDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<ProjectDeleteData, StudioError, ProjectDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
const { invalidateProjectsQuery } = useInvalidateProjectsInfiniteQuery()
|
|
const { invalidateFreeProjectLimitChecks } = useInvalidateFreeProjectLimitChecks()
|
|
|
|
return useMutation<ProjectDeleteData, StudioError, ProjectDeleteVariables>({
|
|
mutationFn: (vars) => deleteProject(vars),
|
|
async onSuccess(data, variables, context) {
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: projectKeys.detail(data.ref) }),
|
|
invalidateFreeProjectLimitChecks(),
|
|
])
|
|
|
|
if (variables.organizationSlug) {
|
|
await Promise.all([
|
|
invalidateProjectsQuery(),
|
|
queryClient.invalidateQueries({
|
|
queryKey: organizationKeys.detail(variables.organizationSlug),
|
|
}),
|
|
])
|
|
}
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete project: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|