"use client" import { useState, useCallback } from "react" import { Upload, X } from "lucide-react" import { Button } from "@/components/ui/button" import { toast } from "sonner" interface ImageUploadProps { onImageSelect: (file: File) => void onImageRemove: () => void maxSizeMB?: number } export function ImageUpload({ onImageSelect, onImageRemove, maxSizeMB = 10 }: ImageUploadProps) { const [preview, setPreview] = useState(null) const [isDragging, setIsDragging] = useState(false) const validateAndProcessFile = useCallback( (file: File) => { // Validate file type if (!file.type.startsWith("image/")) { toast.error("Invalid file type", { description: "Please upload an image file (PNG, JPG, WEBP)", }) return false } // Validate file size const maxSizeBytes = maxSizeMB * 1024 * 1024 if (file.size > maxSizeBytes) { toast.error("File too large", { description: `Image must be under ${maxSizeMB}MB. Current size: ${(file.size / 1024 / 1024).toFixed(2)}MB`, }) return false } // Create preview const reader = new FileReader() reader.onload = () => setPreview(reader.result as string) reader.readAsDataURL(file) onImageSelect(file) return true }, [maxSizeMB, onImageSelect] ) const handleDrop = useCallback( (e: React.DragEvent) => { e.preventDefault() setIsDragging(false) const file = e.dataTransfer.files[0] if (file) { validateAndProcessFile(file) } }, [validateAndProcessFile] ) const handleFileInput = useCallback( (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) { validateAndProcessFile(file) } }, [validateAndProcessFile] ) const handleRemove = () => { setPreview(null) onImageRemove() } if (preview) { return (
Upload preview
) } return (
{ e.preventDefault() setIsDragging(true) }} onDragLeave={() => setIsDragging(false)} className={`rounded-lg border-2 border-dashed p-8 text-center transition cursor-pointer ${ isDragging ? "border-primary bg-primary/5" : "border-muted hover:border-primary/50 hover:bg-muted/50" }`} >
) }