import React, { useState, useEffect } from "react"; import { useSocket } from "../../../hooks/useSocket"; import "./styles/NotificationsTab.css"; const NotificationsTab = () => { const { socket } = useSocket(); const [notifications, setNotifications] = useState([]); useEffect(() => { if (!socket) return; socket.emit("notifications:get_all"); const handleNewNotify = (notify) => { console.log(notify); setNotifications((prev) => [notify, ...prev]); }; const handleInitialNotify = (data) => { setNotifications(data); }; socket.on("notification:new", handleNewNotify); socket.on("notifications:list", handleInitialNotify); return () => { socket.off("notification:new", handleNewNotify); socket.off("notifications:list", handleInitialNotify); }; }, [socket]); const handleAction = (id, action, data) => { if (action === "accept_friend") { socket.emit("friend:add", { friendId: data.fromId }); } socket.emit("notification:read", { id }); setNotifications((prev) => prev.filter((n) => n.id !== id)); }; const getIcon = (type) => { switch (type) { case "friend_request": return "fas fa-user-plus"; case "crafting": return "fas fa-hammer"; case "system": return "fas fa-robot"; default: return "fas fa-bell"; } }; return (
{n.message}