import { useRef, useState } from 'react'; export function UploadTab({ onAddImage }) { const fileInputRef = useRef(null); const [isDragging, setIsDragging] = useState(false); const [isUploading, setIsUploading] = useState(false); const handleFiles = async (files) => { const file = files[0]; if (!file) return; // Validate file type const validTypes = ['image/jpeg', 'image/png', 'image/webp']; if (!validTypes.includes(file.type)) { alert('Please upload a JPEG, PNG, or WebP image'); return; } // Validate file size (20MB) if (file.size > 20 * 1024 * 1024) { alert('File size must be under 20MB'); return; } setIsUploading(true); try { const formData = new FormData(); formData.append('image', file); const response = await fetch('/api/upload', { method: 'POST', body: formData, }); if (!response.ok) { throw new Error('Upload failed'); } const data = await response.json(); // Add the uploaded image to canvas (use preview for canvas) onAddImage({ type: 'image', x: 75, y: 75, width: 150, height: 150, rotation: 0, src: data.preview.url, originalUrl: data.original.url, }); } catch (error) { console.error('Upload error:', error); alert('Failed to upload image. Please try again.'); } finally { setIsUploading(false); } }; const handleDragOver = (e) => { e.preventDefault(); setIsDragging(true); }; const handleDragLeave = (e) => { e.preventDefault(); setIsDragging(false); }; const handleDrop = (e) => { e.preventDefault(); setIsDragging(false); handleFiles(e.dataTransfer.files); }; const handleClick = () => { fileInputRef.current?.click(); }; const handleFileChange = (e) => { handleFiles(e.target.files); }; return (

Upload Image

📁
Click to upload or drag and drop
JPEG, PNG, WebP (max 20MB)
{isUploading && (
Uploading...
)}
Tip: After uploading, you can remove the background from your image using the background removal tool.
); }