53 lines
1.6 KiB
React
Raw Normal View History

2020-08-02 20:22:53 +02:00
import Panel from "../components/Panel";
import React, {useEffect, useRef, useState} from "react";
2020-08-22 19:28:22 +02:00
import socket from "../../api/socket";
2020-08-02 20:22:53 +02:00
2020-08-18 16:38:55 +02:00
const Console = ({serverStatus}) => {
2020-08-02 20:22:53 +02:00
const [logs, setLogs] = useState([]);
const consoleInput = useRef(null);
const isRunning = serverStatus.status === 'running';
2020-08-02 20:22:53 +02:00
useEffect(() => {
2020-08-18 16:38:55 +02:00
2020-08-22 19:28:22 +02:00
const appendLog = line => {
setLogs(lines => [...lines, line]);
2020-08-18 16:38:55 +02:00
}
2020-12-06 17:08:53 +01:00
socket.on('gamelog', appendLog)
2020-08-22 19:28:22 +02:00
socket.emit('log subscribe')
2020-08-02 20:22:53 +02:00
consoleInput.current?.focus();
2020-08-18 16:38:55 +02:00
return () => {
2020-12-06 17:08:53 +01:00
socket.off('gamelog', appendLog);
socket.emit("log unsubscribe")
2020-08-18 16:38:55 +02:00
}
2020-08-02 20:22:53 +02:00
}, []);
return (
<Panel
title="Console"
content={
2020-08-18 17:14:45 +02:00
isRunning
2020-08-22 19:28:22 +02:00
? <>
<ul>
{logs?.map((log, i) => (<li key={i}>{log}</li>))}
</ul>
<input type="text" ref={consoleInput}
className="shadow appearance-none border w-full py-2 px-3 text-black" onKeyPress={e => {
2020-08-18 16:38:55 +02:00
if (e.key === "Enter" && socket) {
2020-08-22 19:28:22 +02:00
socket.emit("command send", consoleInput.current.value);
2020-08-02 20:22:53 +02:00
consoleInput.current.value = ""
}
}
2020-08-22 19:28:22 +02:00
}/>
</>
: <p className="text-red-light pt-4">
The console is not available, because Factorio is not running.
</p>
2020-08-02 20:22:53 +02:00
}
/>
)
}
export default Console;