mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## What
Moves the `ResponseError` class (`types/base.ts`) and the classified
error classes (`types/api-errors.ts`) into
`lib/errors/studio-errors.ts`, and renames `ResponseError` to
`StudioError`.
## Why
Studio's error classification is being expanded to cover errors that
don't come from API responses (thrown network failures, aborted
requests, unexpected client errors). The API-specific name and location
no longer fit. Landing the rename on its own keeps the follow-up PR,
which introduces the classified error model, small and reviewable.
## Notes for reviewers
- Mechanical change, no behavior change. Most of the diff is a one-line
import change per file plus import re-sorting by Prettier.
- Files that only use the type now use `import type { StudioError }`.
- `ResponseError` is no longer re-exported from `@/types`; there are no
compatibility shims at the old paths.
- Unrelated identifiers like `QueryResponseError` and
`parseResponseErrors` are unchanged.
97 lines
3.3 KiB
TypeScript
97 lines
3.3 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import type { QueryKey } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import type { ContentData } from './content-query'
|
|
import { contentKeys } from './keys'
|
|
import { del, handleError } from '@/data/fetchers'
|
|
import type { StudioError } from '@/lib/errors/studio-errors'
|
|
import type { UseCustomMutationOptions } from '@/types'
|
|
|
|
type DeleteContentVariables = { projectRef: string; ids: string[] }
|
|
type DeleteContext = { snapshots: [QueryKey, ContentData | undefined][] }
|
|
|
|
export async function deleteContents(
|
|
{ projectRef, ids }: DeleteContentVariables,
|
|
signal?: AbortSignal,
|
|
headersInit?: HeadersInit
|
|
) {
|
|
const headers = new Headers(headersInit)
|
|
headers.set('Version', '2')
|
|
|
|
const { data, error } = await del('/platform/projects/{ref}/content', {
|
|
headers,
|
|
params: {
|
|
path: { ref: projectRef },
|
|
query: { ids: ids.join(',') },
|
|
},
|
|
signal,
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data.map((x) => x.id)
|
|
}
|
|
|
|
type DeleteContentData = Awaited<ReturnType<typeof deleteContents>>
|
|
|
|
/** Returns a copy of a cached content list with the given ids removed. Exported for testing. */
|
|
export function removeContentFromList(data: ContentData, ids: string[]): ContentData {
|
|
return { ...data, content: data.content.filter((item) => !ids.includes(item.id)) }
|
|
}
|
|
|
|
export const useContentDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<DeleteContentData, StudioError, DeleteContentVariables, DeleteContext>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<DeleteContentData, StudioError, DeleteContentVariables, DeleteContext>({
|
|
mutationFn: (args) => deleteContents(args),
|
|
async onMutate({ projectRef, ids }) {
|
|
await queryClient.cancelQueries({ queryKey: contentKeys.allContentLists(projectRef) })
|
|
|
|
const snapshots = queryClient.getQueriesData<ContentData>({
|
|
queryKey: contentKeys.allContentLists(projectRef),
|
|
})
|
|
|
|
// allContentLists is a prefix of sibling caches (sql snippets, folders, counts) that
|
|
// don't share the { content: [...] } shape, so only optimistically update entries that do.
|
|
for (const [queryKey, data] of snapshots) {
|
|
if (data && Array.isArray(data.content)) {
|
|
queryClient.setQueryData<ContentData>(queryKey, removeContentFromList(data, ids))
|
|
}
|
|
}
|
|
|
|
return { snapshots }
|
|
},
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: contentKeys.allContentLists(projectRef) }),
|
|
queryClient.invalidateQueries({ queryKey: contentKeys.infiniteList(projectRef) }),
|
|
])
|
|
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(error, variables, context) {
|
|
// Restore the snapshots captured in onMutate before surfacing the error.
|
|
if (context?.snapshots) {
|
|
for (const [queryKey, data] of context.snapshots) {
|
|
queryClient.setQueryData(queryKey, data)
|
|
}
|
|
}
|
|
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete contents: ${error.message}`)
|
|
} else {
|
|
onError(error, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|