import React, { useState, useRef, useEffect } from 'react'; import { ChatMessage } from '../types'; import { GeminiService, ChatHistoryItem } from '../geminiService'; const ChatView: React.FC = () => { const [messages, setMessages] = useState([]); const [input, setInput] = useState(''); const [isLoading, setIsLoading] = useState(false); const scrollRef = useRef(null); useEffect(() => { scrollRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); const handleSend = async () => { if (!input.trim() || isLoading) return; const userMsg: ChatMessage = { id: Date.now().toString(), role: 'user', text: input, timestamp: Date.now() }; const newMessages = [...messages, userMsg]; setMessages(newMessages); setInput(''); setIsLoading(true); try { // Map current messages to Gemini's history format const history: ChatHistoryItem[] = messages.map(m => ({ role: m.role === 'user' ? 'user' : 'model', parts: [{ text: m.text }] })); const response = await GeminiService.chat(input, history); const assistantMsg: ChatMessage = { id: (Date.now() + 1).toString(), role: 'assistant', text: response.text, groundingLinks: response.grounding, timestamp: Date.now() }; setMessages(prev => [...prev, assistantMsg]); } catch (err) { console.error(err); setMessages(prev => [...prev, { id: (Date.now() + 1).toString(), role: 'assistant', text: "Bhai, error aa gaya. API key check karo.", timestamp: Date.now() }]); } finally { setIsLoading(false); } }; return (
{messages.length === 0 && (

Neural Chat Memory

I remember our past conversation context.

)} {messages.map((msg) => (

{msg.text}

{msg.groundingLinks && msg.groundingLinks.length > 0 && (

Verified Sources:

{msg.groundingLinks.map((link, idx) => ( {link.title} ))}
)}
))} {isLoading && (
)}
setInput(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleSend()} placeholder="Type your command..." className="flex-1 bg-transparent border-none focus:ring-0 px-4 py-3 text-slate-100 text-sm font-medium placeholder:text-slate-700" />
); }; export default ChatView;