'use client' import { useState, useRef, useCallback } from 'react' import { Upload, Link2, X, AlertCircle } from 'lucide-react' interface BatchInputProps { onUrlsSubmit: (urls: string[]) => void disabled?: boolean } export default function BatchInput({ onUrlsSubmit, disabled }: BatchInputProps) { const [inputValue, setInputValue] = useState('') const [errors, setErrors] = useState([]) const fileInputRef = useRef(null) const parseUrls = useCallback((text: string): string[] => { return text .split(/[\n,;]+/) .map(url => url.trim()) .filter(url => url.length > 0) }, []) const validateUrls = useCallback((urls: string[]): { valid: string[]; invalid: string[] } => { const valid: string[] = [] const invalid: string[] = [] for (const url of urls) { try { let normalizedUrl = url if (!normalizedUrl.startsWith('http://') && !normalizedUrl.startsWith('https://')) { normalizedUrl = `https://${normalizedUrl}` } new URL(normalizedUrl) valid.push(url) } catch { invalid.push(url) } } return { valid, invalid } }, []) const handleSubmit = useCallback(() => { const urls = parseUrls(inputValue) if (urls.length === 0) { setErrors(['Please enter at least one URL']) return } const { valid, invalid } = validateUrls(urls) if (invalid.length > 0) { setErrors(invalid.map(url => `Invalid URL: ${url}`)) } else { setErrors([]) } if (valid.length > 0) { onUrlsSubmit(valid) } }, [inputValue, parseUrls, validateUrls, onUrlsSubmit]) const handleFileUpload = useCallback((event: React.ChangeEvent) => { const file = event.target.files?.[0] if (!file) return const reader = new FileReader() reader.onload = (e) => { const text = e.target?.result as string setInputValue(text) setErrors([]) } reader.onerror = () => { setErrors(['Failed to read file']) } reader.readAsText(file) // Reset file input if (fileInputRef.current) { fileInputRef.current.value = '' } }, []) const handleClear = useCallback(() => { setInputValue('') setErrors([]) }, []) const urlCount = parseUrls(inputValue).length return (
{/* Input Area */}