import React, { useState, useEffect, useRef } from 'react'; import ReactMarkdown from 'react-markdown'; import { Message } from '../types'; import { sendMessageToGemini } from '../services/geminiService'; import { VerifiedBotBadge, StaffBadge, CertifiedBadge, BugHunterBadge } from './Badges'; import MiniProfile from './MiniProfile'; interface ChatAreaProps { activeChannelName: string; onOpenFullProfile: (user: any) => void; } const GEMINI_AVATAR = 'https://raw.githubusercontent.com/m4rcel-lol/assets/f08fbf7f9e51418c372697e2df89ddb28c59efe3/rectangle-gemini-google-icon-symbol-logo-free-png.webp'; const USER_AVATAR = 'https://picsum.photos/seed/me/50/50'; const GEMINI_BANNER = 'https://raw.githubusercontent.com/m4rcel-lol/assets/13ebd5bfa7abe5ee591107b9a7b411f3e3ae2d13/Gemini-API-IoT-banner_1.original.png'; const ChatArea: React.FC = ({ activeChannelName, onOpenFullProfile }) => { const [messages, setMessages] = useState([ { id: 'welcome', role: 'model', content: `Hello! I'm your AI assistant. I support **bold**, *italics*, \`code\`, and code blocks: \`\`\`javascript console.log("Hello Discord!"); \`\`\` Ask me anything!`, timestamp: new Date(), senderName: 'Gemini AI', avatarUrl: GEMINI_AVATAR } ]); const [inputValue, setInputValue] = useState(''); const [isLoading, setIsLoading] = useState(false); // Popout State const [popoutUser, setPopoutUser] = useState(null); const [popoutPos, setPopoutPos] = useState({ x: 0, y: 0 }); const messagesEndRef = useRef(null); const scrollToBottom = () => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }; useEffect(() => { scrollToBottom(); }, [messages]); const handleSendMessage = async () => { if (!inputValue.trim() || isLoading) return; const userMsg: Message = { id: Date.now().toString(), role: 'user', content: inputValue, timestamp: new Date(), senderName: 'You', avatarUrl: USER_AVATAR }; setMessages(prev => [...prev, userMsg]); setInputValue(''); setIsLoading(true); try { const responseText = await sendMessageToGemini(inputValue, messages); const botMsg: Message = { id: (Date.now() + 1).toString(), role: 'model', content: responseText, timestamp: new Date(), senderName: 'Gemini AI', avatarUrl: GEMINI_AVATAR }; setMessages(prev => [...prev, botMsg]); } catch (error) { console.error(error); } finally { setIsLoading(false); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSendMessage(); } }; const formatTime = (date: Date) => { return date.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' }); }; const handleUserClick = (e: React.MouseEvent, msg: Message) => { e.stopPropagation(); const rect = e.currentTarget.getBoundingClientRect(); setPopoutPos({ x: rect.right, y: rect.top }); setPopoutUser({ name: msg.senderName, avatarUrl: msg.avatarUrl, isBot: msg.role === 'model', username: msg.role === 'model' ? 'gemini_ai' : 'current_user', color: msg.role === 'model' ? '#5865F2' : '#f0b232', // Banner/Accent Color theme: msg.role === 'model' ? 'linear-gradient(to bottom right, #4c1d95, #1e40af)' // Darker Purple-Blue Gradient for better text visibility : '#232428', // Default Dark Gray for User bannerUrl: msg.role === 'model' ? GEMINI_BANNER : undefined }); }; return (
{/* Header */}
@

{activeChannelName}

{/* Messages */}
{/* Welcome Placeholder */}
Gemini

{activeChannelName}

Verified System Bot This is a temporary chat conversation with {activeChannelName} Your messages will be lost after refreshing the site.
{messages.map((msg, index) => { const isUser = msg.role === 'user'; return (
{/* Avatar */}
handleUserClick(e, msg)} > {msg.senderName}
{/* Content */}
handleUserClick(e, msg)} > {msg.senderName} {!isUser && (
)} {formatTime(msg.timestamp)}
{msg.content}
); })} {isLoading && (
)}
{/* Input Area */}
setInputValue(e.target.value)} onKeyDown={handleKeyDown} disabled={isLoading} autoFocus />
{isLoading && (
Gemini AI is typing...
)}
{popoutUser && ( setPopoutUser(null)} onOpenFullProfile={onOpenFullProfile} /> )}
); }; export default ChatArea;