NOVAESPORT / src /components /ChatSystem.tsx
Musabek's picture
Deploy all codebase from Google AI Studio
049b896
Raw History Blame Contribute Delete
10.5 kB
import React, { useState, useEffect, useRef } from 'react';
import { ChatMessage, User } from '../types';
import { Send, MessageSquare, Users, ShieldAlert, X, Minimize2, Maximize2 } from 'lucide-react';
interface ChatSystemProps {
currentUser: User;
onSendMessage: (text: string, channel: 'global' | 'team' | 'match') => void;
messages: ChatMessage[];
}
const CHAT_BOT_NAMES = ['ViperStrike', 'NeonNinja', 'SniperPro77', 'ShadowDemon', 'T1_Fanboy', 'ToxicReaper', 'MLBB_Queen'];
const CHAT_BOT_ALIVE_TEXTS = [
'Anyone watching the Grand Finals live currently?',
'That triple kill in game 2 was actually insane...',
'Who wants to join my solo lobby for some practice scrims?',
'Sentinels are playing crazy good tactics today.',
'Can we talk about the latest update balance patches?',
'MLBB draft selection was so meta.',
'Registering my team right now. Let us go cybergang!',
'Dota 2 bracket looks stacked this phase.',
'Let us showcase our badges on the pro league ranks!'
];
export const ChatSystem: React.FC<ChatSystemProps> = ({ currentUser, onSendMessage, messages }) => {
const [isOpen, setIsOpen] = useState(false);
const [channel, setChannel] = useState<'global' | 'team' | 'match'>('global');
const [inputText, setInputText] = useState('');
const [unreadCount, setUnreadCount] = useState(0);
const scrollRef = useRef<HTMLDivElement>(null);
// Auto-scroll chat to bottom
useEffect(() => {
if (scrollRef.current && isOpen) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
}, [messages, isOpen]);
// Keep track of unread messages when closed
useEffect(() => {
if (!isOpen) {
setUnreadCount((prev) => prev + 1);
}
}, [messages]);
// Reset unread counts when opened
useEffect(() => {
if (isOpen) {
setUnreadCount(0);
}
}, [isOpen]);
const handleSend = (e: React.FormEvent) => {
e.preventDefault();
if (!inputText.trim()) return;
onSendMessage(inputText, channel);
setInputText('');
};
const filteredMessages = messages.filter((m) => m.channel === channel);
return (
<div className="fixed bottom-6 right-6 z-50 font-sans">
{/* Floating Chat Button */}
{!isOpen && (
<button
onClick={() => setIsOpen(true)}
className="relative bg-gradient-to-r from-cyan-500 to-purple-600 hover:from-cyan-400 hover:to-purple-500 text-white p-4 rounded-full shadow-lg shadow-cyan-500/20 flex items-center justify-center transition-all duration-300 hover:scale-110 border border-cyan-400/30 group cursor-pointer"
id="chat-toggle-btn"
>
<MessageSquare className="w-6 h-6 animate-pulse" />
{unreadCount > 0 && (
<span className="absolute -top-1 -right-1 bg-red-500 text-white text-[10px] font-bold w-5 h-5 rounded-full flex items-center justify-center border-2 border-slate-900 animate-bounce">
{unreadCount > 9 ? '9+' : unreadCount}
</span>
)}
<span className="absolute right-16 bg-slate-900/90 text-cyan-400 text-xs px-2.5 py-1 rounded-md border border-cyan-500/30 whitespace-nowrap opacity-0 group-hover:opacity-100 transition-opacity duration-300 pointer-events-none">
Battle Chat
</span>
</button>
)}
{/* Chat Window */}
{isOpen && (
<div
className="w-80 md:w-96 h-[450px] bg-[#020205]/75 backdrop-blur-lg rounded-2xl border border-white/10 shadow-2xl shadow-cyan-950/20 flex flex-col overflow-hidden animate-in fade-in slide-in-from-bottom-6 duration-300"
id="chat-window-panel"
>
{/* Header */}
<div className="bg-white/5 p-4 border-b border-white/10 flex items-center justify-between">
<div className="flex items-center gap-2">
<span className="w-2 h-2 rounded-full bg-cyan-400 animate-pulse" />
<h3 className="font-semibold text-white tracking-wide text-sm flex items-center gap-2 font-mono uppercase text-xs">
Nova Esports Chat Stream
</h3>
</div>
<div className="flex items-center gap-2">
<button
onClick={() => setIsOpen(false)}
className="text-slate-400 hover:text-white p-1 rounded transition-colors cursor-pointer"
>
<X className="w-4 h-4" />
</button>
</div>
</div>
{/* Channel Tabs */}
<div className="grid grid-cols-3 bg-white/5 border-b border-white/10 text-[11px] font-mono text-slate-450 text-slate-400">
<button
onClick={() => setChannel('global')}
className={`py-2.5 px-1 font-medium transition-all duration-250 border-b-2 flex items-center justify-center gap-1.5 cursor-pointer uppercase ${
channel === 'global'
? 'border-cyan-400 text-cyan-300 bg-cyan-500/5 font-bold'
: 'border-transparent hover:text-slate-200'
}`}
>
<Users className="w-3.5 h-3.5 text-cyan-400" />
Global
</button>
<button
onClick={() => setChannel('team')}
className={`py-2.5 px-1 font-medium transition-all duration-250 border-b-2 flex items-center justify-center gap-1.5 cursor-pointer uppercase ${
channel === 'team'
? 'border-purple-400 text-purple-300 bg-purple-500/5 font-bold'
: 'border-transparent hover:text-slate-200'
}`}
>
<Users className="w-3.5 h-3.5 text-purple-400" />
Team
</button>
<button
onClick={() => setChannel('match')}
className={`py-2.5 px-1 font-medium transition-all duration-250 border-b-2 flex items-center justify-center gap-1.5 cursor-pointer uppercase ${
channel === 'match'
? 'border-pink-450 text-pink-400 bg-pink-500/5 font-bold'
: 'border-transparent hover:text-slate-200'
}`}
>
<ShieldAlert className="w-3.5 h-3.5 text-pink-400" />
Match
</button>
</div>
{/* Message List */}
<div ref={scrollRef} className="flex-1 overflow-y-auto p-4 space-y-3.5 custom-scrollbar bg-white/5">
{filteredMessages.length === 0 ? (
<div className="h-full flex flex-col items-center justify-center text-slate-500 text-center p-4">
<MessageSquare className="w-8 h-8 stroke-1 text-slate-500 mb-2" />
<p className="text-xs">No chatter in this chamber yet.</p>
<p className="text-[10px] text-slate-650 mt-1">Be the first to spark the fire!</p>
</div>
) : (
filteredMessages.map((msg) => {
const isAdmin = msg.role === 'admin';
const isMe = msg.userId === currentUser.id;
return (
<div key={msg.id} className={`flex items-start gap-2.5 ${isMe ? 'flex-row-reverse' : ''}`}>
{/* Avatar */}
<div className="w-7 h-7 rounded-full bg-[#020205] border border-white/10 flex items-center justify-center text-xs shrink-0 shadow-inner overflow-hidden">
{msg.userAvatar && (msg.userAvatar.startsWith('http://') || msg.userAvatar.startsWith('https://')) ? (
<img src={msg.userAvatar} className="w-full h-full object-cover rounded-full" alt="" referrerPolicy="no-referrer" />
) : (
msg.userAvatar || '👤'
)}
</div>
{/* Content Block */}
<div className={`max-w-[75%] space-y-0.5`}>
<div className={`flex items-baseline gap-1.5 ${isMe ? 'flex-row-reverse' : ''}`}>
<span
className={`text-[11px] font-bold tracking-wide ${
isAdmin
? 'text-rose-400'
: isMe
? 'text-purple-300'
: 'text-cyan-300'
}`}
>
{msg.username}
</span>
{isAdmin && (
<span className="text-[8px] px-1 bg-rose-500/10 border border-rose-500/20 text-rose-400 font-bold rounded uppercase">
Admin
</span>
)}
<span className="text-[9px] text-slate-500 font-mono">{msg.timestamp}</span>
</div>
<div
className={`text-xs px-3 py-2 rounded-xl border leading-relaxed ${
isMe
? 'bg-purple-500/15 border-purple-500/20 text-indigo-100 rounded-tr-none'
: 'bg-white/5 border-white/10 text-slate-200 rounded-tl-none'
}`}
>
{msg.message}
</div>
</div>
</div>
);
})
)}
</div>
{/* Message Input */}
<form onSubmit={handleSend} className="p-3 border-t border-white/10 bg-white/5 flex gap-2">
<input
type="text"
value={inputText}
onChange={(e) => setInputText(e.target.value)}
placeholder={`Send message to #${channel}...`}
maxLength={150}
className="flex-1 bg-[#020205]/60 border border-white/10 hover:border-cyan-500/30 focus:border-cyan-500 text-white rounded-lg px-3 py-1.5 text-xs outline-none focus:ring-1 focus:ring-cyan-500/20 transition-all font-sans"
/>
<button
type="submit"
className="bg-gradient-to-r from-cyan-500 to-cyan-600 hover:from-cyan-400 hover:to-cyan-500 text-white p-2 rounded-lg transition-all duration-150 hover:scale-105 active:scale-95 shrink-0 flex items-center justify-center cursor-pointer border border-cyan-400/35 shadow shadow-cyan-500/10"
>
<Send className="w-3.5 h-3.5" />
</button>
</form>
</div>
)}
</div>
);
};