Spaces:
Sleeping
Sleeping
File size: 10,526 Bytes
049b896 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | 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>
);
};
|