import React, { useState, useEffect, useRef } from "react"; import { useSocket } from "../../../hooks/useSocket"; import "./styles/ChatTab.css"; const ChatTab = () => { const { socket } = useSocket(); const [activeChat, setActiveChat] = useState("global"); const [searchQuery, setSearchQuery] = useState(""); const [searchResults, setSearchResults] = useState([]); const [messages, setMessages] = useState([]); const [friends, setFriends] = useState([]); const [inputValue, setInputValue] = useState(""); const [showSidebar, setShowSidebar] = useState(true); const [isMobile, setIsMobile] = useState(window.innerWidth <= 768); const messagesEndRef = useRef(null); useEffect(() => { const handleResize = () => { const mobile = window.innerWidth <= 768; setIsMobile(mobile); if (!mobile) setShowSidebar(true); }; window.addEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize); }, []); useEffect(() => { if (!socket) return; socket.emit("friend:get_list"); if (activeChat === "global") { socket.emit("chat:get_global_history"); } const handleNewMessage = (msg) => { if ( (activeChat === "global" && msg.type === "global") || (activeChat !== "global" && (msg.senderId === activeChat || msg.receiverId === activeChat)) ) { setMessages((prev) => [...prev, msg]); } }; const handleHistory = (history) => setMessages(history); const handleSearchResults = (results) => setSearchResults(results); const handleFriendList = (list) => setFriends(list); socket.on("chat:new_message", handleNewMessage); socket.on("chat:global_history", handleHistory); socket.on("player:search_results", handleSearchResults); socket.on("friend:list", handleFriendList); return () => { socket.off("chat:new_message", handleNewMessage); socket.off("chat:global_history", handleHistory); socket.off("player:search_results", handleSearchResults); socket.off("friend:list", handleFriendList); }; }, [socket, activeChat]); useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages]); const handleSearch = (e) => { const query = e.target.value; setSearchQuery(query); if (query.length > 1) { socket.emit("player:search", { query }); } else { setSearchResults([]); } }; const addFriend = (player) => { socket.emit("friend:add", { friendId: player.id }); setSearchQuery(""); setSearchResults([]); }; const sendMessage = () => { if (!inputValue.trim() || !socket) return; socket.emit("chat:send_message", { content: inputValue, type: activeChat === "global" ? "global" : "private", receiverId: activeChat === "global" ? null : activeChat, }); setInputValue(""); console.log(activeChat === "global" ? null : activeChat); }; const selectChat = (id) => { setActiveChat(id); if (isMobile) setShowSidebar(false); }; const getChatName = () => { if (activeChat === "global") return "GLOBAL_SYSTEM_CHAT"; const friend = friends.find((f) => f.id === activeChat); return friend ? friend.username : "UNKNOWN_PILOT"; }; return (
{isMobile && ( )}

{getChatName()}

{messages.length === 0 && (
NO_LOGS_FOUND. SECURE_LINE_READY...
)} {messages.map((msg, index) => (
[ {new Date(msg.createdAt).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", })} ] {msg.senderName}: {msg.content}
))}
setInputValue(e.target.value)} onKeyDown={(e) => e.key === "Enter" && sendMessage()} />
); }; export default ChatTab;