Files
supabase/apps/studio/data/projects/project-delete-mutation.ts
Joshen Lim 9efe2643e3 Joshen/fe 2141 swap organization settings page to use paginated projects (#40513)
* Use version 2 organization roles endpoint and fix all affected files + unit tests

* Update API codegen

* Replace all usage of old useProjectsQuery with useOrgProjectsInfiniteQuery

* Swap access callout for project roles to use collapsible instead

* Deprecate useProjectsQuery and clean up

* Update apps/studio/components/interfaces/Organization/TeamSettings/UpdateRolesPanel/UpdateRolesPanel.tsx

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>

---------

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
2025-11-18 13:26:09 +08:00

66 lines
2.0 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { del, handleError } from 'data/fetchers'
import { organizationKeys } from 'data/organizations/keys'
import type { ResponseError, UseCustomMutationOptions } from 'types'
import { projectKeys } from './keys'
import { useInvalidateProjectsInfiniteQuery } from './org-projects-infinite-query'
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, ResponseError, ProjectDeleteVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
const { invalidateProjectsQuery } = useInvalidateProjectsInfiniteQuery()
return useMutation<ProjectDeleteData, ResponseError, ProjectDeleteVariables>({
mutationFn: (vars) => deleteProject(vars),
async onSuccess(data, variables, context) {
await Promise.all([queryClient.invalidateQueries({ queryKey: projectKeys.detail(data.ref) })])
if (variables.organizationSlug) {
await Promise.all([
invalidateProjectsQuery(),
queryClient.invalidateQueries({
queryKey: organizationKeys.detail(variables.organizationSlug),
}),
queryClient.invalidateQueries({
queryKey: organizationKeys.freeProjectLimitCheck(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,
})
}