BucketFilePickerDialog.utils.tsx44 lines · main
1import { QueryClient } from '@tanstack/react-query'
2import { toast } from 'sonner'
3
4import { storageKeys } from '@/data/storage/keys'
5import { createProjectBrivenClient } from '@/lib/project-briven-client'
6
7export async function uploadFilesToBucket({
8 files,
9 projectRef,
10 hostEndpoint,
11 bucketName,
12 bucketId,
13 currentPath,
14 queryClient,
15}: {
16 files: File[]
17 projectRef: string
18 hostEndpoint: string
19 bucketName: string
20 bucketId: string
21 currentPath: string
22 queryClient: QueryClient
23}) {
24 if (files.length === 0) return
25
26 const client = await createProjectBrivenClient(projectRef, hostEndpoint)
27 let successCount = 0
28
29 for (const file of files) {
30 const filePath = currentPath ? `${currentPath}/${file.name}` : file.name
31 const { error } = await client.storage.from(bucketName).upload(filePath, file, { upsert: true })
32 if (error) {
33 toast.error(`Failed to upload ${file.name}: ${error.message}`)
34 } else {
35 successCount++
36 }
37 }
38
39 if (successCount > 0) {
40 toast.success(`Successfully uploaded ${successCount} file${successCount > 1 ? 's' : ''}`)
41 const queryKey = storageKeys.objects(projectRef, bucketId, '')
42 await queryClient.refetchQueries({ queryKey, type: 'active' })
43 }
44}