From 22266a39c75b3fc61833deb07226762289c70500 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Mon, 29 Sep 2025 17:53:55 +0000 Subject: [PATCH] Fix: Resolve upload race condition --- src/components/upload/UppyPhotoUpload.tsx | 28 +++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/components/upload/UppyPhotoUpload.tsx b/src/components/upload/UppyPhotoUpload.tsx index 6de53e19..d37c7f99 100644 --- a/src/components/upload/UppyPhotoUpload.tsx +++ b/src/components/upload/UppyPhotoUpload.tsx @@ -130,8 +130,8 @@ export function UppyPhotoUpload({ setUploadProgress(0); onUploadStart?.(); - // Process each file to get upload URL - for (const file of files) { + // Process all files in parallel to get upload URLs + const uploadPromises = files.map(async (file) => { try { const response = await supabase.functions.invoke('upload-image', { body: { metadata, variant }, @@ -156,6 +156,8 @@ export function UppyPhotoUpload({ endpoint: result.uploadURL, }); } + + return { success: true, fileId: file.id }; } catch (error) { console.error('Failed to get upload URL:', error); uppy.removeFile(file.id); @@ -165,7 +167,22 @@ export function UppyPhotoUpload({ description: `Failed to prepare upload for ${file.name}`, }); onUploadError?.(error as Error); + return { success: false, fileId: file.id }; } + }); + + // Wait for all URL fetching to complete + const results = await Promise.all(uploadPromises); + + // Check if any files are ready to upload + const successfulFiles = results.filter(r => r.success); + if (successfulFiles.length > 0) { + // All URLs are fetched, now trigger the upload + uppy.upload(); + } else { + // No files ready, reset state + setIsUploading(false); + setUploadProgress(0); } }); @@ -279,7 +296,7 @@ export function UppyPhotoUpload({ if (!uppyRef.current || disabled) return; try { - // Add files to Uppy + // Add files to Uppy - the files-added event will handle URL fetching and upload files.forEach((file) => { try { uppyRef.current?.addFile({ @@ -298,10 +315,7 @@ export function UppyPhotoUpload({ } }); - // Auto-start upload after adding files - if (uppyRef.current.getFiles().length > 0) { - uppyRef.current.upload(); - } + // Don't call upload() here - the files-added handler will trigger it after URLs are ready } catch (error) { console.error('Error adding files:', error); toast({