"use client"; import { useState, useRef, type DragEvent, type ChangeEvent } from "react"; import clsx from "clsx"; import { api } from "@/lib/api"; interface ImageUploadProps { onUpload: (url: string) => void; } const ACCEPTED_TYPES = ["image/jpeg", "image/png", "image/webp"]; const MAX_SIZE = 5 * 1024 * 1024; // 5MB export function ImageUpload({ onUpload }: ImageUploadProps) { const [isDragging, setIsDragging] = useState(false); const [uploading, setUploading] = useState(false); const [error, setError] = useState(null); const inputRef = useRef(null); const validateFile = (file: File): string | null => { if (!ACCEPTED_TYPES.includes(file.type)) { return "Only JPG, PNG, and WebP files are allowed."; } if (file.size > MAX_SIZE) { return "File size must be under 5MB."; } return null; }; const uploadFile = async (file: File) => { const validationError = validateFile(file); if (validationError) { setError(validationError); setTimeout(() => setError(null), 3000); return; } setError(null); setUploading(true); try { const response = await api.uploadImage(file); onUpload(response.url); } catch { setError("Upload failed. Please try again."); setTimeout(() => setError(null), 3000); } finally { setUploading(false); } }; const handleDragOver = (e: DragEvent) => { e.preventDefault(); setIsDragging(true); }; const handleDragLeave = (e: DragEvent) => { e.preventDefault(); setIsDragging(false); }; const handleDrop = (e: DragEvent) => { e.preventDefault(); setIsDragging(false); const file = e.dataTransfer.files[0]; if (file) { uploadFile(file); } }; const handleChange = (e: ChangeEvent) => { const file = e.target.files?.[0]; if (file) { uploadFile(file); } }; const handleClick = () => { inputRef.current?.click(); }; return (
{uploading ? (

Uploading...

) : error ? (

{error}

) : (

Drop an image here or click to upload

)}

JPG, PNG, WebP up to 5MB

); }