mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 07:24:53 +09:00
Refactor web interface
Reimplement the pages of the web interface in order to make them prettier and more easily amended by plugins.
This commit is contained in:
1 parent
0c0626564a
commit
f67f56e6de
32 files changed
+1502
-1059
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { notification } from "antd";
|
||||
|
||||
|
||||
export default function notify(message, type = "info", description = undefined) {
|
||||
notification[type]({
|
||||
message: typeof message === "string" ? message : "ERROR: See console",
|
||||
@@ -8,3 +9,25 @@ export default function notify(message, type = "info", description = undefined)
|
||||
});
|
||||
if (typeof message !== "string") console.error(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Promise rejection handler notifying the user
|
||||
*
|
||||
* Shows the error occuring in a notification card.
|
||||
*
|
||||
* @param {string} message -
|
||||
* User facing message to give context to the error. Should say which
|
||||
* operation failed.
|
||||
* @returns {function(err)} function showing the error passed to it.
|
||||
*/
|
||||
export function notifyErrorHandler(message) {
|
||||
return function handler(err) {
|
||||
console.error(err);
|
||||
notification.error({
|
||||
message,
|
||||
description: err.message,
|
||||
duration: 0,
|
||||
placement: "bottomRight",
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -3,34 +3,6 @@ const libPlugin = require("@clusterio/lib/plugin");
|
||||
const libErrors = require("@clusterio/lib/errors");
|
||||
const version = require("../../package.json").version;
|
||||
|
||||
/**
|
||||
* Format a parsed Factorio output message
|
||||
*
|
||||
* Formats a parsed Factorio output from lib/factorio back into the
|
||||
* text string it was parsed from.
|
||||
*
|
||||
* @param {Object} output - Factorio server output.
|
||||
* @returns {string} origial output text.
|
||||
* @private
|
||||
*/
|
||||
function formatOutput(output) {
|
||||
let time = "";
|
||||
if (output.format === "seconds") {
|
||||
time = `${output.time.padStart(8)} `;
|
||||
} else if (output.format === "date") {
|
||||
time = `${output.time} `;
|
||||
}
|
||||
|
||||
let info = "";
|
||||
if (output.type === "log") {
|
||||
info = `${output.level} ${output.file}: `;
|
||||
|
||||
} else if (output.type === "action") {
|
||||
info = `[${output.action}] `;
|
||||
}
|
||||
|
||||
return `${time}${info}${output.message}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connector for control connection to master server
|
||||
@@ -75,16 +47,59 @@ export class Control extends libLink.Link {
|
||||
for (let controlPlugin of controlPlugins.values()) {
|
||||
libPlugin.attachPluginMessages(this, controlPlugin.info, controlPlugin);
|
||||
}
|
||||
|
||||
this.instanceOutputHandlers = new Map();
|
||||
}
|
||||
|
||||
async instanceOutputEventHandler(message) {
|
||||
let { instance_id, output } = message.data;
|
||||
console.log(formatOutput(output));
|
||||
if (window.instanceOutputEventHandler) {
|
||||
window.instanceOutputEventHandler({instance_id, output});
|
||||
let handlers = this.instanceOutputHandlers.get(instance_id);
|
||||
for (let handler of handlers || []) {
|
||||
handler(output);
|
||||
}
|
||||
}
|
||||
|
||||
async onInstanceOutput(id, handler) {
|
||||
if (!Number.isInteger(id)) {
|
||||
throw new Error("Invalid instance id");
|
||||
}
|
||||
|
||||
let handlers = this.instanceOutputHandlers.get(id);
|
||||
if (!handlers) {
|
||||
handlers = [];
|
||||
this.instanceOutputHandlers.set(id, handlers);
|
||||
}
|
||||
|
||||
handlers.push(handler);
|
||||
|
||||
if (handlers.length === 1) {
|
||||
await this.updateInstanceOutputSubscriptions();
|
||||
}
|
||||
}
|
||||
|
||||
async offInstanceOutput(id, handler) {
|
||||
let handlers = this.instanceOutputHandlers.get(id);
|
||||
if (!handlers || !handlers.length) {
|
||||
throw new Error(`No handlers for instance ${id} exist`);
|
||||
}
|
||||
|
||||
let index = handlers.lastIndexOf(handler);
|
||||
if (index === -1) {
|
||||
throw new Error(`Given handler is not registered for instance ${id}`);
|
||||
}
|
||||
|
||||
handlers.splice(index, 1);
|
||||
if (!handlers.length) {
|
||||
await this.updateInstanceOutputSubscriptions();
|
||||
}
|
||||
}
|
||||
|
||||
async updateInstanceOutputSubscriptions() {
|
||||
await libLink.messages.setInstanceOutputSubscriptions.send(this, {
|
||||
instance_ids: [...this.instanceOutputHandlers.keys()],
|
||||
});
|
||||
}
|
||||
|
||||
async debugWsMessageEventHandler(message) {
|
||||
console.log("WS", message.data.direction, message.data.content);
|
||||
}
|
||||
@@ -100,6 +115,6 @@ export class Control extends libLink.Link {
|
||||
}
|
||||
}
|
||||
|
||||
await this.connector.close(1001, "Control Quit");
|
||||
await this.connector.close(1000, "Control Quit");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user