'use client'; import { useState, useRef, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Send, Bot, User, Sparkles, Loader2, ArrowDown, Calendar, Layers, Hash } from 'lucide-react'; import { motion, AnimatePresence } from 'framer-motion'; import ReactMarkdown from 'react-markdown'; interface Message { id: string; content: string; role: 'user' | 'assistant'; timestamp: Date; } interface ChatInterfaceProps { userId?: string; conversationId?: number; compact?: boolean; } export default function ChatInterface({ userId, conversationId, compact = false }: ChatInterfaceProps) { const [inputMessage, setInputMessage] = useState(''); const [messages, setMessages] = useState([]); const [isLoading, setIsLoading] = useState(false); const [showScrollButton, setShowScrollButton] = useState(false); const scrollAreaRef = useRef(null); useEffect(() => { if (messages.length === 0) { setMessages([ { id: 'welcome', content: "Welcome to **TaskFlow Elite**. I'm your high-fidelity coordination agent.\n\nI can now manage your **Projects**, **Tasks**, and **Calendar** with geometric precision. \n\n### Quick Capabilities:\n* **Projects**: \"Create a new project called 'Hackathon'\"\n* **Coordination**: \"Add 'Update UI' to my Hackathon project\"\n* **Calendar**: \"What's on my schedule for this week?\"", role: 'assistant', timestamp: new Date(), } ]); } }, [messages.length]); const scrollToBottom = () => { if (scrollAreaRef.current) { scrollAreaRef.current.scrollTo({ top: scrollAreaRef.current.scrollHeight, behavior: 'smooth' }); } }; useEffect(() => { scrollToBottom(); }, [messages]); const handleScroll = () => { if (scrollAreaRef.current) { const { scrollTop, scrollHeight, clientHeight } = scrollAreaRef.current; setShowScrollButton(scrollHeight - scrollTop - clientHeight > 100); } }; const handleSendMessage = async () => { if (!inputMessage.trim() || isLoading || !userId) return; const userMessage: Message = { id: Date.now().toString(), content: inputMessage, role: 'user', timestamp: new Date(), }; setMessages(prev => [...prev, userMessage]); setInputMessage(''); setIsLoading(true); try { const API_URL = 'https://tahasaif3-ai-taskflow-backend.hf.space'; const storedToken = localStorage.getItem('auth_token'); const response = await fetch(`${API_URL}/api/${userId}/chat/`, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(storedToken ? { 'Authorization': `Bearer ${storedToken}` } : {}), }, credentials: 'include', body: JSON.stringify({ message: inputMessage, conversation_id: conversationId || null }) }); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); const data = await response.json(); setMessages(prev => [...prev, { id: `resp-${Date.now()}`, content: data.response, role: 'assistant', timestamp: new Date(), }]); } catch (error) { setMessages(prev => [...prev, { id: `error-${Date.now()}`, content: "**System Overload.** I couldn't process that request. Please try again or check your connection.", role: 'assistant', timestamp: new Date(), }]); } finally { setIsLoading(false); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSendMessage(); } }; return (
{/* Dynamic Background Accents */}
{/* Top Refinement Line */}
{/* Messages Feed */}
{messages.map((message) => (
{/* Avatar with Glow */}
{message.role === 'user' ? ( ) : ( )} {message.role === 'assistant' && ( )}
{/* Message Content Container */}
{message.content}
{message.role === 'assistant' && } {message.timestamp.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}
))}
{/* Advanced Loading State */} {isLoading && (
{[0, 1, 2].map((i) => ( ))}
Synthesizing...
)}
{/* Floating Scroll Hub */} {showScrollButton && ( )} {/* Futuristic Input Control */}
setInputMessage(e.target.value)} onKeyDown={handleKeyDown} placeholder="Architect your day..." className="h-14 pl-6 pr-14 bg-[#0a0a0a] border-white/10 focus:border-indigo-500/50 focus:ring-4 focus:ring-indigo-500/10 text-white placeholder:text-gray-600 rounded-2xl transition-all duration-300 text-[15px] font-medium" disabled={isLoading} /> {isLoading ? : }
{/* Status Bar */}
Calendar Sync
Projects Node
v2.1 Elite
); }