mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 12:51:13 -05:00
Fix photo upload issues and improve readiness
This commit is contained in:
@@ -70,6 +70,7 @@ export function UppyPhotoUpload({
|
|||||||
const [isUploading, setIsUploading] = useState(false);
|
const [isUploading, setIsUploading] = useState(false);
|
||||||
const [uploadProgress, setUploadProgress] = useState(0);
|
const [uploadProgress, setUploadProgress] = useState(0);
|
||||||
const uppyRef = useRef<Uppy | null>(null);
|
const uppyRef = useRef<Uppy | null>(null);
|
||||||
|
const uploadedImagesRef = useRef<string[]>([]);
|
||||||
const { toast } = useToast();
|
const { toast } = useToast();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -81,6 +82,7 @@ export function UppyPhotoUpload({
|
|||||||
maxFileSize: maxSizeMB * 1024 * 1024,
|
maxFileSize: maxSizeMB * 1024 * 1024,
|
||||||
},
|
},
|
||||||
autoProceed: false,
|
autoProceed: false,
|
||||||
|
allowMultipleUploadBatches: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add Dashboard plugin
|
// Add Dashboard plugin
|
||||||
@@ -203,7 +205,9 @@ export function UppyPhotoUpload({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (imageData?.urls) {
|
if (imageData?.urls) {
|
||||||
setUploadedImages(prev => [...prev, imageData.urls!.public]);
|
const newUrl = imageData.urls.public;
|
||||||
|
uploadedImagesRef.current = [...uploadedImagesRef.current, newUrl];
|
||||||
|
setUploadedImages(prev => [...prev, newUrl]);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Upload post-processing failed:', error);
|
console.error('Upload post-processing failed:', error);
|
||||||
@@ -251,11 +255,15 @@ export function UppyPhotoUpload({
|
|||||||
setUploadProgress(0);
|
setUploadProgress(0);
|
||||||
|
|
||||||
if (result.successful.length > 0) {
|
if (result.successful.length > 0) {
|
||||||
onUploadComplete?.(uploadedImages);
|
// Use ref to get current uploaded images
|
||||||
|
onUploadComplete?.(uploadedImagesRef.current);
|
||||||
toast({
|
toast({
|
||||||
title: 'Upload Complete',
|
title: 'Upload Complete',
|
||||||
description: `Successfully uploaded ${result.successful.length} image(s)`,
|
description: `Successfully uploaded ${result.successful.length} image(s)`,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Clear Uppy's file list for next batch
|
||||||
|
uppy.cancelAll();
|
||||||
}
|
}
|
||||||
setIsModalOpen(false);
|
setIsModalOpen(false);
|
||||||
});
|
});
|
||||||
@@ -265,24 +273,48 @@ export function UppyPhotoUpload({
|
|||||||
return () => {
|
return () => {
|
||||||
uppy.destroy();
|
uppy.destroy();
|
||||||
};
|
};
|
||||||
}, [maxFiles, maxSizeMB, allowedFileTypes, metadata, variant, disabled, onUploadStart, onUploadComplete, onUploadError, toast, uploadedImages, size]);
|
}, [maxFiles, maxSizeMB, allowedFileTypes, metadata, variant, disabled, onUploadStart, onUploadComplete, onUploadError, toast, size]);
|
||||||
|
|
||||||
const handleDragDropFiles = async (files: File[]) => {
|
const handleDragDropFiles = async (files: File[]) => {
|
||||||
if (!uppyRef.current || disabled) return;
|
if (!uppyRef.current || disabled) return;
|
||||||
|
|
||||||
// Add files to Uppy and start upload
|
try {
|
||||||
files.forEach((file) => {
|
// Add files to Uppy
|
||||||
uppyRef.current?.addFile({
|
files.forEach((file) => {
|
||||||
source: 'drag-drop',
|
try {
|
||||||
name: file.name,
|
uppyRef.current?.addFile({
|
||||||
type: file.type,
|
source: 'drag-drop',
|
||||||
data: file,
|
name: file.name,
|
||||||
|
type: file.type,
|
||||||
|
data: file,
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
// Handle duplicate file error gracefully
|
||||||
|
if (error.message?.includes('duplicate')) {
|
||||||
|
console.log(`Skipping duplicate file: ${file.name}`);
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
// Auto-start upload after adding files
|
||||||
|
if (uppyRef.current.getFiles().length > 0) {
|
||||||
|
uppyRef.current.upload();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error adding files:', error);
|
||||||
|
toast({
|
||||||
|
variant: 'destructive',
|
||||||
|
title: 'Error',
|
||||||
|
description: 'Failed to add files for upload',
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const removeImage = (index: number) => {
|
const removeImage = (index: number) => {
|
||||||
const newUrls = uploadedImages.filter((_, i) => i !== index);
|
const newUrls = uploadedImages.filter((_, i) => i !== index);
|
||||||
|
uploadedImagesRef.current = newUrls;
|
||||||
setUploadedImages(newUrls);
|
setUploadedImages(newUrls);
|
||||||
if (onUploadComplete) {
|
if (onUploadComplete) {
|
||||||
onUploadComplete(newUrls);
|
onUploadComplete(newUrls);
|
||||||
@@ -390,14 +422,11 @@ export function UppyPhotoUpload({
|
|||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
<DragDropZone
|
<DragDropZone
|
||||||
onFilesAdded={(files) => {
|
onFilesAdded={handleDragDropFiles}
|
||||||
handleDragDropFiles(files);
|
|
||||||
handleOpenModal();
|
|
||||||
}}
|
|
||||||
maxFiles={maxFiles}
|
maxFiles={maxFiles}
|
||||||
maxSizeMB={maxSizeMB}
|
maxSizeMB={maxSizeMB}
|
||||||
allowedFileTypes={allowedFileTypes}
|
allowedFileTypes={allowedFileTypes}
|
||||||
disabled={disabled}
|
disabled={disabled || isUploading}
|
||||||
className="min-h-[200px]"
|
className="min-h-[200px]"
|
||||||
/>
|
/>
|
||||||
{renderPreview()}
|
{renderPreview()}
|
||||||
|
|||||||
Reference in New Issue
Block a user