mirror of
https://github.com/supabase/supabase.git
synced 2026-10-14 13:49:09 +03:00
* feat: basic clarity improvements * feat: all switches * feat: basic errors * fix: simplify * fix: properly separate sections * fix: remove smart toggle closure * fix: remember file size unit * fix: validation * fix: UpgradeToPro styles * fix: UpgradeToPro styles for full-width * feat: create modal match * fix: tests * Small fix for error message and desc for file size limit in create and edit bucket modals * Prettify error message handling for storage settings * Nit * set default unit as MB * dotted underline and default cursor for tooltip * fix: default MB tests * minor form style and copy improvements * remove extraneous divs * fix mime check on bucket creation * Clean up + minor refactors including improving perms loading state in storage settings * EditBucketModal ensure that file size limit unit defaults to MB if bucket doesnt have a file size limit * Refactor EditBucketModal to use react query mutation * Refactor * Fix unit tests for edit bucket modal * last round of clean up * last fix * Tiny unrelated fix --------- Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { del, handleError, post } from 'data/fetchers'
|
|
import type { ResponseError } from 'types'
|
|
import { BucketType } from './buckets-query'
|
|
import { storageKeys } from './keys'
|
|
|
|
type BucketDeleteVariables = {
|
|
projectRef: string
|
|
id: string
|
|
type: BucketType
|
|
}
|
|
|
|
async function deleteBucket({ projectRef, id, type }: BucketDeleteVariables) {
|
|
if (!projectRef) throw new Error('projectRef is required')
|
|
if (!id) throw new Error('Bucket name is requried')
|
|
|
|
if (type !== 'ANALYTICS') {
|
|
const { error: emptyBucketError } = await post('/platform/storage/{ref}/buckets/{id}/empty', {
|
|
params: { path: { ref: projectRef, id } },
|
|
})
|
|
if (emptyBucketError) handleError(emptyBucketError)
|
|
}
|
|
|
|
const { data, error: deleteBucketError } = await del('/platform/storage/{ref}/buckets/{id}', {
|
|
params: { path: { ref: projectRef, id }, query: { type } },
|
|
} as any)
|
|
if (deleteBucketError) handleError(deleteBucketError)
|
|
return data
|
|
}
|
|
|
|
type BucketDeleteData = Awaited<ReturnType<typeof deleteBucket>>
|
|
|
|
export const useBucketDeleteMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseMutationOptions<BucketDeleteData, ResponseError, BucketDeleteVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<BucketDeleteData, ResponseError, BucketDeleteVariables>(
|
|
(vars) => deleteBucket(vars),
|
|
{
|
|
async onSuccess(data, variables, context) {
|
|
const { projectRef } = variables
|
|
await queryClient.invalidateQueries(storageKeys.buckets(projectRef))
|
|
await onSuccess?.(data, variables, context)
|
|
},
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to delete bucket: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
}
|
|
)
|
|
}
|