mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 16:31:12 -05:00
Fix: Resolve upload race condition
This commit is contained in:
@@ -130,8 +130,8 @@ export function UppyPhotoUpload({
|
|||||||
setUploadProgress(0);
|
setUploadProgress(0);
|
||||||
onUploadStart?.();
|
onUploadStart?.();
|
||||||
|
|
||||||
// Process each file to get upload URL
|
// Process all files in parallel to get upload URLs
|
||||||
for (const file of files) {
|
const uploadPromises = files.map(async (file) => {
|
||||||
try {
|
try {
|
||||||
const response = await supabase.functions.invoke('upload-image', {
|
const response = await supabase.functions.invoke('upload-image', {
|
||||||
body: { metadata, variant },
|
body: { metadata, variant },
|
||||||
@@ -156,6 +156,8 @@ export function UppyPhotoUpload({
|
|||||||
endpoint: result.uploadURL,
|
endpoint: result.uploadURL,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return { success: true, fileId: file.id };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to get upload URL:', error);
|
console.error('Failed to get upload URL:', error);
|
||||||
uppy.removeFile(file.id);
|
uppy.removeFile(file.id);
|
||||||
@@ -165,7 +167,22 @@ export function UppyPhotoUpload({
|
|||||||
description: `Failed to prepare upload for ${file.name}`,
|
description: `Failed to prepare upload for ${file.name}`,
|
||||||
});
|
});
|
||||||
onUploadError?.(error as Error);
|
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;
|
if (!uppyRef.current || disabled) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Add files to Uppy
|
// Add files to Uppy - the files-added event will handle URL fetching and upload
|
||||||
files.forEach((file) => {
|
files.forEach((file) => {
|
||||||
try {
|
try {
|
||||||
uppyRef.current?.addFile({
|
uppyRef.current?.addFile({
|
||||||
@@ -298,10 +315,7 @@ export function UppyPhotoUpload({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Auto-start upload after adding files
|
// Don't call upload() here - the files-added handler will trigger it after URLs are ready
|
||||||
if (uppyRef.current.getFiles().length > 0) {
|
|
||||||
uppyRef.current.upload();
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error adding files:', error);
|
console.error('Error adding files:', error);
|
||||||
toast({
|
toast({
|
||||||
|
|||||||
Reference in New Issue
Block a user