import React, { useState, useEffect, useRef } from "react"; import { useSocket } from "../../../hooks/useSocket"; import "./styles/ChatTab.css"; import PlayerManager from "../../../services/PlayerManager"; 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 [confirmUnfriend, setConfirmUnfriend] = useState(null); const [onlineList, setOnlineList] = useState( PlayerManager.onlinePlayers || [], ); const messagesEndRef = useRef(null); const activeChatRef = useRef(activeChat); useEffect(() => { activeChatRef.current = activeChat; }, [activeChat]); 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"); } else { socket.emit("chat:get_private_history", { friendId: activeChat }); } }, [socket, activeChat]); useEffect(() => { if (!socket) return; const interval = setInterval(() => { setOnlineList([...PlayerManager.onlinePlayers]); }, 3000); const handleNewMessage = (msg) => { const currentActive = activeChatRef.current; const isGlobalMatch = currentActive === "global" && msg.type === "global"; const isPrivateMatch = currentActive !== "global" && msg.type === "private" && (msg.senderId === currentActive || msg.receiverId === currentActive); if (isGlobalMatch || isPrivateMatch) { setMessages((prev) => { if (prev.some((m) => m.id === msg.id)) return prev; return [...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("chat:private_history", handleHistory); socket.on("player:search_results", handleSearchResults); socket.on("friend:list", handleFriendList); return () => { clearInterval(interval); socket.off("chat:new_message", handleNewMessage); socket.off("chat:global_history", handleHistory); socket.off("chat:private_history", handleHistory); socket.off("player:search_results", handleSearchResults); socket.off("friend:list", handleFriendList); }; }, [socket]); 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 removeFriend = () => { if (confirmUnfriend) { socket.emit("friend:remove", { friendId: confirmUnfriend.id }); if (activeChat === confirmUnfriend.id) setActiveChat("global"); setConfirmUnfriend(null); } }; 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(""); }; 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"; }; const isFriendOnline = (username) => { return onlineList.includes(username); }; return (
Are you sure you want to remove {confirmUnfriend.username} from your contacts?