mirror of
https://github.com/supabase/supabase.git
synced 2026-10-10 20:05:06 +03:00
- Fix 'occured' -> 'occurred' in ErrorDetailsButton.tsx - Fix 'requried' -> 'required' in storage bucket mutations - Fix 'reponse' -> 'response' in API spec files - Fix 'localy' -> 'locally' and 'depoy' -> 'deploy' in README Co-authored-by: Chris Chinchilla <chris.ward@supabase.io>
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { handleError, post } from 'data/fetchers'
|
|
import type { ResponseError, UseCustomMutationOptions } from 'types'
|
|
import { storageKeys } from './keys'
|
|
|
|
export type BucketEmptyVariables = {
|
|
projectRef: string
|
|
id: string
|
|
}
|
|
|
|
export async function emptyBucket({ projectRef, id }: BucketEmptyVariables) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
if (!id) throw new Error('Bucket name is required')
|
|
|
|
const { data, error } = await post('/platform/storage/{ref}/buckets/{id}/empty', {
|
|
params: { path: { id, ref: projectRef } },
|
|
})
|
|
|
|
if (error) handleError(error)
|
|
return data
|
|
}
|
|
|
|
type BucketEmptyData = Awaited<ReturnType<typeof emptyBucket>>
|
|
|
|
export const useBucketEmptyMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<BucketEmptyData, ResponseError, BucketEmptyVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<BucketEmptyData, ResponseError, BucketEmptyVariables>({
|
|
mutationFn: (vars) => emptyBucket(vars),
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries({ queryKey: storageKeys.buckets(projectRef) })
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to empty bucket: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|