/* eslint-disable @typescript-eslint/no-explicit-any */ import { Box, Button, LoadingOverlay, Text, Title, Tooltip, } from "@mantine/core"; import { useEffect, useRef, useState } from "react"; import io from "socket.io-client"; import { WorkingPage } from "../components/dashboard"; import { IBid, IWebBid } from "../system/type"; import { checkStatus } from "../apis/auth"; import { IconPower, IconRestore } from "@tabler/icons-react"; import { useConfirmStore } from "../lib/zustand/use-confirm"; import { getStatusTool, resetTool, shutdownTool } from "../apis/dashboard"; import { cn } from "../utils"; import { useStatusToolStore } from "../lib/zustand/use-status-tool-store"; const socket = io(`${import.meta.env.VITE_SOCKET_URL}/admin-bid-ws`, { autoConnect: true, transports: ["websocket"], }); export default function DashBoard() { const [workingData, setWorkingData] = useState< (IWebBid & { type: string })[] | (IBid & { type: string })[] >([]); const { setConfirm } = useConfirmStore(); const [loading, setLoading] = useState(false); const { setStatusTool, statusTool } = useStatusToolStore(); const RETRY_CONNECT = useRef(2); useEffect(() => { setLoading(true); socket.connect(); socket.on("connect", () => { socket.emit("getBidsData"); }); socket.on("disconnect", async () => { if (RETRY_CONNECT.current > 0) { await checkStatus(); socket.connect(); RETRY_CONNECT.current--; return; } }); socket.on("adminBidsUpdated", (data: IWebBid[]) => { const array = data.reduce((prev, cur) => { if (cur.children?.length > 0) { prev = [...prev, ...cur.children]; } prev.push(cur); return prev; }, [] as any[]); const newData = array.map((item) => { if (item.children) { return { ...item, type: "API_BID", }; } return { ...item, type: "PRODUCT_TAB", }; }); setWorkingData(newData); }); setLoading(false); return () => { console.log("🔌 Cleanup WebSocket listeners..."); socket.off("adminBidsUpdated"); socket.off("working"); socket.off("connect"); socket.off("disconnect"); socket.disconnect(); }; }, []); useEffect(() => { const statusTool = async () => { const result = await getStatusTool(); if (result?.data) { setStatusTool(result?.data); } else { setStatusTool(false); } }; const intervalId = setInterval(statusTool, 5000); return () => { clearInterval(intervalId); }; }, []); const handleResetTool = () => { setConfirm({ handleOk: async () => { setLoading(true); await resetTool(); setLoading(false); }, title: "Confirm tool reset", message: "Are you sure you want to reset this tool? All current processes will be stopped and restarted.", okButton: { value: "Ok", color: "blue" }, }); }; const handleShutdownTool = () => { setConfirm({ handleOk: async () => { setLoading(true); await shutdownTool(); setLoading(false); }, title: "Confirm tool shutdown", message: "Are you sure you want to shut down this tool? All running processes will be stopped and the tool will go offline.", okButton: { value: "Ok", color: "blue" }, }); }; return ( Admin Dashboard {workingData.length > 0 && workingData.map((item, index) => ( ))} {workingData.length <= 0 && ( No Pages )} ); }