Files
supabase/apps/studio/data/storage/bucket-create-mutation.ts
Danny WhiteandJoshen Lim bd57a73521 feat: clearer upload limits (#38272)
* 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>
2025-09-03 12:05:34 +10:00

73 lines
2.1 KiB
TypeScript

import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { components } from 'api-types'
import { handleError, post } from 'data/fetchers'
import type { ResponseError } from 'types'
import { storageKeys } from './keys'
type BucketCreateVariables = Omit<CreateStorageBucketBody, 'public'> & {
projectRef: string
isPublic: boolean
}
type CreateStorageBucketBody = components['schemas']['CreateStorageBucketBody']
async function createBucket({
projectRef,
id,
type,
isPublic,
file_size_limit,
allowed_mime_types,
}: BucketCreateVariables) {
if (!projectRef) throw new Error('projectRef is required')
if (!id) throw new Error('Bucket name is required')
const payload: CreateStorageBucketBody = { id, type, public: isPublic }
if (type === 'STANDARD') {
if (file_size_limit) payload.file_size_limit = file_size_limit
if (allowed_mime_types) payload.allowed_mime_types = allowed_mime_types
}
const { data, error } = await post('/platform/storage/{ref}/buckets', {
params: { path: { ref: projectRef } },
body: payload,
})
if (error) handleError(error)
return data as { name: string }
}
type BucketCreateData = Awaited<ReturnType<typeof createBucket>>
export const useBucketCreateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<BucketCreateData, ResponseError, BucketCreateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<BucketCreateData, ResponseError, BucketCreateVariables>(
(vars) => createBucket(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 create bucket: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}