Files
supabase/apps/studio/data/storage/bucket-create-mutation.ts
Ivan VasilovandAlaister Young 89c803551b feat: Analytic storage bucket (#37003)
* Migrate the CreateBucketModal to use RHF.

* Minor fixes for the create bucket modal.

* Use the bucket type from the API everywhere.

* Improve the types on some functions. Add "skip" mode to FDWCreateMutation.

* Expand the CreateBucketModal to explain what will happen when creating an iceberg bucket.

* Add a page for iceberg buckets.

* Add error states to the iceberg explorer.

* Fix the names for the FDWs.

* Add a dialog for setting up namespace.

* Restructure the new files.

* Add a download env button.

* Add warning to install the wrappers extension when creating an iceberg bucket.

* Add failover details when the FDW is not setup.

* Fix a lint error.

* Fix a type error.

* Minor fixes.

* Fix the server name.

* Add an icon for iceberg buckets.

* Fix the import foreign schema dialog.

* Make the setup wrapper button functional.

* Fix a bad useMemo dependency.

* Small changes to the iceberg bucket page.

* Minor fix for the edit wrapper sheet.

* Rename the files from kebab-case to PascalCase.

* Rename the files again to include Analytic instead of Iceberg.

* Rename Iceberg type to Analytic.

* Add a switch for creating namespace in the import foreign schema dialog.

* Fix the CreateBucketModal.

* Fix the delete modal feature.

* Fix the S3 keys in the FDW.

* Only create a namespace if the switch is true.

* Regenerate and fix the types.

* Fix the FDW create mutation to handle numbers in the FDW names.

* Make the icon smaller.

* Check whether the namespace exists, if it doesn't create it.

* Hide action from the analytic bucket which don't work.

* Invalidate namespaces when creating them.

* Add small explanation for the creation of namespaces.

* Minor fixes.

* Tons of changes to make the namespace feature work.

* Fix type errors.

* Fix bad import.

* Minor copy fixes.

* Replace the multiple cards with a table of namespaces.

* update copy icon

* tiny copy update

* Fix the empty state for foreign tables.

* Hide the analytics bucket option for self-hosted.

* Minor copy fixes.

* Expand the CTA on no namespaces state.

* More minor fixes.

* More small fixes.

---------

Co-authored-by: Alaister Young <a@alaisteryoung.com>
2025-07-15 14:18:38 +02: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'
export type BucketCreateVariables = Omit<CreateStorageBucketBody, 'public'> & {
projectRef: string
isPublic: boolean
}
type CreateStorageBucketBody = components['schemas']['CreateStorageBucketBody']
export 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,
}
)
}