1- import { get as _get , find } from 'lodash'
21import { useRouter } from 'next/router'
32import { toast } from 'sonner'
43
@@ -7,9 +6,9 @@ import { TextConfirmModal } from 'components/ui/TextConfirmModalWrapper'
76import { useDatabasePoliciesQuery } from 'data/database-policies/database-policies-query'
87import { useDatabasePolicyDeleteMutation } from 'data/database-policies/database-policy-delete-mutation'
98import { useBucketDeleteMutation } from 'data/storage/bucket-delete-mutation'
10- import { Bucket , useBucketsQuery } from 'data/storage/buckets-query'
9+ import { Bucket } from 'data/storage/buckets-query'
1110import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
12- import { formatPoliciesForStorage } from './Storage.utils'
11+ import { extractBucketNameFromDefinition } from './Storage.utils'
1312
1413export interface DeleteBucketModalProps {
1514 visible : boolean
@@ -22,9 +21,6 @@ export const DeleteBucketModal = ({ visible, bucket, onClose }: DeleteBucketModa
2221 const { ref : projectRef , bucketId } = useParams ( )
2322 const { data : project } = useSelectedProjectQuery ( )
2423
25- const { data } = useBucketsQuery ( { projectRef } )
26- const buckets = data ?? [ ]
27-
2824 const { data : policies } = useDatabasePoliciesQuery ( {
2925 projectRef : project ?. ref ,
3026 connectionString : project ?. connectionString ,
@@ -39,20 +35,16 @@ export const DeleteBucketModal = ({ visible, bucket, onClose }: DeleteBucketModa
3935 if ( ! project ) return console . error ( 'Project is required' )
4036
4137 // Clean up policies from the corresponding bucket that was deleted
42- const storageObjectsPolicies = ( policies ?? [ ] ) . filter ( ( policy ) => policy . table === 'objects' )
43- const formattedStorageObjectPolicies = formatPoliciesForStorage (
44- buckets ,
45- storageObjectsPolicies
46- )
47- const bucketPolicies = _get (
48- find ( formattedStorageObjectPolicies , { name : bucket . id } ) ,
49- [ 'policies' ] ,
50- [ ]
51- )
38+ const bucketPolicies = ( policies ?? [ ] ) . filter ( ( policy ) => {
39+ if ( policy . table !== 'objects' ) return false
40+
41+ const policyBucket = extractBucketNameFromDefinition ( policy . definition ?? policy . check )
42+ return policyBucket === bucket . name
43+ } )
5244
5345 try {
5446 await Promise . all (
55- bucketPolicies . map ( ( policy : any ) =>
47+ bucketPolicies . map ( ( policy ) =>
5648 deletePolicy ( {
5749 projectRef : project ?. ref ,
5850 connectionString : project ?. connectionString ,
0 commit comments