2024-03-29 16:58:17 -06:00
|
|
|
// @ts-nocheck
|
2023-03-15 15:20:08 +00:00
|
|
|
import { useState, useEffect, useRef } from "react";
|
|
|
|
import Box from "@mui/material/Box";
|
|
|
|
import Button from "@mui/material/Button";
|
|
|
|
import TextField from "@mui/material/TextField";
|
2024-02-11 03:57:01 +00:00
|
|
|
import Skeleton from "@mui/material/Skeleton";
|
|
|
|
import Typography from "@mui/material/Typography";
|
2024-04-06 20:38:10 -06:00
|
|
|
import RconSocket from "./RconSocket.ts";
|
2023-03-15 15:20:08 +00:00
|
|
|
import "@mcl/css/rcon.css";
|
|
|
|
|
2024-02-11 03:57:01 +00:00
|
|
|
function RconLogSkeleton() {
|
|
|
|
return (
|
|
|
|
<Skeleton
|
|
|
|
variant="text"
|
|
|
|
width="100%"
|
|
|
|
sx={{ backgroundColor: "rgba(255,255,255,.25)" }}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-15 15:20:08 +00:00
|
|
|
export default function RconView(props) {
|
2024-01-15 20:30:31 +00:00
|
|
|
const { serverId } = props;
|
2023-03-15 15:20:08 +00:00
|
|
|
const logsRef = useRef(0);
|
|
|
|
const [cmd, setCmd] = useState("");
|
|
|
|
const [logs, setLogs] = useState([]);
|
2024-01-15 20:30:31 +00:00
|
|
|
const [rcon, setRcon] = useState();
|
2023-03-15 15:20:08 +00:00
|
|
|
const updateCmd = (e) => setCmd(e.target.value);
|
|
|
|
|
2024-01-15 20:30:31 +00:00
|
|
|
const disconnectRcon = () => {
|
|
|
|
if (!rcon || typeof rcon.disconnect !== "function") return;
|
|
|
|
rcon.disconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
function () {
|
|
|
|
if (!serverId) return;
|
|
|
|
const rs = new RconSocket(setLogs, serverId);
|
|
|
|
setRcon(rs);
|
|
|
|
return disconnectRcon;
|
|
|
|
},
|
|
|
|
[serverId],
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() => logsRef.current.scrollTo(0, logsRef.current.scrollHeight),
|
|
|
|
[(rcon ?? {}).logs],
|
|
|
|
);
|
2023-03-15 15:20:08 +00:00
|
|
|
|
|
|
|
function sendCommand() {
|
|
|
|
rcon.send(cmd);
|
|
|
|
setCmd("");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-02-11 03:57:01 +00:00
|
|
|
<Box sx={{ height: "100%", display: "flex", flexWrap: "wrap" }}>
|
|
|
|
<Box
|
|
|
|
className="rconLogsWrapper"
|
|
|
|
ref={logsRef}
|
|
|
|
style={{
|
|
|
|
padding: "1rem",
|
|
|
|
backgroundColor: "rgba(0,0,0,.815)",
|
|
|
|
color: "white",
|
|
|
|
borderRadius: "4px",
|
|
|
|
width: "100%",
|
2024-03-12 01:58:25 +00:00
|
|
|
height: "100%",
|
2024-02-11 03:57:01 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{logs.length === 0 &&
|
|
|
|
[...Array(20).keys()].map((_v, i) => <RconLogSkeleton key={i} />)}
|
|
|
|
{logs.length > 0 &&
|
|
|
|
logs.map((v, k) => (
|
|
|
|
<Box key={k}>
|
|
|
|
<Typography variant="subtitle2">{v}</Typography>
|
|
|
|
</Box>
|
|
|
|
))}
|
|
|
|
</Box>
|
|
|
|
<Box
|
|
|
|
className="rconActions"
|
|
|
|
sx={{ marginTop: "auto", paddingTop: "1rem", width: "100%" }}
|
|
|
|
>
|
2023-03-15 15:20:08 +00:00
|
|
|
<TextField
|
|
|
|
id="outlined-basic"
|
|
|
|
label="Command"
|
|
|
|
variant="outlined"
|
|
|
|
value={cmd}
|
|
|
|
onChange={updateCmd}
|
2024-02-05 02:13:32 +00:00
|
|
|
disabled={!(rcon && rcon.rconLive && !rcon.rconError)}
|
2024-02-11 03:57:01 +00:00
|
|
|
sx={{ width: "100%" }}
|
2023-03-15 15:20:08 +00:00
|
|
|
/>
|
2024-02-05 02:13:32 +00:00
|
|
|
{rcon && rcon.rconLive && !rcon.rconError && (
|
2024-02-11 03:57:01 +00:00
|
|
|
<Button onClick={sendCommand} sx={{ padding: "0 2rem" }}>
|
|
|
|
Send
|
|
|
|
</Button>
|
2024-02-05 02:13:32 +00:00
|
|
|
)}
|
|
|
|
{!(rcon && rcon.rconLive && !rcon.rconError) && (
|
2024-01-15 20:30:31 +00:00
|
|
|
<Button color="secondary">Not Connected</Button>
|
|
|
|
)}
|
2023-03-15 15:20:08 +00:00
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|