import {Avatar, Box, Button, Chip, CircularProgress, IconButton, Stack, Tooltip, Typography} from "@mui/material"; import {AvTimer, Bolt, Dns, Group, PowerOff, PowerSettingsNew, Share} from "@mui/icons-material"; import PurpurImage from "@/common/assets/software/purpur.webp"; import PaperImage from "@/common/assets/software/paper.webp"; import SpigotImage from "@/common/assets/software/spigot.webp"; import {useContext, useEffect, useRef, useState} from "react"; import {isDev, postRequest, PROXY_URL, proxyRequest} from "@/common/utils/RequestUtil.js"; import {ServerContext} from "@/common/contexts/Server/index.js"; import {t} from "i18next"; export const Server = ({uuid, configuration, status}) => { const {updateServer} = useContext(ServerContext); const frameRef = useRef(null); const serverRef = useRef(null); const [serverInfo, setServerInfo] = useState({}); const [frameOpen, setFrameOpen] = useState(false); const [frameLoaded, setFrameLoaded] = useState(false); useEffect(() => { if (!frameOpen) setFrameLoaded(false); }, [frameOpen]); useEffect(() => { if (isDev) return () => { }; const interval = setInterval(() => { if (frameRef.current) { frameRef.current.contentWindow.document.querySelectorAll(".MuiToolbar-regular").forEach((el) => { clearInterval(interval); el.style.display = "none"; }); frameRef.current.contentWindow.document.querySelectorAll(".MuiDivider-root").forEach((el) => { el.style.display = "none"; }); } }, 200); return () => clearInterval(interval); }, [frameOpen]); useEffect(() => { if (serverInfo === null || Object.keys(serverInfo).length === 0) setFrameOpen(false); }, [serverInfo]); const updateServerInfo = () => { proxyRequest(uuid + "/api/stats/") .then((response) => { if (response.status === 200) return response.json(); throw new Error("Server is offline"); }) .then((data) => setServerInfo(data)) .catch(() => setServerInfo(null)); } const startServer = async () => { await postRequest("server/start", {uuid}); updateServer(); } const stopServer = async () => { await postRequest("server/stop", {uuid}); updateServer(); } const onServerClick = () => { if (serverRef.current === null) return; if (serverRef.current.contains(document.activeElement)) return; if (serverInfo !== null && Object.keys(serverInfo).length > 0) { setFrameOpen(!frameOpen); } } useEffect(() => { updateServerInfo(); const interval = setInterval(() => updateServerInfo(), 5000); return () => clearInterval(interval); }, []); return ( 0 ? "pointer" : "default"}} boxShadow={5} gap={2} ref={serverRef} onClick={onServerClick}> {configuration.type} {configuration.name} {configuration.description} {serverInfo === null && ( )} {serverInfo !== null && Object.keys(serverInfo).length === 0 && ( )} {serverInfo !== null && Object.keys(serverInfo).length > 0 && ( {serverInfo.online_players} / {serverInfo.max_players} {serverInfo.tps} )} status === "OFFLINE" ? startServer() : stopServer()}> {frameOpen && !frameLoaded && ( )} {frameOpen && (