mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-26 08:33:12 +09:00
Rewrite instances page
Render the table in the instances page using an antd Table component directly and feed it the data via an automatically updating list of instances.
This commit is contained in:
1 parent
0e9893fcb6
commit
82c3d27bff
4 files changed
+136
-36
No files matched your search
@@ -1,52 +1,104 @@
|
|||||||
import React, { useContext } from "react";
|
import React, { useContext, useState } from "react";
|
||||||
import { useHistory } from "react-router-dom";
|
import { useHistory } from "react-router-dom";
|
||||||
|
import { Button, Form, Input, Modal, PageHeader, Table } from "antd";
|
||||||
|
|
||||||
import libLink from "@clusterio/lib/link";
|
import libLink from "@clusterio/lib/link";
|
||||||
import libConfig from "@clusterio/lib/config";
|
import libConfig from "@clusterio/lib/config";
|
||||||
|
|
||||||
import DataTable from "./data-table";
|
|
||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
import PageLayout from "./PageLayout";
|
import PageLayout from "./PageLayout";
|
||||||
|
import { useInstanceList } from "../model/instance";
|
||||||
|
import { notifyErrorHandler } from "../util/notify";
|
||||||
|
import { useSlaveList } from "../model/slave";
|
||||||
|
|
||||||
|
|
||||||
export default function InstancesPage() {
|
function CreateInstanceButton(props) {
|
||||||
let control = useContext(ControlContext);
|
let control = useContext(ControlContext);
|
||||||
let history = useHistory();
|
let history = useHistory();
|
||||||
|
let [visible, setVisible] = useState(false);
|
||||||
|
let [form] = Form.useForm();
|
||||||
|
|
||||||
async function listInstances() {
|
async function createInstance() {
|
||||||
let result = await libLink.messages.listInstances.send(control);
|
let values = form.getFieldsValue();
|
||||||
return result["list"].map(item => ({
|
if (!values.instanceName) {
|
||||||
key: item["id"],
|
form.setFields([{ name: "mapGenSettings", errors: ["Name is required"] }]);
|
||||||
"Name": item["name"],
|
}
|
||||||
"Assigned Slave": item["assigned_slave"],
|
|
||||||
"Status": item["status"],
|
let instanceConfig = new libConfig.InstanceConfig("control");
|
||||||
}));
|
await instanceConfig.init();
|
||||||
|
instanceConfig.set("instance.name", values.instanceName);
|
||||||
|
let serialized_config = instanceConfig.serialize("master");
|
||||||
|
let result = await libLink.messages.createInstance.send(control, { serialized_config });
|
||||||
|
setVisible(false);
|
||||||
|
history.push(`/instances/${instanceConfig.get("instance.id")}/view`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return <>
|
||||||
|
<Button
|
||||||
|
type="primary"
|
||||||
|
onClick={() => {
|
||||||
|
setVisible(true);
|
||||||
|
}}
|
||||||
|
>Create</Button>
|
||||||
|
<Modal
|
||||||
|
title="Create Instance"
|
||||||
|
okText="Create"
|
||||||
|
visible={visible}
|
||||||
|
onOk={() => { createInstance().catch(notifyErrorHandler("Error creating instance")); }}
|
||||||
|
onCancel={() => { setVisible(false); }}
|
||||||
|
destroyOnClose
|
||||||
|
>
|
||||||
|
<Form form={form}>
|
||||||
|
<Form.Item name="instanceName" label="Name">
|
||||||
|
<Input/>
|
||||||
|
</Form.Item>
|
||||||
|
</Form>
|
||||||
|
</Modal>
|
||||||
|
</>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function InstancesPage() {
|
||||||
|
let history = useHistory();
|
||||||
|
let [slaveList] = useSlaveList();
|
||||||
|
let [instanceList] = useInstanceList();
|
||||||
|
|
||||||
return <PageLayout nav={[{ name: "Instances" }]}>
|
return <PageLayout nav={[{ name: "Instances" }]}>
|
||||||
<h2>Instances</h2>
|
<PageHeader
|
||||||
<DataTable
|
className="site-page-header"
|
||||||
DataFunction={listInstances}
|
title="Instances"
|
||||||
AddRecord={{
|
extra=<CreateInstanceButton />
|
||||||
fields: [{
|
/>
|
||||||
dataIndex: "name",
|
|
||||||
|
<Table
|
||||||
|
columns={[
|
||||||
|
{
|
||||||
title: "Name",
|
title: "Name",
|
||||||
}],
|
dataIndex: "name",
|
||||||
insert: async args => {
|
|
||||||
let instanceConfig = new libConfig.InstanceConfig("control");
|
|
||||||
await instanceConfig.init();
|
|
||||||
instanceConfig.set("instance.name", args.name);
|
|
||||||
let serialized_config = instanceConfig.serialize("master");
|
|
||||||
let response = await libLink.messages.createInstance.send(control, { serialized_config });
|
|
||||||
},
|
},
|
||||||
}}
|
{
|
||||||
TableProps={{
|
title: "Assigned Slave",
|
||||||
onRow: (record, rowIndex) => ({
|
key: "assigned_slave",
|
||||||
onClick: event => {
|
render: instance => {
|
||||||
history.push(`/instances/${record.key}/view`);
|
let slave = slaveList.find(s => s.id === instance.assigned_slave);
|
||||||
|
if (slave) {
|
||||||
|
return slave.name;
|
||||||
|
}
|
||||||
|
return instance.assigned_slave;
|
||||||
},
|
},
|
||||||
}),
|
},
|
||||||
}}
|
{
|
||||||
|
title: "Status",
|
||||||
|
dataIndex: "status",
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
dataSource={instanceList}
|
||||||
|
rowKey={instance => instance["id"]}
|
||||||
|
pagination={false}
|
||||||
|
onRow={(record, rowIndex) => ({
|
||||||
|
onClick: event => {
|
||||||
|
history.push(`/instances/${record.id}/view`);
|
||||||
|
},
|
||||||
|
})}
|
||||||
/>
|
/>
|
||||||
</PageLayout>;
|
</PageLayout>;
|
||||||
}
|
}
|
||||||
@@ -67,6 +67,10 @@ body {
|
|||||||
padding-right: 64px;
|
padding-right: 64px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.site-page-header {
|
||||||
|
padding: 0 0 16px;
|
||||||
|
}
|
||||||
|
|
||||||
.section-header {
|
.section-header {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
@@ -39,3 +39,44 @@ export function useInstance(id) {
|
|||||||
|
|
||||||
return [instance, updateInstance];
|
return [instance, updateInstance];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useInstanceList() {
|
||||||
|
let control = useContext(ControlContext);
|
||||||
|
let [instanceList, setInstanceList] = useState([]);
|
||||||
|
|
||||||
|
function updateInstanceList() {
|
||||||
|
libLink.messages.listInstances.send(control).then(result => {
|
||||||
|
setInstanceList(result.list);
|
||||||
|
}).catch(err => {
|
||||||
|
logger.error(`Failed to list instances:\n${err}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
updateInstanceList();
|
||||||
|
|
||||||
|
function updateHandler(newInstance) {
|
||||||
|
setInstanceList(oldList => {
|
||||||
|
let newList = oldList.concat();
|
||||||
|
let index = newList.findIndex(s => s.id === newInstance.id);
|
||||||
|
if (newInstance.status !== "deleted") {
|
||||||
|
if (index !== -1) {
|
||||||
|
newList[index] = newInstance;
|
||||||
|
} else {
|
||||||
|
newList.push(newInstance);
|
||||||
|
}
|
||||||
|
} else if (index !== -1) {
|
||||||
|
newList.splice(index, 1);
|
||||||
|
}
|
||||||
|
return newList;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
control.onInstanceUpdate(null, updateHandler);
|
||||||
|
return () => {
|
||||||
|
control.offInstanceUpdate(null, updateHandler);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return [instanceList];
|
||||||
|
}
|
||||||
@@ -154,14 +154,17 @@ export class Control extends libLink.Link {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async instanceUpdateEventHandler(message) {
|
async instanceUpdateEventHandler(message) {
|
||||||
let handlers = this.instanceUpdateHandlers.get(message.data.id);
|
let handlers = [].concat(
|
||||||
for (let handler of handlers || []) {
|
this.instanceUpdateHandlers.get(null) || [],
|
||||||
|
this.instanceUpdateHandlers.get(message.data.id) || [],
|
||||||
|
);
|
||||||
|
for (let handler of handlers) {
|
||||||
handler(message.data);
|
handler(message.data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async onInstanceUpdate(id, handler) {
|
async onInstanceUpdate(id, handler) {
|
||||||
if (!Number.isInteger(id)) {
|
if (id !== null && !Number.isInteger(id)) {
|
||||||
throw new Error("Invalid instance id");
|
throw new Error("Invalid instance id");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,8 +205,8 @@ export class Control extends libLink.Link {
|
|||||||
}
|
}
|
||||||
|
|
||||||
await libLink.messages.setInstanceSubscriptions.send(this, {
|
await libLink.messages.setInstanceSubscriptions.send(this, {
|
||||||
all: false,
|
all: this.instanceUpdateHandlers.has(null),
|
||||||
instance_ids: [...this.instanceUpdateHandlers.keys()],
|
instance_ids: [...this.instanceUpdateHandlers.keys()].filter(e => e !== null),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user