Files
supabase/apps/studio/components/interfaces/Storage/DeleteBucketModal.tsx
47e1134a1d feat: Vector buckets - initial work (#39597)
* Initial work on vector buckets.

* Add more queries and UI.

* chore(studio): vector buckets contents (#39705)

* language

* match analytics buckets

* shared empty state

* proper loading state

* create dialog cleanup

* simplify copywriting

* proper details

* filtering

* use real data

* chore(studio): vector buckets polish A (#39759)

* empty state

* better checks

* bucket name in sheet

* fix typo

* sheet improvements

* rename vector index to vector table

* cleanup

* More work on the vector indexes.

* Add delete for bucket and indexes. Create index should now work.

* chore(studio): vector buckets polish B (#39794)

* loading

* delete modal

* fix modal behaviour

* Add modal for deleting vector buckets.

* Bring back the shimmering loader.

* chore(studio): vector buckets polish C (#39887)

* fix bucketname on deletion dialog

* empty states

* remove extraneous padding

* proper loading and not-found cases

* Minor fixes.

* Minor updates to the vector components.

* Add more instructions to the cursor rules.

* Some refactors and shifting files around

* Refactor analytics buckets detial to improve time to fcp

* Fix

* Refactors + simplify

* Last few nits, just leaving comments

* If the bucket is not empty, delete all indexes first.

---------

Co-authored-by: Danny White <3104761+dnywh@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-11-04 15:25:46 +01:00

182 lines
5.9 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { get as _get, find } from 'lodash'
import { useRouter } from 'next/router'
import { SubmitHandler, useForm } from 'react-hook-form'
import { toast } from 'sonner'
import z from 'zod'
import { useParams } from 'common'
import { useDatabasePoliciesQuery } from 'data/database-policies/database-policies-query'
import { useDatabasePolicyDeleteMutation } from 'data/database-policies/database-policy-delete-mutation'
import { AnalyticsBucket } from 'data/storage/analytics-buckets-query'
import { useBucketDeleteMutation } from 'data/storage/bucket-delete-mutation'
import { Bucket, useBucketsQuery } from 'data/storage/buckets-query'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import {
Button,
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogSection,
DialogSectionSeparator,
DialogTitle,
Form_Shadcn_,
FormControl_Shadcn_,
FormField_Shadcn_,
Input_Shadcn_,
} from 'ui'
import { Admonition } from 'ui-patterns'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { formatPoliciesForStorage } from './Storage.utils'
export interface DeleteBucketModalProps {
visible: boolean
bucket: Bucket | AnalyticsBucket
onClose: () => void
}
const formId = `delete-storage-bucket-form`
export const DeleteBucketModal = ({ visible, bucket, onClose }: DeleteBucketModalProps) => {
const router = useRouter()
const { ref: projectRef, bucketId } = useParams()
const { data: project } = useSelectedProjectQuery()
const schema = z.object({
confirm: z.literal(bucket.id, {
errorMap: () => ({ message: `Please enter "${bucket.id}" to confirm` }),
}),
})
const form = useForm<z.infer<typeof schema>>({
resolver: zodResolver(schema),
})
const { data } = useBucketsQuery({ projectRef })
const buckets = data ?? []
const { data: policies } = useDatabasePoliciesQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
schema: 'storage',
})
const { mutateAsync: deletePolicy } = useDatabasePolicyDeleteMutation()
const { mutate: deleteBucket, isLoading: isDeletingBucket } = useBucketDeleteMutation({
onSuccess: async () => {
if (!project) return console.error('Project is required')
// Clean up policies from the corresponding bucket that was deleted
const storageObjectsPolicies = (policies ?? []).filter((policy) => policy.table === 'objects')
const formattedStorageObjectPolicies = formatPoliciesForStorage(
buckets,
storageObjectsPolicies
)
const bucketPolicies = _get(
find(formattedStorageObjectPolicies, { name: bucket.id }),
['policies'],
[]
)
try {
await Promise.all(
bucketPolicies.map((policy: any) =>
deletePolicy({
projectRef: project?.ref,
connectionString: project?.connectionString,
originalPolicy: policy,
})
)
)
toast.success(`Successfully deleted bucket ${bucket.id}`)
if (!!bucketId) router.push(`/project/${projectRef}/storage/files`)
onClose()
} catch (error) {
toast.success(
`Successfully deleted bucket ${bucket.id}. However, there was a problem deleting the policies tied to the bucket. Please review them in the storage policies section`
)
}
},
})
const onSubmit: SubmitHandler<z.infer<typeof schema>> = async () => {
if (!projectRef) return console.error('Project ref is required')
if (!bucket) return console.error('No bucket is selected')
deleteBucket({ projectRef, id: bucket.id })
}
return (
<Dialog
open={visible}
onOpenChange={(open) => {
if (!open) onClose()
}}
>
<DialogContent aria-describedby={undefined}>
<DialogHeader>
<DialogTitle>Confirm deletion of {bucket.id}</DialogTitle>
</DialogHeader>
<DialogSectionSeparator />
<Admonition
type="destructive"
className="rounded-none border-x-0 border-t-0 mb-0"
title="You cannot recover this bucket once deleted."
description="All bucket data will be lost."
/>
<DialogSection>
<p className="text-sm">
Your bucket <span className="font-bold text-foreground">{bucket.id}</span> and all its
contents will be permanently deleted.
</p>
</DialogSection>
<DialogSectionSeparator />
<DialogSection>
<Form_Shadcn_ {...form}>
<form id={formId} onSubmit={form.handleSubmit(onSubmit)}>
<FormField_Shadcn_
key="confirm"
name="confirm"
control={form.control}
render={({ field }) => (
<FormItemLayout
name="confirm"
label={
<>
Type <span className="font-bold text-foreground">{bucket.id}</span> to
confirm.
</>
}
>
<FormControl_Shadcn_>
<Input_Shadcn_
id="confirm"
autoComplete="off"
{...field}
placeholder="Type bucket name"
/>
</FormControl_Shadcn_>
</FormItemLayout>
)}
/>
</form>
</Form_Shadcn_>
</DialogSection>
<DialogFooter>
<Button type="default" disabled={isDeletingBucket} onClick={onClose}>
Cancel
</Button>
<Button form={formId} htmlType="submit" type="danger" loading={isDeletingBucket}>
Delete bucket
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}