import { useState, useRef, useEffect } from 'react'; import { Box, TextField, IconButton, Paper, Typography } from '@mui/material'; import { Send, ExpandLess, ExpandMore } from '@mui/icons-material'; import { sendChatMessage } from '../services/claudeService'; function ChatInterface() { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [loading, setLoading] = useState(false); const [isExpanded, setIsExpanded] = useState(false); const messagesEndRef = useRef(null); const handleSend = async () => { if (!input.trim() || loading) return; const userMessage = { role: 'user', content: input }; setMessages(prev => [...prev, userMessage]); setInput(''); setLoading(true); setIsExpanded(true); // Expand chat when sending a message try { const response = await sendChatMessage(input, messages); setMessages(prev => [...prev, { role: 'assistant', content: response }]); } catch (error) { console.error('Chat error:', error); setMessages(prev => [...prev, { role: 'assistant', content: 'Sorry, I encountered an error. Please try again.' }]); } finally { setLoading(false); } }; useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); return ( {/* Expand/Collapse Button */} setIsExpanded(!isExpanded)} size="small" > {isExpanded ? : } Chat with Claude {/* Messages area - only visible when expanded */} {isExpanded && ( {messages.length === 0 && ( Ask me anything about your briefing or the news... )} {messages.map((msg, idx) => ( {msg.content} ))}
)} {/* Input area - always visible */} setInput(e.target.value)} onKeyPress={(e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }} placeholder="Ask Claude anything..." disabled={loading} size="small" /> ); } export default ChatInterface;