import * as React from 'react'; import { ChatWidgetSettings, ChatMessage } from '../types'; import { sendChatMessage } from '../services/geminiService'; import { Icon } from './ui/Icon'; interface ChatWidgetProps { settings: ChatWidgetSettings; } const ChatWidget: React.FC = ({ settings }) => { const [isOpen, setIsOpen] = React.useState(false); const [messages, setMessages] = React.useState([]); const [userInput, setUserInput] = React.useState(''); const [isLoading, setIsLoading] = React.useState(false); const messagesEndRef = React.useRef(null); React.useEffect(() => { if (isOpen && messages.length === 0) { setMessages([{ id: `msg-ai-${Date.now()}`, sender: 'ai', text: 'Hello! How can I help you today?' }]); } }, [isOpen, messages.length]); React.useEffect(() => { if (messagesEndRef.current) { messagesEndRef.current.scrollIntoView({ behavior: 'smooth' }); } }, [messages, isLoading]); const handleSendMessage = React.useCallback(async () => { if (!userInput.trim()) return; const userMessage: ChatMessage = { id: `msg-user-${Date.now()}`, sender: 'user', text: userInput }; setMessages(prev => [...prev, userMessage]); const messageToSend = userInput; setUserInput(''); setIsLoading(true); const aiResponse = await sendChatMessage(messageToSend); setMessages(prev => [...prev, aiResponse]); setIsLoading(false); }, [userInput]); return ( <>

AI Assistant

{messages.map(msg => (
{msg.sender === 'ai' && (
)}

{msg.text}

))} {isLoading && (
)}
setUserInput(e.target.value)} onKeyPress={e => e.key === 'Enter' && !isLoading && handleSendMessage()} placeholder="Ask me anything..." className="flex-1 bg-gray-100 dark:bg-gray-700 text-gray-900 dark:text-white p-2 rounded-md border border-gray-300 dark:border-gray-600 focus:ring-2 focus:ring-brand-primary focus:outline-none" disabled={isLoading} />
); }; export default ChatWidget;