import React, { useState, useRef, useEffect } from 'react'; import { Message } from '@/types'; import { streamChatResponse } from '@/services/geminiService'; const ChatInterface: React.FC = () => { const [input, setInput] = useState(''); const [messages, setMessages] = useState([ { id: 'welcome', role: 'model', text: "Sistem aktif. ZeroSixLab AI arayüzüne hoş geldiniz. Araştırmanız veya projeniz için nasıl yardımcı olabilirim?", timestamp: new Date(), } ]); const [isLoading, setIsLoading] = useState(false); const [isInitialLoad, setIsInitialLoad] = useState(true); const messagesEndRef = useRef(null); const messagesContainerRef = useRef(null); const inputRef = useRef(null); const scrollToBottom = () => { // Only scroll inside the messages container, not the whole page if (messagesContainerRef.current) { messagesContainerRef.current.scrollTop = messagesContainerRef.current.scrollHeight; } }; useEffect(() => { // Skip auto-scroll on initial load if (isInitialLoad) { setIsInitialLoad(false); return; } scrollToBottom(); }, [messages, isInitialLoad]); // Focus input on load (without scrolling page) useEffect(() => { inputRef.current?.focus({ preventScroll: true }); }, []); const handleSend = async () => { if (!input.trim() || isLoading) return; const userMsg: Message = { id: Date.now().toString(), role: 'user', text: input, timestamp: new Date(), }; setMessages(prev => [...prev, userMsg]); setInput(''); setIsLoading(true); // Prepare history for API const history = messages.map(m => ({ role: m.role, parts: [{ text: m.text }] })); try { const stream = await streamChatResponse(userMsg.text, history); const modelMsgId = (Date.now() + 1).toString(); const modelMsg: Message = { id: modelMsgId, role: 'model', text: '', timestamp: new Date(), }; setMessages(prev => [...prev, modelMsg]); let fullText = ''; for await (const chunk of stream) { fullText += chunk; setMessages(prev => prev.map(msg => msg.id === modelMsgId ? { ...msg, text: fullText } : msg) ); } } catch (error) { setMessages(prev => [...prev, { id: Date.now().toString(), role: 'model', text: "Hata: Nöral bağlantı kararsız. Lütfen API anahtarını kontrol edin.", timestamp: new Date(), isError: true }]); } finally { setIsLoading(false); } }; return (
{/* Decorative Corner Lines */}
{/* HUD Header */}
CANLI_BAĞLANTI // GEMINI-2.5-FLASH
MEM: 32GB NET: SECURE LATENCY: 12ms
{/* Background Effect inside Chat */}
{/* Messages Area */}
{messages.map((msg) => (
{msg.role === 'user' ? '>> OPERATOR' : '>> ZERO_AI'} {msg.timestamp.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})}
{msg.text} {msg.role === 'model' && msg.id === messages[messages.length - 1].id && isLoading && ( )}
{/* Decorative side accent for model */} {msg.role === 'model' && (
)}
))} {/* Loading Indicator */} {isLoading && messages[messages.length - 1].role === 'user' && (
PROCESSING...
)}
{/* Input Area */}
{'>'}
setInput(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleSend()} placeholder="Komut girişi yapın..." className="flex-1 bg-zsl-bg/50 border border-slate-700 focus:border-zsl-primary text-white pl-8 pr-5 py-4 rounded-lg font-mono text-sm outline-none transition-all placeholder-slate-600 focus:shadow-[0_0_15px_rgba(0,212,255,0.1)] focus:bg-zsl-bg/80" />
); }; export default ChatInterface;