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";
|
|
|
|
import RconSocket from "./RconSocket.js";
|
|
|
|
import "@mcl/css/rcon.css";
|
|
|
|
|
|
|
|
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 (
|
|
|
|
<Box>
|
|
|
|
<div className="rconLogsWrapper" ref={logsRef}>
|
|
|
|
{logs.map((v, k) => (
|
|
|
|
<Box key={k}>
|
|
|
|
{v}
|
|
|
|
<br />
|
|
|
|
</Box>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
<Box className="rconActions">
|
|
|
|
<TextField
|
|
|
|
id="outlined-basic"
|
|
|
|
label="Command"
|
|
|
|
variant="outlined"
|
|
|
|
value={cmd}
|
|
|
|
onChange={updateCmd}
|
2024-01-15 20:30:31 +00:00
|
|
|
disabled={!(rcon && rcon.rconLive)}
|
2023-03-15 15:20:08 +00:00
|
|
|
/>
|
2024-01-15 20:30:31 +00:00
|
|
|
{rcon && rcon.rconLive && <Button onClick={sendCommand}>Send</Button>}
|
|
|
|
{!(rcon && rcon.rconLive) && (
|
|
|
|
<Button color="secondary">Not Connected</Button>
|
|
|
|
)}
|
2023-03-15 15:20:08 +00:00
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|