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) => { 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:accept", { id, friendId: data.fromId }); socket.emit("notification:read", { id }); } else if (action === "dismiss") { socket.emit("notification:dismiss", { id }); } else { 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"; case "item_received": return "fas fa-box-open"; case "inventory_clear": return "fas fa-trash-alt"; default: return "fas fa-bell"; } }; return (
SYSTEM_ALERTS

NOTIFICATIONS

{notifications.length === 0 && (
NO_ACTIVE_ALERTS_FOUND
)} {notifications.map((n) => (

{n.title}

{new Date(n.createdAt).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", })}

{n.message}

{n.type === "friend_request" && ( )}
))}
); }; export default NotificationsTab;