import { useState, useRef, useEffect } from 'react' import { motion, AnimatePresence } from 'framer-motion' import { FaRobot, FaUser, FaPaperPlane, FaMicrophone, FaStop, FaCopy, FaThumbsUp, FaThumbsDown, FaRedo, FaLanguage, FaBrain, FaGlobe, FaCheckCircle, FaExclamationTriangle } from 'react-icons/fa' import ReactMarkdown from 'react-markdown' import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter' import { vscDarkPlus } from 'react-syntax-highlighter/dist/cjs/styles/prism' import toast from 'react-hot-toast' export default function ChatInterface({ language, theme }) { const [messages, setMessages] = useState([ { id: 1, type: 'assistant', content: language === 'fa' ? 'سلام! من GhadirSync-AI هستم، دستیار هوشمند شما. چطور میتوانم به شما کمک کنم؟' : 'Hello! I am GhadirSync-AI, your intelligent assistant. How can I help you?', timestamp: new Date(), sources: [], confidence: 0.95 } ]) const [input, setInput] = useState('') const [isTyping, setIsTyping] = useState(false) const [isListening, setIsListening] = useState(false) const [selectedModel, setSelectedModel] = useState('local') const [useInternet, setUseInternet] = useState(false) const messagesEndRef = useRef(null) const inputRef = useRef(null) const models = [ { id: 'local', name: language === 'fa' ? 'مدل محلی' : 'Local Model', icon: FaBrain, color: 'text-green-400' }, { id: 'gpt4', name: 'GPT-4', icon: FaGlobe, color: 'text-blue-400' }, { id: 'claude', name: 'Claude', icon: FaBrain, color: 'text-purple-400' }, ] const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }) } useEffect(() => { scrollToBottom() }, [messages]) const handleSend = async () => { if (!input.trim()) return const userMessage = { id: Date.now(), type: 'user', content: input, timestamp: new Date(), } setMessages(prev => [...prev, userMessage]) setInput('') setIsTyping(true) // Simulate AI response setTimeout(() => { const aiResponse = { id: Date.now() + 1, type: 'assistant', content: generateAIResponse(input), timestamp: new Date(), sources: useInternet ? [ { title: 'Wikipedia', url: 'https://wikipedia.org' }, { title: 'Stack Overflow', url: 'https://stackoverflow.com' } ] : [], confidence: Math.random() * 0.3 + 0.7, model: selectedModel } setMessages(prev => [...prev, aiResponse]) setIsTyping(false) }, 1500) } const generateAIResponse = (userInput) => { const responses = { fa: [ `برای سوال "${userInput}"، من تحلیل کاملی ارائه میدهم:\n\n## تحلیل اصلی\n\n1. **نکته اول**: این یک موضوع مهم است\n2. **نکته دوم**: نیاز به بررسی بیشتر دارد\n3. **نکته سوم**: پیشنهادات عملی\n\n\`\`\`javascript\n// نمونه کد\nfunction example() {\n return "Hello World";\n}\n\`\`\`\n\n**نتیجهگیری**: بر اساس تحلیل، بهترین راهکار...`, `در پاسخ به "${userInput}":\n\n### اطلاعات کلیدی\n\n- 🎯 هدف اصلی\n- 📊 تحلیل دادهها\n- 💡 راهکارهای نوآورانه\n\n## پیشنهادات\n\n1. راهکار اول\n2. راهکار دوم\n3. راهکار سوم\n\n**توجه**: این اطلاعات بر اساس بهترین منابع موجود است.`, ], en: [ `For your question "${userInput}", here's my comprehensive analysis:\n\n## Main Analysis\n\n1. **First Point**: This is an important topic\n2. **Second Point**: Requires further investigation\n3. **Third Point**: Practical suggestions\n\n\`\`\`python\n# Sample code\ndef example():\n return "Hello World"\n\`\`\`\n\n**Conclusion**: Based on the analysis, the best solution is...`, `In response to "${userInput}":\n\n### Key Information\n\n- 🎯 Main objective\n- 📊 Data analysis\n- 💡 Innovative solutions\n\n## Recommendations\n\n1. First solution\n2. Second solution\n3. Third solution\n\n**Note**: This information is based on the best available sources.`, ] } const langResponses = responses[language] || responses.en return langResponses[Math.floor(Math.random() * langResponses.length)] } const handleVoiceInput = () => { if (isListening) { setIsListening(false) toast.success(language === 'fa' ? 'ضبط صدا متوقف شد' : 'Voice recording stopped') } else { setIsListening(true) toast.success(language === 'fa' ? 'در حال ضبط صدا...' : 'Recording voice...') // Simulate voice recognition setTimeout(() => { setInput(language === 'fa' ? 'این یک متن نمونه از تشخیص صدا است' : 'This is a sample text from voice recognition') setIsListening(false) }, 3000) } } const handleCopyMessage = (content) => { navigator.clipboard.writeText(content) toast.success(language === 'fa' ? 'کپی شد!' : 'Copied!') } const handleRegenerateResponse = (messageId) => { setIsTyping(true) setTimeout(() => { setMessages(prev => prev.map(msg => msg.id === messageId ? { ...msg, content: generateAIResponse(' regenerated response'), timestamp: new Date() } : msg )) setIsTyping(false) }, 1500) } return (
{children}
)
}
>
{message.content}