import React, { useState, useRef, useEffect } from 'react'; import { chatWithExpert } from '../services/geminiService'; import { Message } from '../types'; const ChatBot: React.FC = () => { const [isOpen, setIsOpen] = useState(false); const [input, setInput] = useState(''); const [messages, setMessages] = useState([ { role: 'model', text: "Welcome to Hanmo! I'm your Technical Assistant. How can I help you with our DCS, SCADA, or field instrument solutions today?" } ]); const [isLoading, setIsLoading] = useState(false); const scrollRef = useRef(null); useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight; } }, [messages, isOpen]); const handleSend = async () => { if (!input.trim() || isLoading) return; const userMsg = input.trim(); setInput(''); setMessages(prev => [...prev, { role: 'user', text: userMsg }]); setIsLoading(true); const history = messages.map(m => ({ role: m.role, text: m.text })); const responseText = await chatWithExpert(userMsg, history); setMessages(prev => [...prev, { role: 'model', text: responseText }]); setIsLoading(false); }; return (
{isOpen && (
Tech Advisor
{messages.map((m, i) => (
{m.text}
))} {isLoading && (
)}
setInput(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleSend()} placeholder="Ask a technical question..." className="flex-1 bg-slate-100 text-sm p-3 rounded-xl outline-none focus:ring-2 focus:ring-blue-600/20" />
)}
); }; export default ChatBot;