import React, { useEffect, useState, useRef } from 'react'; import { Search, Link, FileText, Youtube, Image as ImageIcon, Upload, Loader2, X } from 'lucide-react'; import api from '../services/api'; import toast from 'react-hot-toast'; export default function InputBox({ onSubmit, isLoading }) { const RESEARCH_LIMIT = 25000; const [input, setInput] = useState(''); const [inputType, setInputType] = useState('text'); const [previewImage, setPreviewImage] = useState(null); const [imageBase64, setImageBase64] = useState(null); const fileInputRef = useRef(null); const textareaRef = useRef(null); const detectType = (value) => { if (value.match(/youtube\.com|youtu\.be/i)) setInputType('youtube'); else if (value.match(/^https?:\/\//i)) setInputType('url'); else setInputType('text'); }; const handleSubmit = async (e) => { e.preventDefault(); if (isLoading) return; if (inputType === 'image') { if (imageBase64) onSubmit({ image_base64: imageBase64, input_type: 'image' }); } else { if (input.length > RESEARCH_LIMIT) { toast.error('Input exceeds research limit (25k chars).'); return; } if (input.trim()) onSubmit({ input_text: input.trim(), input_type: inputType, max_sources: 10 }); } }; const adjustTextareaHeight = () => { if (!textareaRef.current) return; textareaRef.current.style.height = 'auto'; textareaRef.current.style.height = `${Math.min(textareaRef.current.scrollHeight, 160)}px`; }; useEffect(() => { adjustTextareaHeight(); }, [input, inputType]); const handleImageUpload = async (e) => { const file = e.target.files?.[0]; if (!file) return; if (file.size > 10 * 1024 * 1024) { alert('Image must be under 10MB'); return; } setPreviewImage(URL.createObjectURL(file)); const base64 = await api.fileToBase64(file); setImageBase64(base64.split(',')[1]); }; const clearImage = () => { setPreviewImage(null); setImageBase64(null); if (fileInputRef.current) fileInputRef.current.value = ''; }; const typeButtons = [ { id: 'text', icon: FileText, label: 'Text/Claim' }, { id: 'url', icon: Link, label: 'URL' }, { id: 'youtube', icon: Youtube, label: 'YouTube' }, { id: 'image', icon: ImageIcon, label: 'Visual Lens' }, ]; const placeholders = { text: 'Enter a claim, headline, or topic to verify...', url: 'Paste a news URL to verify...', youtube: 'Paste a YouTube video URL...', image: 'Upload an image for deepfake/AI detection...' }; const isNearLimit = input.length >= RESEARCH_LIMIT * 0.8; const isOverLimit = input.length > RESEARCH_LIMIT; const counterColor = isOverLimit ? 'text-red-400' : isNearLimit ? 'text-amber-400' : 'text-slate-500'; return (