import React, { useState, useRef, useEffect } from 'react'; import { UploadCloudIcon, SparklesIcon } from './icons'; interface ImageUploaderProps { onAnalyze: (imageBase64: string, species: string, location: string) => void; isAnalyzing: boolean; defaultSpecies?: string; defaultLocation?: string; disabled?: boolean; } const ImageUploader: React.FC = ({ onAnalyze, isAnalyzing, defaultSpecies = '', defaultLocation = '', disabled = false }) => { const [imagePreview, setImagePreview] = useState(null); const [imageBase64, setImageBase64] = useState(''); const [species, setSpecies] = useState(defaultSpecies); const [location, setLocation] = useState(defaultLocation); const [error, setError] = useState(''); const fileInputRef = useRef(null); useEffect(() => { setSpecies(defaultSpecies); setLocation(defaultLocation); }, [defaultSpecies, defaultLocation]); const handleFileChange = (event: React.ChangeEvent) => { const file = event.target.files?.[0]; if (file) { if (file.size > 4 * 1024 * 1024) { // 4MB limit setError("File size exceeds 4MB. Please upload a smaller image."); return; } const reader = new FileReader(); reader.onloadend = () => { const base64String = (reader.result as string).split(',')[1]; setImagePreview(reader.result as string); setImageBase64(base64String); setError(''); }; reader.onerror = () => { setError("Failed to read the file."); } reader.readAsDataURL(file); } }; const handleAnalyzeClick = () => { if (!imageBase64) { setError('Please upload an image of your bonsai.'); return; } if (!species.trim()) { setError('Please enter the bonsai species.'); return; } if (!location.trim()) { setError('Please enter your city or region for climate data.'); return; } setError(''); onAnalyze(imageBase64, species, location); }; const triggerFileSelect = () => fileInputRef.current?.click(); return (

Provide Tree Details

Upload a clear photo and tell us about your tree for the most accurate AI analysis.

e.preventDefault()} onDrop={(e) => { e.preventDefault(); if (e.dataTransfer.files) { const mockEvent = { target: { files: e.dataTransfer.files } } as unknown as React.ChangeEvent; handleFileChange(mockEvent); } }} >
{imagePreview ? ( Bonsai preview ) : ( <>
setSpecies(e.target.value)} className="block w-full rounded-md border-0 py-2 px-3 text-stone-900 shadow-sm ring-1 ring-inset ring-stone-300 placeholder:text-stone-400 focus:ring-2 focus:ring-inset focus:ring-green-600 sm:text-sm sm:leading-6" placeholder="e.g., Japanese Maple, Ficus" />
setLocation(e.target.value)} className="block w-full rounded-md border-0 py-2 px-3 text-stone-900 shadow-sm ring-1 ring-inset ring-stone-300 placeholder:text-stone-400 focus:ring-2 focus:ring-inset focus:ring-green-600 sm:text-sm sm:leading-6" placeholder="e.g., San Francisco, CA" />
{error &&

{error}

}
); }; export default ImageUploader;