import React, { useState, useEffect, useRef } from 'react'; import { useChatbot } from '../../contexts/ChatbotContext'; import './ChatbotInterface.css'; const ChatbotInterface = ({ isVisible, onClose }) => { const { messages, isLoading, error, sendMessage } = useChatbot(); const [inputValue, setInputValue] = useState(''); const messagesEndRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }; useEffect(() => { scrollToBottom(); }, [messages]); const handleSend = () => { if (!inputValue.trim() || isLoading) return; sendMessage(inputValue); setInputValue(''); }; const handleKeyPress = (e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSend(); } }; if (!isVisible) return null; return (

AI Assistant

{messages.map((message) => (
{message.text}
))} {isLoading && (
)} {error && (
{error}
)}
setInputValue(e.target.value)} onKeyPress={handleKeyPress} placeholder="Ask a question about the book..." disabled={isLoading} />
); }; export default ChatbotInterface;