'use client'; // Phase III - AI-Powered Todo Chatbot // Advanced ChatInterface - Modern, professional UI with animations import React, { useState, useRef, useEffect } from 'react'; import { Send, Bot, User, Loader2, Sparkles, Trash2, Copy, Check } from 'lucide-react'; import RobotAvatar from './RobotAvatar'; interface Message { role: 'user' | 'assistant'; content: string; timestamp: Date; tool_calls?: any[]; } interface ChatResponse { reply: string; conversation_id: string; tool_calls?: any[]; } interface ChatInterfaceProps { jwtToken: string; apiBaseUrl?: string; } const ChatInterface: React.FC = ({ jwtToken, apiBaseUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000' }) => { const [messages, setMessages] = useState([]); const [inputMessage, setInputMessage] = useState(''); const [isLoading, setIsLoading] = useState(false); const [conversationId, setConversationId] = useState(null); const [error, setError] = useState(null); const [copiedIndex, setCopiedIndex] = useState(null); const messagesEndRef = useRef(null); const inputRef = useRef(null); const containerRef = useRef(null); // Auto-scroll to bottom when new messages arrive useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); // Focus input on mount useEffect(() => { inputRef.current?.focus(); }, []); const detectLanguage = (text: string): 'en' | 'ur' => { const urduPattern = /[\u0600-\u06FF]/; return urduPattern.test(text) ? 'ur' : 'en'; }; const handleSendMessage = async () => { if (!inputMessage.trim() || isLoading) { return; } const userMessage: Message = { role: 'user', content: inputMessage.trim(), timestamp: new Date(), }; setMessages((prev) => [...prev, userMessage]); setInputMessage(''); setIsLoading(true); setError(null); inputRef.current?.focus(); try { const response = await fetch(`${apiBaseUrl}/api/chat/`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}`, }, body: JSON.stringify({ message: userMessage.content, conversation_id: conversationId }), }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.detail || 'Failed to send message'); } const data: ChatResponse = await response.json(); const assistantMessage: Message = { role: 'assistant', content: data.reply, timestamp: new Date(), tool_calls: data.tool_calls }; setMessages((prev) => [...prev, assistantMessage]); setConversationId(data.conversation_id); if (data.tool_calls && data.tool_calls.length > 0) { console.log('Tool calls executed:', data.tool_calls); } } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Failed to send message'; setError(errorMessage); const errorMessageObj: Message = { role: 'assistant', content: `❌ Error: ${errorMessage}`, timestamp: new Date(), }; setMessages((prev) => [...prev, errorMessageObj]); } finally { setIsLoading(false); } }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSendMessage(); } }; const handleClearChat = () => { setMessages([]); setConversationId(null); setError(null); inputRef.current?.focus(); }; const handleCopyMessage = (content: string, index: number) => { navigator.clipboard.writeText(content); setCopiedIndex(index); setTimeout(() => setCopiedIndex(null), 2000); }; const suggestions = [ { text: 'Add a task to buy milk', lang: 'en' }, { text: 'میرے ٹاسک دکھاؤ', lang: 'ur' }, { text: 'Show my pending tasks', lang: 'en' }, { text: 'ہائی پرئورٹی ٹاسک شامل کرو', lang: 'ur' }, ]; const currentLang = detectLanguage(inputMessage); return (
{/* Header */}

AI Assistant

{currentLang === 'ur' ? 'اردو میں بات کریں' : 'Chat in English or Urdu'}

{/* Messages Area */}
{messages.length === 0 ? (

{currentLang === 'ur' ? 'آپ کا AI مددگار' : 'Your AI Assistant'}

{currentLang === 'ur' ? 'قدمی بات سے ٹاسک منیج کریں' : 'Manage tasks with natural conversation'}

{currentLang === 'ur' ? 'مثالیں:' : 'Try these:'}

{suggestions.map((suggestion, idx) => ( ))}
) : ( messages.map((message, index) => (
{/* Avatar */}
{message.role === 'user' ? (
) : (
)}
{/* Message */}

{message.content}

{/* Timestamp & Actions */}
{message.timestamp.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} {message.role === 'assistant' && ( )}
{/* Tool Calls Indicator */} {message.tool_calls && message.tool_calls.length > 0 && (
{message.tool_calls.length} tool(s) executed
)}
)) )} {/* Loading Indicator */} {isLoading && (
{currentLang === 'ur' ? 'سوچ رہا ہے...' : 'Thinking...'}
)}
{/* Error Display */} {error && (

{error}

)} {/* Input Area */}
setInputMessage(e.target.value)} onKeyPress={handleKeyPress} placeholder={ currentLang === 'ur' ? 'اپنا میسج ٹائپ کریں...' : 'Type your message...' } disabled={isLoading} maxLength={1000} className="w-full px-5 py-4 bg-slate-100 dark:bg-slate-900 text-gray-900 dark:text-white placeholder-gray-500 dark:placeholder-gray-400 rounded-2xl border-2 border-transparent focus:border-blue-500 focus:ring-2 focus:ring-blue-500/20 outline-none transition-all pr-12 text-[15px]" />
{inputMessage.length}/1000

{currentLang === 'ur' ? 'قانونی طور پر: انگریزی یا اردو میں بات کریں' : 'Press Enter to send • Shift+Enter for new line • Supports English & Urdu'}

{conversationId ? `Session: ${conversationId.slice(0, 8)}...` : 'New session'}

); }; export default ChatInterface;