mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 23:33:12 +09:00
Refactor link system to JSON serialisable class pattern
Reverse the direction of the API for sending messages from
messageDefinition.send(link, { content });
to
link.send(new MessageClass(content));
and convert all message definitions to use the JSON serialisable class
pattern and explicit registration of message handlers, along with a
end-to-end based routing of messages instead of link to link.
The experience I gained from the existing way messages were defined and
sent over links was that it was difficult to understand for people
unfamiliar with the system. The automatic registration of message
handlers ment that code was being called as if by magic from nowhere
with no easy way to discover it. The manual forwarding and rules for
what can be sent where and how obscured its working.
The use of pure JSON as both input and output from message handlers made
it tedious to work with. Data had to be manually converted between the
program representation and the over the wire format, which made handler
code unplesant to write. Particularly because the over the wire format
used snake_case while the program code uses camelCase for variables.
The idea behind the decision to do it this way was to make it more clear
which data came from the wire and which from the program, and while this
worked it didn't make message handlers any nicer to write.
The direction of the API and magic registration of message handlers also
meant that a future migration to TypeScript would be troublesome. While
it may be possible to implement, the degree of TypeScript magic required
would go starkly against the principle of keep it simple stupid.
By refactoring to using the JSON serialisable class pattern most of
these issues go away. There is no longer a need to distinguish the on
wire format of messages with the program representation because the only
place that deals with the wire format of messages are the toJSON and
fromJSON methods on the classes themselves. Message handlers will work
with proper instances of classes instead of raw data, something that
makes them easier to write, and trivial to type with TypeScript.
End-to-end routing also simplifies sending messages that need to do
mulitple hops to reach the destination. And removes the need for having
all messages be specified in plugin's info module, as they can be routed
without having to be parsed completely.
The explicit registration of message handlers is something that I
thought would be tedious boilerplate code that could be automated away.
But experience now shows it to be the opposite. Allthough dull
repetition, the purpose it serves is to link together the code in a way
that humans can easily find. With explicit registration searching the
name of the message handler in the editor will show the registration that
ties it together with the type of message it handles and the kind of
registration. With implicit registration searching the name of the
message handler turns up only the handler with no clue as to what calls
it.
This commit is contained in:
1 parent
9a967a5779
commit
bcd94709d2
106 files changed
+8173
-4928
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Button, Form, Modal, Select, Typography } from "antd";
|
||||
|
||||
import { libLink } from "@clusterio/lib";
|
||||
import { libData } from "@clusterio/lib";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
@@ -33,10 +33,9 @@ export default function AssignInstanceModal(props) {
|
||||
}
|
||||
|
||||
setApplying(true);
|
||||
libLink.messages.assignInstanceCommand.send(control, {
|
||||
instance_id: props.id,
|
||||
host_id: hostId,
|
||||
}).then(() => {
|
||||
control.send(
|
||||
new libData.InstanceAssignRequest(props.id, hostId)
|
||||
).then(() => {
|
||||
setVisible(false);
|
||||
if (props.onFinish) {
|
||||
props.onFinish();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { useContext } from "react";
|
||||
|
||||
import { libConfig, libLink } from "@clusterio/lib";
|
||||
import { libConfig, libData } from "@clusterio/lib";
|
||||
|
||||
import BaseConfigTree from "./BaseConfigTree";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -10,27 +10,26 @@ export default function ControllerConfigTree(props) {
|
||||
let control = useContext(ControlContext);
|
||||
|
||||
async function retrieveConfig() {
|
||||
let result = await libLink.messages.getControllerConfig.send(control);
|
||||
return result.serialized_config;
|
||||
let result = await control.send(new libData.ControllerConfigGetRequest());
|
||||
return result.serializedConfig;
|
||||
}
|
||||
|
||||
async function setField(field, value) {
|
||||
await libLink.messages.setControllerConfigField.send(control, {
|
||||
await control.send(new libData.ControllerConfigSetFieldRequest(
|
||||
field,
|
||||
value: String(value),
|
||||
});
|
||||
String(value),
|
||||
));
|
||||
}
|
||||
|
||||
async function setProp(field, prop, value) {
|
||||
let data = { field, prop };
|
||||
if (value) {
|
||||
try {
|
||||
data.value = JSON.parse(value);
|
||||
value = JSON.parse(value);
|
||||
} catch (err) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
await libLink.messages.setControllerConfigProp.send(control, data);
|
||||
await control.send(new libData.ControllerConfigSetPropRequest(field, prop, value));
|
||||
}
|
||||
|
||||
return <BaseConfigTree
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Button, Form, Input, Modal } from "antd";
|
||||
|
||||
import { libFactorio, libLink } from "@clusterio/lib";
|
||||
import { libFactorio, libData } from "@clusterio/lib";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
@@ -51,13 +51,10 @@ export default function CreateSaveModal(props) {
|
||||
}
|
||||
|
||||
setCreatingSave(true);
|
||||
libLink.messages.createSave.send(control, {
|
||||
instance_id: props.instance.id,
|
||||
name,
|
||||
seed,
|
||||
map_gen_settings: mapGenSettings,
|
||||
map_settings: mapSettings,
|
||||
}).then(() => {
|
||||
control.sendTo(
|
||||
new libData.InstanceCreateSaveRequest(name, seed, mapGenSettings, mapSettings),
|
||||
{ instanceId: props.instance.id }
|
||||
).then(() => {
|
||||
form.resetFields();
|
||||
setVisible(false);
|
||||
}).catch(
|
||||
|
||||
@@ -20,7 +20,7 @@ export default function HostViewPage(props) {
|
||||
let [instanceList] = useInstanceList();
|
||||
let [host] = useHost(hostId);
|
||||
|
||||
instanceList = instanceList.filter(instance => instance["assigned_host"] === hostId);
|
||||
instanceList = instanceList.filter(instance => instance.assignedHost === hostId);
|
||||
|
||||
let nav = [{ name: "Hosts", path: "/hosts" }, { name: host.name || hostId }];
|
||||
if (host.loading) {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useHistory } from "react-router-dom";
|
||||
import { Button, Form, InputNumber, Modal, PageHeader, Table, Tag, Typography } from "antd";
|
||||
import CopyOutlined from "@ant-design/icons/lib/icons/CopyOutlined";
|
||||
|
||||
import { libLink } from "@clusterio/lib";
|
||||
import { libData } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -35,7 +35,7 @@ function GenerateHostTokenButton(props) {
|
||||
}, []);
|
||||
|
||||
async function generateToken() {
|
||||
let id;
|
||||
let id = null;
|
||||
let values = form.getFieldsValue();
|
||||
if (values.hostId) {
|
||||
id = Number.parseInt(values.hostId, 10);
|
||||
@@ -46,8 +46,8 @@ function GenerateHostTokenButton(props) {
|
||||
form.setFields([{ name: "hostId", errors: [] }]);
|
||||
}
|
||||
|
||||
let result = await libLink.messages.generateHostToken.send(control, { host_id: id || null });
|
||||
setToken(result.token);
|
||||
let newToken = await control.send(new libData.HostGenerateTokenRequest(id));
|
||||
setToken(newToken);
|
||||
setHostId(id);
|
||||
}
|
||||
|
||||
@@ -192,37 +192,37 @@ export default function HostsPage() {
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(a["name"], b["name"]),
|
||||
sorter: (a, b) => strcmp(a.name, b.name),
|
||||
},
|
||||
{
|
||||
title: "Agent",
|
||||
dataIndex: "agent",
|
||||
sorter: (a, b) => strcmp(a["agent"], b["agent"]),
|
||||
sorter: (a, b) => strcmp(a.agent, b.agent),
|
||||
responsive: ["lg"],
|
||||
},
|
||||
{
|
||||
title: "Version",
|
||||
dataIndex: "version",
|
||||
sorter: (a, b) => strcmp(a["version"], b["version"]),
|
||||
sorter: (a, b) => strcmp(a.version, b.version),
|
||||
},
|
||||
{
|
||||
title: "Public address",
|
||||
dataIndex: "public_address",
|
||||
sorter: (a, b) => strcmp(a["public_address"], b["public_address"]),
|
||||
dataIndex: "publicAddress",
|
||||
sorter: (a, b) => strcmp(a.publicAddress, b.publicAddress),
|
||||
},
|
||||
{
|
||||
title: "Connected",
|
||||
key: "connected",
|
||||
render: host => <Tag
|
||||
color={host["connected"] ? "#389e0d" : "#cf1322"}
|
||||
color={host.connected ? "#389e0d" : "#cf1322"}
|
||||
>
|
||||
{host["connected"] ? "Connected" : "Disconnected"}
|
||||
{host.connected ? "Connected" : "Disconnected"}
|
||||
</Tag>,
|
||||
sorter: (a, b) => a["connected"] - b["connected"],
|
||||
sorter: (a, b) => a.connected - b.connected,
|
||||
},
|
||||
]}
|
||||
dataSource={hostList}
|
||||
rowKey={host => host["id"]}
|
||||
rowKey={host => host.id}
|
||||
pagination={false}
|
||||
onRow={(record, rowIndex) => ({
|
||||
onClick: event => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { useContext } from "react";
|
||||
|
||||
import { libConfig, libLink } from "@clusterio/lib";
|
||||
import { libConfig, libData } from "@clusterio/lib";
|
||||
|
||||
import BaseConfigTree from "./BaseConfigTree";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -10,34 +10,23 @@ export default function InstanceConfigTree(props) {
|
||||
let control = useContext(ControlContext);
|
||||
|
||||
async function retrieveConfig() {
|
||||
let result = await libLink.messages.getInstanceConfig.send(control, {
|
||||
instance_id: props.id,
|
||||
});
|
||||
return result.serialized_config;
|
||||
let result = await control.send(new libData.InstanceConfigGetRequest(props.id));
|
||||
return result.config;
|
||||
}
|
||||
|
||||
async function setField(field, value) {
|
||||
await libLink.messages.setInstanceConfigField.send(control, {
|
||||
instance_id: props.id,
|
||||
field,
|
||||
value,
|
||||
});
|
||||
await control.send(new libData.InstanceConfigSetFieldRequest(props.id, field, value));
|
||||
}
|
||||
|
||||
async function setProp(field, prop, value) {
|
||||
let data = {
|
||||
instance_id: props.id,
|
||||
field,
|
||||
prop,
|
||||
};
|
||||
if (value) {
|
||||
try {
|
||||
data.value = JSON.parse(value);
|
||||
value = JSON.parse(value);
|
||||
} catch (err) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
await libLink.messages.setInstanceConfigProp.send(control, data);
|
||||
await control.send(new libData.InstanceConfigSetPropRequest(props.id, field, prop, value));
|
||||
}
|
||||
|
||||
return <BaseConfigTree
|
||||
|
||||
@@ -27,12 +27,12 @@ export default function InstanceList(props) {
|
||||
}
|
||||
|
||||
function instancePublicAddress(instance) {
|
||||
let host = hostList.find(s => s.id === instance.assigned_host);
|
||||
if (host && host.public_address) {
|
||||
if (instance.game_port) {
|
||||
return `${host.public_address}:${instance.game_port}`;
|
||||
let host = hostList.find(s => s.id === instance.assignedHost);
|
||||
if (host && host.publicAddress) {
|
||||
if (instance.gamePort) {
|
||||
return `${host.publicAddress}:${instance.gamePort}`;
|
||||
}
|
||||
return host.public_address;
|
||||
return host.publicAddress;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -46,30 +46,30 @@ export default function InstanceList(props) {
|
||||
},
|
||||
{
|
||||
title: "Assigned Host",
|
||||
key: "assigned_host",
|
||||
render: instance => hostName(instance["assigned_host"]),
|
||||
sorter: (a, b) => strcmp(hostName(a["assigned_host"]), hostName(b["assigned_host"])),
|
||||
key: "assignedHost",
|
||||
render: instance => hostName(instance.assignedHost),
|
||||
sorter: (a, b) => strcmp(hostName(a.assignedHost), hostName(b.assignedHost)),
|
||||
responsive: ["sm"],
|
||||
},
|
||||
{
|
||||
title: "Public address",
|
||||
key: "public_address",
|
||||
key: "publicAddress",
|
||||
render: instance => {
|
||||
let public_address = instancePublicAddress(instance);
|
||||
return public_address ? <>
|
||||
{public_address}
|
||||
let publicAddress = instancePublicAddress(instance);
|
||||
return publicAddress ? <>
|
||||
{publicAddress}
|
||||
<Button
|
||||
type="text"
|
||||
icon={<CopyOutlined/>}
|
||||
onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
navigator.clipboard.writeText(public_address);
|
||||
navigator.clipboard.writeText(publicAddress);
|
||||
message.success("Copied public address!");
|
||||
}}
|
||||
/>
|
||||
</> : "";
|
||||
},
|
||||
sorter: (a, b) => strcmp(hostPublicAddress(a["assigned_host"]), hostPublicAddress(b["assigned_host"])),
|
||||
sorter: (a, b) => strcmp(hostPublicAddress(a.assignedHost), hostPublicAddress(b.assignedHost)),
|
||||
responsive: ["lg"],
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Input, Typography } from "antd";
|
||||
|
||||
import { libLink } from "@clusterio/lib";
|
||||
import { libData } from "@clusterio/lib";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
@@ -22,11 +22,11 @@ export default function InstanceRcon(props) {
|
||||
|
||||
setRunning(true);
|
||||
try {
|
||||
let result = await libLink.messages.sendRcon.send(control, {
|
||||
instance_id: props.id,
|
||||
command: command,
|
||||
});
|
||||
setOutput(result.result);
|
||||
let result = await control.sendTo(
|
||||
new libData.InstanceSendRconRequest(command),
|
||||
{ instanceId: props.id }
|
||||
);
|
||||
setOutput(result);
|
||||
} finally {
|
||||
setRunning(false);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Alert, Button, Descriptions, Dropdown, Menu, Modal, PageHeader, Space,
|
||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||
import DownOutlined from "@ant-design/icons/DownOutlined";
|
||||
|
||||
import { libLink } from "@clusterio/lib";
|
||||
import { libData } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -29,7 +29,7 @@ function InstanceDescription(props) {
|
||||
let account = useAccount();
|
||||
|
||||
const { host, instance } = props;
|
||||
let assigned = instance["assigned_host"] !== null;
|
||||
let assigned = instance.assignedHost !== null;
|
||||
return <Descriptions
|
||||
bordered
|
||||
size="small"
|
||||
@@ -37,21 +37,21 @@ function InstanceDescription(props) {
|
||||
<Descriptions.Item label="Host">
|
||||
{!assigned
|
||||
? <em>Unassigned</em>
|
||||
: host["name"] || instance["assigned_host"]
|
||||
: host.name || instance.assignedHost
|
||||
}
|
||||
{account.hasPermission("core.instance.assign") && <AssignInstanceModal
|
||||
id={instance["id"]}
|
||||
hostId={instance["assigned_host"]}
|
||||
id={instance.id}
|
||||
hostId={instance.assignedHost}
|
||||
buttonProps={{
|
||||
size: "small",
|
||||
style: { float: "Right" },
|
||||
type: assigned ? "default" : "primary",
|
||||
disabled: !["unknown", "unassigned", "stopped"].includes(instance["status"]),
|
||||
disabled: !["unknown", "unassigned", "stopped"].includes(instance.status),
|
||||
}}
|
||||
buttonContent={assigned ? "Reassign" : "Assign"}
|
||||
/>}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Status"><InstanceStatusTag status={instance["status"]} /></Descriptions.Item>
|
||||
<Descriptions.Item label="Status"><InstanceStatusTag status={instance.status} /></Descriptions.Item>
|
||||
</Descriptions>;
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ export default function InstanceViewPage(props) {
|
||||
let account = useAccount();
|
||||
let control = useContext(ControlContext);
|
||||
let [instance] = useInstance(instanceId);
|
||||
let [host] = useHost(Number(instance["assigned_host"]));
|
||||
let [host] = useHost(Number(instance.assignedHost));
|
||||
|
||||
let [exportingData, setExportingData] = useState(false);
|
||||
|
||||
@@ -73,10 +73,10 @@ export default function InstanceViewPage(props) {
|
||||
return <PageLayout nav={nav}><Spin size="large" /></PageLayout>;
|
||||
}
|
||||
|
||||
if (instance.missing || instance["status"] === "deleted") {
|
||||
if (instance.missing || instance.status === "deleted") {
|
||||
return <PageLayout nav={nav}>
|
||||
<Alert
|
||||
message={instance["status"] === "deleted" ? "Instance has been deleted" : "Instance not found" }
|
||||
message={instance.status === "deleted" ? "Instance has been deleted" : "Instance not found" }
|
||||
showIcon
|
||||
description={<>Instance with id {instanceId} was not found on the controller.</>}
|
||||
type="warning"
|
||||
@@ -102,14 +102,14 @@ export default function InstanceViewPage(props) {
|
||||
}
|
||||
if (account.hasPermission("core.instance.extract_players")) {
|
||||
instanceButtonMenuItems.push({
|
||||
disabled: instance["status"] !== "running",
|
||||
disabled: instance.status !== "running",
|
||||
key: "extract",
|
||||
label: "Extract players from save",
|
||||
});
|
||||
}
|
||||
if (account.hasPermission("core.instance.kill")) {
|
||||
instanceButtonMenuItems.push({
|
||||
disabled: ["unknown", "unassigned", "stopped"].includes(instance["status"]),
|
||||
disabled: ["unknown", "unassigned", "stopped"].includes(instance.status),
|
||||
key: "kill",
|
||||
label: "Kill process",
|
||||
});
|
||||
@@ -119,7 +119,7 @@ export default function InstanceViewPage(props) {
|
||||
instanceButtonMenuItems.push({ type: "divider" });
|
||||
}
|
||||
instanceButtonMenuItems.push({
|
||||
disabled: !["unknown", "unassigned", "stopped"].includes(instance["status"]),
|
||||
disabled: !["unknown", "unassigned", "stopped"].includes(instance.status),
|
||||
danger: true,
|
||||
key: "delete",
|
||||
icon: <DeleteOutlined />,
|
||||
@@ -131,8 +131,8 @@ export default function InstanceViewPage(props) {
|
||||
onClick={({ key }) => {
|
||||
if (key === "export") {
|
||||
setExportingData(true);
|
||||
libLink.messages.exportData.send(
|
||||
control, { instance_id: instanceId }
|
||||
control.sendTo(
|
||||
new libData.InstanceExportDataRequest(), { instanceId }
|
||||
).catch(
|
||||
notifyErrorHandler("Error exporting data")
|
||||
).finally(() => {
|
||||
@@ -140,13 +140,13 @@ export default function InstanceViewPage(props) {
|
||||
});
|
||||
|
||||
} else if (key === "extract") {
|
||||
libLink.messages.extractPlayers.send(
|
||||
control, { instance_id: instanceId }
|
||||
control.sendTo(
|
||||
new libData.InstanceExtractPlayersRequest(), { instanceId }
|
||||
).catch(notifyErrorHandler("Error extracting player data"));
|
||||
|
||||
} else if (key === "kill") {
|
||||
libLink.messages.killInstance.send(
|
||||
control, { instance_id: instanceId }
|
||||
control.sendTo(
|
||||
new libData.InstanceKillRequest(), { instanceId }
|
||||
).catch(notifyErrorHandler("Error killing instance"));
|
||||
|
||||
} else if (key === "delete") {
|
||||
@@ -156,8 +156,8 @@ export default function InstanceViewPage(props) {
|
||||
okText: "Delete",
|
||||
okButtonProps: { danger: true },
|
||||
onOk: () => {
|
||||
libLink.messages.deleteInstance.send(
|
||||
control, { instance_id: instanceId }
|
||||
control.send(
|
||||
new libData.InstanceDeleteRequest(instanceId)
|
||||
).then(() => {
|
||||
history.push("/instances");
|
||||
}).catch(notifyErrorHandler("Error deleting instance"));
|
||||
@@ -185,7 +185,7 @@ export default function InstanceViewPage(props) {
|
||||
return <PageLayout nav={nav}>
|
||||
<PageHeader
|
||||
className="site-page-header"
|
||||
title={instance["name"]}
|
||||
title={instance.name}
|
||||
extra={instanceButtons}
|
||||
/>
|
||||
<InstanceDescription host={host} instance={instance} />
|
||||
@@ -201,7 +201,7 @@ export default function InstanceViewPage(props) {
|
||||
{account.hasPermission("core.log.follow") && <LogConsole instances={[instanceId]} />}
|
||||
{
|
||||
account.hasPermission("core.instance.send_rcon")
|
||||
&& <InstanceRcon id={instanceId} disabled={instance["status"] !== "running"} />
|
||||
&& <InstanceRcon id={instanceId} disabled={instance.status !== "running"} />
|
||||
}
|
||||
|
||||
{account.hasPermission("core.instance.get_config") && <InstanceConfigTree id={instanceId} />}
|
||||
|
||||
@@ -2,7 +2,7 @@ import React, { useContext, useState } from "react";
|
||||
import { useHistory } from "react-router-dom";
|
||||
import { Button, Form, Input, Modal, PageHeader } from "antd";
|
||||
|
||||
import { libConfig, libLink } from "@clusterio/lib";
|
||||
import { libConfig, libData } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -28,8 +28,8 @@ function CreateInstanceButton(props) {
|
||||
let instanceConfig = new libConfig.InstanceConfig("control");
|
||||
await instanceConfig.init();
|
||||
instanceConfig.set("instance.name", values.instanceName);
|
||||
let serialized_config = instanceConfig.serialize("controller");
|
||||
await libLink.messages.createInstance.send(control, { serialized_config });
|
||||
let serializedConfig = instanceConfig.serialize("controller");
|
||||
await control.send(new libData.InstanceCreateRequest(serializedConfig));
|
||||
setVisible(false);
|
||||
history.push(`/instances/${instanceConfig.get("instance.id")}/view`);
|
||||
}
|
||||
@@ -72,8 +72,8 @@ export default function InstancesPage() {
|
||||
{account.hasPermission("core.instance.start")
|
||||
&& <Button onClick={e => instanceList.forEach(instance => {
|
||||
if (instance.status === "stopped") {
|
||||
libLink.messages.startInstance.send(
|
||||
control, { instance_id: instance.id, save: null }
|
||||
control.send(
|
||||
new libData.InstanceStartRequest(undefined), { instanceId: instance.id }
|
||||
).catch(notifyErrorHandler("Error starting instance"));
|
||||
}
|
||||
})
|
||||
@@ -83,8 +83,8 @@ export default function InstancesPage() {
|
||||
{account.hasPermission("core.instance.stop")
|
||||
&& <Button onClick={e => instanceList.forEach(instance => {
|
||||
if (["starting", "running"].includes(instance.status)) {
|
||||
libLink.messages.stopInstance.send(
|
||||
control, { instance_id: instance.id }
|
||||
control.sendTo(
|
||||
new libData.InstanceStopRequest(), { instanceId: instance.id }
|
||||
).catch(notifyErrorHandler("Error stopping instance"));
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Button, Form, Input, Modal } from "antd";
|
||||
|
||||
import { libFactorio, libLink } from "@clusterio/lib";
|
||||
import { libFactorio, libData } from "@clusterio/lib";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
@@ -51,13 +51,10 @@ export default function LoadScenarioModal(props) {
|
||||
}
|
||||
|
||||
setLoadingScenario(true);
|
||||
libLink.messages.loadScenario.send(control, {
|
||||
instance_id: props.instance.id,
|
||||
scenario,
|
||||
seed,
|
||||
map_gen_settings: mapGenSettings,
|
||||
map_settings: mapSettings,
|
||||
}).then(() => {
|
||||
control.sendTo(
|
||||
new libData.InstanceLoadScenarioRequest(scenario, seed, mapGenSettings, mapSettings),
|
||||
{ instanceId: props.instance.id }
|
||||
).then(() => {
|
||||
setVisible(false);
|
||||
}).catch(
|
||||
notifyErrorHandler("Error loading scenario")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, { useEffect, useContext, useRef, useState } from "react";
|
||||
import { Typography } from "antd";
|
||||
|
||||
import { libLink } from "@clusterio/lib";
|
||||
import { libData } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -60,8 +60,8 @@ export default function LogConsole(props) {
|
||||
let logFilter = {
|
||||
all: props.all || false,
|
||||
controller: props.controller || false,
|
||||
host_ids: props.hosts || [],
|
||||
instance_ids: props.instances || [],
|
||||
hostIds: props.hosts || [],
|
||||
instanceIds: props.instances || [],
|
||||
};
|
||||
|
||||
// Scroll view to the anchor so it sticks to the bottom
|
||||
@@ -70,12 +70,15 @@ export default function LogConsole(props) {
|
||||
|
||||
if (account.hasPermission("core.log.query")) {
|
||||
setPastLines([<span key={0}>{"Loading past entries..."}<br/></span>]);
|
||||
libLink.messages.queryLog.send(control, {
|
||||
...logFilter,
|
||||
max_level: null,
|
||||
limit: 400,
|
||||
order: "desc",
|
||||
}).then(result => {
|
||||
control.send(new libData.LogQueryRequest(
|
||||
logFilter.all,
|
||||
logFilter.controller,
|
||||
logFilter.hostIds,
|
||||
logFilter.instanceIds,
|
||||
null,
|
||||
400,
|
||||
"desc",
|
||||
)).then(result => {
|
||||
setPastLines(result.log.map((info, index) => formatLog(info, -index - 1)).reverse());
|
||||
}).catch(err => {
|
||||
setPastLines([<span key={0}>{`Error loading log: ${err.message}`}<br/></span>]);
|
||||
|
||||
@@ -13,7 +13,7 @@ import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||
import PlusOutlined from "@ant-design/icons/PlusOutlined";
|
||||
import UploadOutlined from "@ant-design/icons/UploadOutlined";
|
||||
|
||||
import { libData, libLink, libLogging } from "@clusterio/lib";
|
||||
import { libData, libLogging } from "@clusterio/lib";
|
||||
import ControlContext from "./ControlContext";
|
||||
import { useAccount } from "../model/account";
|
||||
import { useModPack } from "../model/mod_pack";
|
||||
@@ -43,22 +43,20 @@ function SearchModsTable(props) {
|
||||
|
||||
useEffect(() => {
|
||||
let canceled = false;
|
||||
libLink.messages.searchMods.send(control, {
|
||||
query: searchText,
|
||||
factorio_version: factorioVersion,
|
||||
page_size: modResultPageSize,
|
||||
page: modResultPage,
|
||||
sort: modResultSort,
|
||||
sort_order: modResultSortOrder,
|
||||
}).then(response => {
|
||||
control.send(new libData.ModSearchRequest(
|
||||
searchText,
|
||||
factorioVersion,
|
||||
modResultPage,
|
||||
modResultPageSize,
|
||||
modResultSort,
|
||||
modResultSortOrder,
|
||||
)).then(response => {
|
||||
if (canceled) {
|
||||
return;
|
||||
}
|
||||
// In React < v18 this causes 3 renders and a partial state necessitating the use
|
||||
// of || 0 on modResultSelectedVersion.get() calls. Remove when updating React.
|
||||
setModResults(response.results.map(
|
||||
({ name, versions }) => ({ name, versions: versions.map(mod => libData.ModInfo.fromJSON(mod)) })
|
||||
));
|
||||
setModResults(response.results);
|
||||
setModResultSelectedVersion(new Map(response.results.map(({ name, versions }) => [name, 0])));
|
||||
setModResultCount(response.result_count);
|
||||
});
|
||||
@@ -686,8 +684,8 @@ export default function ModPackViewPage() {
|
||||
okText="Delete"
|
||||
okButtonProps={{ danger: true }}
|
||||
onConfirm={() => {
|
||||
libLink.messages.deleteModPack.send(
|
||||
control, { id: modPack.id }
|
||||
control.send(
|
||||
new libData.ModPackDeleteRequest(modPack.id)
|
||||
).then(() => {
|
||||
history.push("/mods");
|
||||
}).catch(notifyErrorHandler("Error deleting mod pack"));
|
||||
@@ -785,8 +783,8 @@ export default function ModPackViewPage() {
|
||||
<Button
|
||||
type="primary"
|
||||
onClick={() => {
|
||||
libLink.messages.updateModPack.send(
|
||||
control, { mod_pack: modifiedModPack.toJSON() }
|
||||
control.send(
|
||||
new libData.ModPackUpdateRequest(modifiedModPack)
|
||||
).then(() => {
|
||||
form.resetFields();
|
||||
setChanges([]);
|
||||
|
||||
@@ -5,7 +5,7 @@ import ImportOutlined from "@ant-design/icons/ImportOutlined";
|
||||
import PlusOutlined from "@ant-design/icons/PlusOutlined";
|
||||
import UploadOutlined from "@ant-design/icons/UploadOutlined";
|
||||
|
||||
import { libData, libHelpers, libLink, libLogging } from "@clusterio/lib";
|
||||
import { libData, libHelpers, libLogging } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import { useModList } from "../model/mods";
|
||||
@@ -47,7 +47,7 @@ function ImportModPackButton() {
|
||||
return; // Validation failed
|
||||
}
|
||||
const modPack = libData.ModPack.fromModPackString(values.string);
|
||||
await libLink.messages.createModPack.send(control, { mod_pack: modPack.toJSON() });
|
||||
await control.send(new libData.ModPackCreateRequest(modPack));
|
||||
history.push(`/mods/mod-packs/${modPack.id}/view`);
|
||||
})().catch(notifyErrorHandler("Error creating mod pack"));
|
||||
}}
|
||||
@@ -99,7 +99,7 @@ function CreateModPackButton() {
|
||||
modPack.name = values.name;
|
||||
modPack.factorioVersion = values.factorioVersion;
|
||||
if (values.description) { modPack.description = values.description; }
|
||||
await libLink.messages.createModPack.send(control, { mod_pack: modPack.toJSON() });
|
||||
await control.send(new libData.ModPackCreateRequest(modPack.toJSON()));
|
||||
history.push(`/mods/mod-packs/${modPack.id}/view`);
|
||||
})().catch(notifyErrorHandler("Error creating mod pack"));
|
||||
}}
|
||||
@@ -139,11 +139,11 @@ export default function ModsPage() {
|
||||
{account.hasPermission("core.mod.download")
|
||||
&& <Typography.Link
|
||||
onClick={() => {
|
||||
libLink.messages.downloadMod.send(
|
||||
control, { name: mod.name, version: mod.version }
|
||||
).then(response => {
|
||||
control.send(
|
||||
new libData.ModDownloadRequest(mod.name, mod.version)
|
||||
).then(streamId => {
|
||||
let url = new URL(webRoot, document.location);
|
||||
url.pathname += `api/stream/${response.stream_id}`;
|
||||
url.pathname += `api/stream/${streamId}`;
|
||||
document.location = url;
|
||||
}).catch(
|
||||
notifyErrorHandler("Error downloading save")
|
||||
@@ -153,10 +153,10 @@ export default function ModsPage() {
|
||||
}
|
||||
{account.hasPermission("core.mod.delete")
|
||||
&& <Popconfirm
|
||||
title="Are you sure you want to delete this mod?"
|
||||
title={`Delete ${mod.name}_${mod.version}.zip?`}
|
||||
onConfirm={event => {
|
||||
libLink.messages.deleteMod.send(
|
||||
control, { name: mod.name, version: mod.version }
|
||||
control.send(
|
||||
new libData.ModDeleteRequest(mod.name, mod.version)
|
||||
).catch(notifyErrorHandler("Error deleting mod"));
|
||||
}}
|
||||
okText="Delete"
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useHistory, useParams } from "react-router-dom";
|
||||
import { Button, Checkbox, Form, Input, PageHeader, Popconfirm, Spin } from "antd";
|
||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||
|
||||
import { libLink, libUsers } from "@clusterio/lib";
|
||||
import { libData, libUsers } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -18,8 +18,8 @@ function useRole(id) {
|
||||
|
||||
function updateRole() {
|
||||
// XXX optimize by requesting only the role in question
|
||||
libLink.messages.listRoles.send(control).then(result => {
|
||||
let match = result.list.find(u => u.id === id);
|
||||
control.send(new libData.RoleListRequest()).then(roles => {
|
||||
let match = roles.find(u => u.id === id);
|
||||
if (!match) {
|
||||
setRole({ missing: true });
|
||||
} else {
|
||||
@@ -92,12 +92,9 @@ export default function RoleViewPage() {
|
||||
if (value) { newPermissions.push(name); }
|
||||
}
|
||||
|
||||
libLink.messages.updateRole.send(control, {
|
||||
id: roleId,
|
||||
name: values.name || "",
|
||||
description: values.description || "",
|
||||
permissions: newPermissions,
|
||||
}).then(() => {
|
||||
control.send(
|
||||
new libData.RoleUpdateRequest(roleId, values.name || "", values.description || "", newPermissions)
|
||||
).then(() => {
|
||||
setEdited(false);
|
||||
updateRole();
|
||||
}).catch(notifyErrorHandler("Error applying changes"));
|
||||
@@ -114,8 +111,8 @@ export default function RoleViewPage() {
|
||||
okText="Delete"
|
||||
okButtonProps={{ danger: true }}
|
||||
onConfirm={() => {
|
||||
libLink.messages.deleteRole.send(
|
||||
control, { id: roleId }
|
||||
control.send(
|
||||
new libData.RoleDeleteRequest(roleId)
|
||||
).then(() => {
|
||||
history.push("/roles");
|
||||
}).catch(notifyErrorHandler("Error deleting role"));
|
||||
|
||||
@@ -2,7 +2,7 @@ import React, { useContext, useEffect, useState } from "react";
|
||||
import { useHistory } from "react-router-dom";
|
||||
import { Button, Form, Input, Modal, PageHeader, Table } from "antd";
|
||||
|
||||
import { libLink } from "@clusterio/lib";
|
||||
import { libData } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -26,11 +26,9 @@ function CreateRoleButton() {
|
||||
return;
|
||||
}
|
||||
|
||||
let result = await libLink.messages.createRole.send(control, {
|
||||
name: values.roleName,
|
||||
description: values.description || "",
|
||||
permissions: [],
|
||||
});
|
||||
let result = await control.send(
|
||||
new libData.RoleCreateRequest(values.roleName, values.description || "", [])
|
||||
);
|
||||
setVisible(false);
|
||||
history.push(`/roles/${result.id}/view`);
|
||||
}
|
||||
@@ -68,8 +66,8 @@ export default function RolesPage() {
|
||||
let [roles, setRoles] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
libLink.messages.listRoles.send(control).then(result => {
|
||||
setRoles(result["list"]);
|
||||
control.send(new libData.RoleListRequest()).then(newRoles => {
|
||||
setRoles(newRoles);
|
||||
}).catch(notifyErrorHandler("Error fetching role list"));
|
||||
}, []);
|
||||
|
||||
@@ -84,7 +82,7 @@ export default function RolesPage() {
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
sorter: (a, b) => strcmp(a["name"], b["name"]),
|
||||
sorter: (a, b) => strcmp(a.name, b.name),
|
||||
},
|
||||
{
|
||||
title: "Description",
|
||||
@@ -93,10 +91,10 @@ export default function RolesPage() {
|
||||
]}
|
||||
dataSource={roles}
|
||||
pagination={false}
|
||||
rowKey={role => role["id"]}
|
||||
rowKey={role => role.id}
|
||||
onRow={(role, rowIndex) => ({
|
||||
onClick: event => {
|
||||
history.push(`/roles/${role["id"]}/view`);
|
||||
history.push(`/roles/${role.id}/view`);
|
||||
},
|
||||
})}
|
||||
/>
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
import CaretLeftOutlined from "@ant-design/icons/CaretLeftOutlined";
|
||||
import LeftOutlined from "@ant-design/icons/LeftOutlined";
|
||||
|
||||
import { libLink, libHelpers } from "@clusterio/lib";
|
||||
import { libData, libHelpers } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -36,8 +36,8 @@ function RenameModal(props) {
|
||||
layout="vertical"
|
||||
initialValues={{ newName: props.save.name }}
|
||||
onFinish={values => {
|
||||
libLink.messages.renameSave.send(control,
|
||||
{ instance_id: props.instanceId, old_name: props.save.name, new_name: values.newName }
|
||||
control.send(
|
||||
new libData.InstanceRenameSaveRequest(props.instanceId, props.save.name, values.newName)
|
||||
).then(() => {
|
||||
setVisible(false);
|
||||
form.resetFields();
|
||||
@@ -75,8 +75,8 @@ function CopyModal(props) {
|
||||
layout="vertical"
|
||||
initialValues={{ newName: props.save.name }}
|
||||
onFinish={values => {
|
||||
libLink.messages.copySave.send(control,
|
||||
{ instance_id: props.instanceId, source: props.save.name, destination: values.newName }
|
||||
control.send(
|
||||
new libData.InstanceCopySaveRequest(props.instanceId, props.save.name, values.newName)
|
||||
).then(() => {
|
||||
setVisible(false);
|
||||
form.resetFields();
|
||||
@@ -120,13 +120,13 @@ function TransferModal(props) {
|
||||
onFinish={values => {
|
||||
setVisible(false);
|
||||
let hide = message.loading("Transferring save...", 0);
|
||||
libLink.messages.transferSave.send(control, {
|
||||
instance_id: props.instanceId,
|
||||
source_save: props.save.name,
|
||||
target_instance_id: values.targetInstanceId,
|
||||
target_save: values.transferredName || props.save.name,
|
||||
copy: values.copy,
|
||||
}).then(() => {
|
||||
control.send(new libData.InstanceTransferSaveRequest(
|
||||
props.instanceId,
|
||||
props.save.name,
|
||||
values.targetInstanceId,
|
||||
values.transferredName || props.save.name,
|
||||
values.copy,
|
||||
)).then(() => {
|
||||
message.success("Transfer complete");
|
||||
form.resetFields();
|
||||
}).catch(
|
||||
@@ -209,9 +209,9 @@ export default function SavesList(props) {
|
||||
},
|
||||
{
|
||||
title: "Last Modified",
|
||||
key: "mtime_ms",
|
||||
render: save => new Date(save.mtime_ms).toLocaleString(),
|
||||
sorter: (a, b) => a.mtime_ms - b.mtime_ms,
|
||||
key: "mtimeMs",
|
||||
render: save => new Date(save.mtimeMs).toLocaleString(),
|
||||
sorter: (a, b) => a.mtimeMs - b.mtimeMs,
|
||||
defaultSortOrder: "descend",
|
||||
},
|
||||
]}
|
||||
@@ -227,8 +227,8 @@ export default function SavesList(props) {
|
||||
disabled={props.instance.status !== "stopped"}
|
||||
onClick={() => {
|
||||
setStarting(true);
|
||||
libLink.messages.startInstance.send(
|
||||
control, { instance_id: props.instance.id, save: save.name }
|
||||
control.sendTo(
|
||||
new libData.InstanceStartRequest(save.name), { instanceId: props.instance.id }
|
||||
).catch(
|
||||
notifyErrorHandler("Error loading save")
|
||||
).finally(
|
||||
@@ -245,11 +245,11 @@ export default function SavesList(props) {
|
||||
{account.hasPermission("core.instance.save.download") && <Button
|
||||
disabled={hostOffline}
|
||||
onClick={() => {
|
||||
libLink.messages.downloadSave.send(
|
||||
control, { instance_id: props.instance.id, save: save.name }
|
||||
).then(response => {
|
||||
control.send(
|
||||
new libData.InstanceDownloadSaveRequest(props.instance.id, save.name)
|
||||
).then(streamId => {
|
||||
let url = new URL(webRoot, document.location);
|
||||
url.pathname += `api/stream/${response.stream_id}`;
|
||||
url.pathname += `api/stream/${streamId}`;
|
||||
document.location = url;
|
||||
}).catch(
|
||||
notifyErrorHandler("Error downloading save")
|
||||
@@ -265,8 +265,8 @@ export default function SavesList(props) {
|
||||
placement="top"
|
||||
okButtonProps={{ danger: true }}
|
||||
onConfirm={() => {
|
||||
libLink.messages.deleteSave.send(
|
||||
control, { instance_id: props.instance.id, save: save.name }
|
||||
control.send(
|
||||
new libData.InstanceDeleteSaveRequest(props.instance.id, save.name)
|
||||
).catch(notifyErrorHandler("Error deleting save"));
|
||||
}}
|
||||
>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Button } from "antd";
|
||||
|
||||
import { libLink } from "@clusterio/lib";
|
||||
import { libData } from "@clusterio/lib";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
@@ -16,15 +16,15 @@ export default function StartStopInstanceButton(props) {
|
||||
setSwitching(true);
|
||||
let action;
|
||||
if (props.instance["status"] === "stopped") {
|
||||
action = libLink.messages.startInstance.send(
|
||||
control, { instance_id: props.instance["id"], save: null }
|
||||
action = control.sendTo(
|
||||
new libData.InstanceStartRequest(undefined), { instanceId: props.instance["id"] }
|
||||
).catch(
|
||||
notifyErrorHandler("Error starting instance")
|
||||
);
|
||||
|
||||
} else if (["starting", "running"].includes(props.instance["status"])) {
|
||||
action = libLink.messages.stopInstance.send(
|
||||
control, { instance_id: props.instance["id"] }
|
||||
action = control.sendTo(
|
||||
new libData.InstanceStopRequest(), { instanceId: props.instance["id"] }
|
||||
).catch(
|
||||
notifyErrorHandler("Error stopping instance")
|
||||
);
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
} from "antd";
|
||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||
|
||||
import { libErrors, libLink } from "@clusterio/lib";
|
||||
import { libErrors, libData } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import { useInstanceList } from "../model/instance";
|
||||
@@ -42,8 +42,8 @@ export default function UserViewPage() {
|
||||
let [rolesError, setRolesError] = useState();
|
||||
|
||||
useEffect(() => {
|
||||
libLink.messages.listRoles.send(control).then(result => {
|
||||
setRoles(new Map(result["list"].map(role => [role["id"], role])));
|
||||
control.send(new libData.RoleListRequest()).then(newRoles => {
|
||||
setRoles(new Map(newRoles.map(role => [role.id, role])));
|
||||
}).catch(() => {
|
||||
setRoles(new Map());
|
||||
});
|
||||
@@ -53,9 +53,9 @@ export default function UserViewPage() {
|
||||
if (
|
||||
account.hasPermission("core.user.set_banned")
|
||||
&& !banReasonDirty
|
||||
&& user["ban_reason"] !== form.getFieldValue("ban_reason")
|
||||
&& user.banReason !== form.getFieldValue("banReason")
|
||||
) {
|
||||
form.setFieldsValue({ "ban_reason": user["ban_reason"] });
|
||||
form.setFieldsValue({ "banReason": user.banReason });
|
||||
}
|
||||
}, [account, form, user, banReasonDirty]);
|
||||
|
||||
@@ -83,7 +83,7 @@ export default function UserViewPage() {
|
||||
let roleSelector = <Form.Item
|
||||
noStyle
|
||||
name="roles"
|
||||
initialValue={user["roles"]}
|
||||
initialValue={user.roles}
|
||||
rules={[
|
||||
{ validator: () => (rolesError ? Promise.reject(rolesError) : Promise.resolve()) },
|
||||
]}
|
||||
@@ -95,19 +95,19 @@ export default function UserViewPage() {
|
||||
showArrow={true}
|
||||
filterOption={(inputValue, option) => {
|
||||
let role = roles.get(option.value);
|
||||
return role && role["name"].toLowerCase().includes(inputValue.toLowerCase());
|
||||
return role && role.name.toLowerCase().includes(inputValue.toLowerCase());
|
||||
}}
|
||||
>
|
||||
{roles && [...roles.values()].map(r => <Select.Option
|
||||
key={r["id"]}
|
||||
value={r["id"]}
|
||||
>{r["name"]}</Select.Option>)}
|
||||
key={r.id}
|
||||
value={r.id}
|
||||
>{r.name}</Select.Option>)}
|
||||
</Select>
|
||||
</Form.Item>;
|
||||
|
||||
function instanceName(id) {
|
||||
let instance = instanceList.find(i => i.id === id);
|
||||
return instance ? instance["name"] : id;
|
||||
return instance ? instance.name : id;
|
||||
}
|
||||
|
||||
return <PageLayout nav={nav}>
|
||||
@@ -116,9 +116,9 @@ export default function UserViewPage() {
|
||||
title={<Space>
|
||||
{userName}
|
||||
<span>
|
||||
{user["is_admin"] && <Tag color="gold">Admin</Tag>}
|
||||
{user["is_whitelisted"] && <Tag>Whitelisted</Tag>}
|
||||
{user["is_banned"] && <Tag color="red">Banned</Tag>}
|
||||
{user.isAdmin && <Tag color="gold">Admin</Tag>}
|
||||
{user.isWhitelisted && <Tag>Whitelisted</Tag>}
|
||||
{user.isBanned && <Tag color="red">Banned</Tag>}
|
||||
</span>
|
||||
</Space>}
|
||||
extra={<>
|
||||
@@ -127,8 +127,8 @@ export default function UserViewPage() {
|
||||
) && <Button
|
||||
danger
|
||||
onClick={() => {
|
||||
libLink.messages.revokeUserToken.send(
|
||||
control, { name: userName }
|
||||
control.send(
|
||||
new libData.UserRevokeTokenRequest(userName)
|
||||
).then(() => {
|
||||
notify("User token revoked");
|
||||
}, err => {
|
||||
@@ -144,14 +144,14 @@ export default function UserViewPage() {
|
||||
{account.hasPermission("core.user.delete") && <Popconfirm
|
||||
title={<>
|
||||
Delete user account and all data associated with it?
|
||||
{(user["is_banned"] && <><br/>This will remove the user ban!</>)}
|
||||
{(user.isBanned && <><br/>This will remove the user ban!</>)}
|
||||
</>}
|
||||
placement="bottomRight"
|
||||
okText="Delete"
|
||||
okButtonProps={{ danger: true }}
|
||||
onConfirm={() => {
|
||||
libLink.messages.deleteUser.send(
|
||||
control, { name: userName }
|
||||
control.send(
|
||||
new libData.UserDeleteRequest(userName)
|
||||
).then(() => {
|
||||
history.push("/users");
|
||||
}).catch(notifyErrorHandler("Error deleting user"));
|
||||
@@ -187,8 +187,8 @@ export default function UserViewPage() {
|
||||
<Col flex="auto">
|
||||
{account.hasPermission("core.user.update_roles")
|
||||
? roleSelector
|
||||
: [...user["roles"]].map(id => <Tag key={id}>{
|
||||
roles ? (roles.get(id) || { name: id })["name"] : id
|
||||
: [...user.roles].map(id => <Tag key={id}>{
|
||||
roles ? (roles.get(id) || { name: id }).name : id
|
||||
}</Tag>)
|
||||
}
|
||||
</Col>
|
||||
@@ -200,10 +200,9 @@ export default function UserViewPage() {
|
||||
onClick={() => {
|
||||
let newRoles = form.getFieldValue("roles");
|
||||
setApplyingRoles(true);
|
||||
libLink.messages.updateUserRoles.send(control, {
|
||||
name: userName,
|
||||
roles: newRoles,
|
||||
}).then(() => {
|
||||
control.send(
|
||||
new libData.UserUpdateRolesRequest(userName, newRoles)
|
||||
).then(() => {
|
||||
setRolesDirty(false);
|
||||
setRolesError();
|
||||
}).catch(err => {
|
||||
@@ -219,25 +218,21 @@ export default function UserViewPage() {
|
||||
</Form.Item>
|
||||
{account.hasPermission("core.user.set_admin") && <Form.Item label="In-game Admin">
|
||||
<Switch
|
||||
checked={user["is_admin"]}
|
||||
checked={user.isAdmin}
|
||||
onClick={() => {
|
||||
libLink.messages.setUserAdmin.send(control, {
|
||||
name: userName,
|
||||
admin: !user["is_admin"],
|
||||
create: false,
|
||||
}).catch(notifyErrorHandler("Error toggling user admin status"));
|
||||
control.send(
|
||||
new libData.UserSetAdminRequest(userName, false, !user.isAdmin)
|
||||
).catch(notifyErrorHandler("Error toggling user admin status"));
|
||||
}}
|
||||
/>
|
||||
</Form.Item>}
|
||||
{account.hasPermission("core.user.set_whitelisted") && <Form.Item label="Whitelisted">
|
||||
<Switch
|
||||
checked={user["is_whitelisted"]}
|
||||
checked={user.isWhitelisted}
|
||||
onClick={() => {
|
||||
libLink.messages.setUserWhitelisted.send(control, {
|
||||
name: userName,
|
||||
whitelisted: !user["is_whitelisted"],
|
||||
create: false,
|
||||
}).catch(notifyErrorHandler("Error toggling user whitelisted status"));
|
||||
control.send(
|
||||
new libData.UserSetWhitelistedRequest(userName, false, !user.isWhitelisted)
|
||||
).catch(notifyErrorHandler("Error toggling user whitelisted status"));
|
||||
}}
|
||||
/>
|
||||
</Form.Item>}
|
||||
@@ -245,50 +240,45 @@ export default function UserViewPage() {
|
||||
? <Form.Item label="Ban reason">
|
||||
<Row gutter={[8, 8]} justify={"end"}>
|
||||
<Col flex="auto" style={{ minWidth: "20em" }}>
|
||||
<Form.Item noStyle name="ban_reason" initialValue={user["ban_reason"]}>
|
||||
<Form.Item noStyle name="banReason" initialValue={user.banReason}>
|
||||
<Input
|
||||
onChange={e => {
|
||||
setBanReasonDirty(e.target.value !== user["ban_reason"]);
|
||||
setBanReasonDirty(e.target.value !== user.banReason);
|
||||
}}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col flex="0 0 auto">
|
||||
{user["is_banned"]
|
||||
{user.isBanned
|
||||
? <Space>
|
||||
<Button
|
||||
type={banReasonDirty ? "primary" : "default"}
|
||||
onClick={() => {
|
||||
libLink.messages.setUserBanned.send(control, {
|
||||
name: userName,
|
||||
create: false,
|
||||
banned: true,
|
||||
reason: form.getFieldValue("ban_reason"),
|
||||
}).then(() => {
|
||||
control.send(
|
||||
new libData.UserSetBannedRequest(
|
||||
userName, false, true, form.getFieldValue("banReason")
|
||||
)
|
||||
).then(() => {
|
||||
setBanReasonDirty(false);
|
||||
}).catch(notifyErrorHandler("Error updating ban"));
|
||||
}}
|
||||
>Update</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
libLink.messages.setUserBanned.send(control, {
|
||||
name: userName,
|
||||
create: false,
|
||||
banned: false,
|
||||
reason: "",
|
||||
}).catch(notifyErrorHandler("Error unbanning user"));
|
||||
control.send(
|
||||
new libData.UserSetBannedRequest(userName, false, false, "")
|
||||
).catch(notifyErrorHandler("Error unbanning user"));
|
||||
}}
|
||||
>Unban</Button>
|
||||
</Space>
|
||||
: <Button
|
||||
type={banReasonDirty ? "primary" : "default"}
|
||||
onClick={() => {
|
||||
libLink.messages.setUserBanned.send(control, {
|
||||
name: userName,
|
||||
create: false,
|
||||
banned: true,
|
||||
reason: form.getFieldValue("ban_reason"),
|
||||
}).then(() => {
|
||||
control.send(
|
||||
new libData.UserSetBannedRequest(
|
||||
userName, false, true, form.getFieldValue("banReason")
|
||||
)
|
||||
).then(() => {
|
||||
setBanReasonDirty(false);
|
||||
}).catch(notifyErrorHandler("Error banning user"));
|
||||
}}
|
||||
@@ -297,17 +287,17 @@ export default function UserViewPage() {
|
||||
</Col>
|
||||
</Row>
|
||||
</Form.Item>
|
||||
: user["is_banned"]
|
||||
&& <Form.Item label="Ban reason">{user["ban_reason"]}</Form.Item>
|
||||
: user.isBanned
|
||||
&& <Form.Item label="Ban reason">{user.banReason}</Form.Item>
|
||||
}
|
||||
</Form>
|
||||
<SectionHeader title="Player stats" />
|
||||
<Descriptions size="small" bordered column={{ xs: 1, sm: 2, lg: 3 }}>
|
||||
<Descriptions.Item label="Total online time">
|
||||
{formatDuration(user["player_stats"]["online_time_ms"] || 0)}
|
||||
{formatDuration(user.playerStats.onlineTimeMs || 0)}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Total join count">
|
||||
{user["player_stats"]["join_count"] || 0}
|
||||
{user.playerStats.joinCount || 0}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Last seen">
|
||||
{formatLastSeen(user) || " "}
|
||||
@@ -327,14 +317,14 @@ export default function UserViewPage() {
|
||||
{
|
||||
title: "Online time",
|
||||
key: "onlineTime",
|
||||
render: ([, stats]) => formatDuration(stats["online_time_ms"] || 0),
|
||||
sorter: (a, b) => (a[1]["online_time_ms"] || 0) - (b[1]["online_time_ms"] || 0),
|
||||
render: ([, stats]) => formatDuration(stats.onlineTimeMs || 0),
|
||||
sorter: (a, b) => (a[1].onlineTimeMs || 0) - (b[1].onlineTimeMs || 0),
|
||||
},
|
||||
{
|
||||
title: "Join count",
|
||||
key: "joinCoint",
|
||||
render: ([, stats]) => stats["join_count"] || 0,
|
||||
sorter: (a, b) => (a[1]["join_count"] || 0) - (b[1]["join_count"] || 0),
|
||||
render: ([, stats]) => stats.joinCount || 0,
|
||||
sorter: (a, b) => (a[1].joinCount || 0) - (b[1].joinCount || 0),
|
||||
responsive: ["sm"],
|
||||
},
|
||||
{
|
||||
@@ -344,7 +334,7 @@ export default function UserViewPage() {
|
||||
sorter: (a, b) => sortLastSeen(user, user, a[0], b[0]),
|
||||
},
|
||||
]}
|
||||
dataSource={user["instance_stats"]}
|
||||
dataSource={[...user.instanceStats.entries()]}
|
||||
pagination={false}
|
||||
rowKey={([id]) => id}
|
||||
onRow={([id], rowIndex) => ({
|
||||
|
||||
@@ -2,7 +2,7 @@ import React, { useEffect, useContext, useState } from "react";
|
||||
import { useHistory } from "react-router-dom";
|
||||
import { Button, Form, Input, Modal, PageHeader, Space, Table, Tag } from "antd";
|
||||
|
||||
import { libLink } from "@clusterio/lib";
|
||||
import { libData } from "@clusterio/lib";
|
||||
|
||||
import { useAccount } from "../model/account";
|
||||
import ControlContext from "./ControlContext";
|
||||
@@ -28,7 +28,7 @@ function CreateUserButton() {
|
||||
return;
|
||||
}
|
||||
|
||||
await libLink.messages.createUser.send(control, { name: values.userName });
|
||||
await control.send(new libData.UserCreateRequest(values.userName));
|
||||
setVisible(false);
|
||||
history.push(`/users/${values.userName}/view`);
|
||||
}
|
||||
@@ -64,8 +64,8 @@ export default function UsersPage() {
|
||||
let [roles, setRoles] = useState(new Map());
|
||||
|
||||
useEffect(() => {
|
||||
libLink.messages.listRoles.send(control).then(result => {
|
||||
setRoles(new Map(result["list"].map(role => [role["id"], role])));
|
||||
control.send(new libData.RoleListRequest()).then(newRoles => {
|
||||
setRoles(new Map(newRoles.map(role => [role.id, role])));
|
||||
}).catch(() => {
|
||||
// ignore
|
||||
});
|
||||
@@ -83,28 +83,28 @@ export default function UsersPage() {
|
||||
title: "Name",
|
||||
key: "name",
|
||||
render: user => <Space>
|
||||
{user["name"]}
|
||||
{user.name}
|
||||
<span>
|
||||
{user["is_admin"] && <Tag color="gold">Admin</Tag>}
|
||||
{user["is_whitelisted"] && <Tag>Whitelisted</Tag>}
|
||||
{user["is_banned"] && <Tag color="red">Banned</Tag>}
|
||||
{user.isAdmin && <Tag color="gold">Admin</Tag>}
|
||||
{user.isWhitelisted && <Tag>Whitelisted</Tag>}
|
||||
{user.isBanned && <Tag color="red">Banned</Tag>}
|
||||
</span>
|
||||
</Space>,
|
||||
defaultSortOrder: "ascend",
|
||||
sorter: (a, b) => strcmp(a["name"], b["name"]),
|
||||
sorter: (a, b) => strcmp(a.name, b.name),
|
||||
},
|
||||
{
|
||||
title: "Roles",
|
||||
key: "roles",
|
||||
render: user => user.roles.map(id => <Tag key={id}>{(roles.get(id) || { name: id })["name"]}</Tag>),
|
||||
render: user => user.roles.map(id => <Tag key={id}>{(roles.get(id) || { name: id }).name}</Tag>),
|
||||
},
|
||||
{
|
||||
title: "Online time",
|
||||
key: "onlineTime",
|
||||
render: user => user["player_stats"]["online_time_ms"]
|
||||
&& formatDuration(user["player_stats"]["online_time_ms"]),
|
||||
sorter: (a, b) => (a["player_stats"]["online_time_ms"] || 0) -
|
||||
(b["player_stats"]["online_time_ms"] || 0),
|
||||
render: user => (user.playerStats.onlineTimeMs
|
||||
? formatDuration(user.playerStats.onlineTimeMs) : null),
|
||||
sorter: (a, b) => (a.playerStats.onlineTimeMs || 0) -
|
||||
(b.playerStats.onlineTimeMs || 0),
|
||||
responsive: ["lg"],
|
||||
},
|
||||
{
|
||||
@@ -117,10 +117,10 @@ export default function UsersPage() {
|
||||
]}
|
||||
dataSource={userList}
|
||||
pagination={false}
|
||||
rowKey={user => user["name"]}
|
||||
rowKey={user => user.name}
|
||||
onRow={(user, rowIndex) => ({
|
||||
onClick: event => {
|
||||
history.push(`/users/${user["name"]}/view`);
|
||||
history.push(`/users/${user.name}/view`);
|
||||
},
|
||||
})}
|
||||
/>
|
||||
|
||||
Reference in new issue
Block a user