From c59cb1c76c0d413ccc3e9abac4823cffd99da0bc Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Thu, 15 Feb 2024 01:05:40 +0100 Subject: [PATCH] Created the Server.jsx --- .../Overview/components/Server/Server.jsx | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 webui/src/states/Root/pages/Overview/components/Server/Server.jsx diff --git a/webui/src/states/Root/pages/Overview/components/Server/Server.jsx b/webui/src/states/Root/pages/Overview/components/Server/Server.jsx new file mode 100644 index 0000000..aa66a5d --- /dev/null +++ b/webui/src/states/Root/pages/Overview/components/Server/Server.jsx @@ -0,0 +1,165 @@ +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 && ( +