mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
Add remote configuration of hosts
Implement remote editing of the host config in a similar fasion to how the remote editing of the controller config works.
This commit is contained in:
1 parent
705fb90740
commit
707c0bb453
10 files changed
+325
-9
No files matched your search
@@ -26,12 +26,13 @@ function getInitialValues(config: lib.Config<any>, props: BaseConfigTreeProps) {
|
||||
}
|
||||
|
||||
type BaseConfigTreeProps = {
|
||||
ConfigClass: typeof lib.ControllerConfig | typeof lib.InstanceConfig;
|
||||
ConfigClass: typeof lib.ControllerConfig | typeof lib.HostConfig | typeof lib.InstanceConfig;
|
||||
retrieveConfig: () => Promise<lib.ConfigSchema>;
|
||||
setField: (field:string, value:any) => Promise<void>;
|
||||
setProp: (field:string, prop:string, value:any) => Promise<void>;
|
||||
id?: number;
|
||||
onApply?: () => void;
|
||||
available?: boolean;
|
||||
};
|
||||
export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
||||
let [config, setConfig] = useState<lib.Config<any>|null>(null);
|
||||
@@ -39,6 +40,7 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
||||
let [changedFields, setChangedFields] = useState<Set<string>>(new Set());
|
||||
let [errorFields, setErrorFields] = useState(new Map());
|
||||
let [applying, setApplying] = useState(false);
|
||||
const available = props.available ?? true;
|
||||
|
||||
async function updateConfig() {
|
||||
let serializedConfig = await props.retrieveConfig();
|
||||
@@ -47,10 +49,12 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
||||
return newConfig;
|
||||
}
|
||||
useEffect(() => {
|
||||
updateConfig().then(updatedConfig => {
|
||||
form.setFieldsValue(getInitialValues(updatedConfig, props));
|
||||
});
|
||||
}, [props.id]);
|
||||
if (available) {
|
||||
updateConfig().then(updatedConfig => {
|
||||
form.setFieldsValue(getInitialValues(updatedConfig, props));
|
||||
});
|
||||
}
|
||||
}, [props.id, available]);
|
||||
|
||||
function renderInput(def: lib.FieldDefinition) {
|
||||
if (def.type === "boolean") {
|
||||
@@ -66,8 +70,22 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
||||
return `Unknown type ${def.type}`;
|
||||
}
|
||||
|
||||
if (!available) {
|
||||
return <>
|
||||
<Title level={5} style={{ marginTop: 16 }}>Config</Title>
|
||||
<Card size="small" className="config-container">
|
||||
Config is currently unavailable.
|
||||
</Card>
|
||||
</>;
|
||||
}
|
||||
|
||||
if (config === null) {
|
||||
return <Spin/>;
|
||||
return <>
|
||||
<Title level={5} style={{ marginTop: 16 }}>Config</Title>
|
||||
<Card size="small" className="config-container">
|
||||
<Spin size="large" />;
|
||||
</Card>
|
||||
</>;
|
||||
}
|
||||
|
||||
let initialValues: any = {};
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import React, { useContext } from "react";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import BaseConfigTree from "./BaseConfigTree";
|
||||
import ControlContext from "./ControlContext";
|
||||
|
||||
|
||||
export default function HostConfigTree(props: { id: number, available: boolean }) {
|
||||
let control = useContext(ControlContext);
|
||||
|
||||
async function retrieveConfig() {
|
||||
return await control.sendTo({ hostId: props.id }, new lib.HostConfigGetRequest());
|
||||
}
|
||||
|
||||
async function setField(field: string, value: any) {
|
||||
await control.sendTo({ hostId: props.id }, new lib.HostConfigSetFieldRequest(field, value));
|
||||
}
|
||||
|
||||
async function setProp(field: string, prop: string, value: any) {
|
||||
if (value) {
|
||||
try {
|
||||
value = JSON.parse(value);
|
||||
} catch (err) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
await control.sendTo({ hostId: props.id }, new lib.HostConfigSetPropRequest(field, prop, value));
|
||||
}
|
||||
|
||||
return <BaseConfigTree
|
||||
ConfigClass={lib.HostConfig}
|
||||
retrieveConfig={retrieveConfig}
|
||||
setField={setField}
|
||||
setProp={setProp}
|
||||
id={props.id}
|
||||
available={props.available}
|
||||
/>;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { useParams } from "react-router-dom";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import notify, { notifyErrorHandler } from "../util/notify";
|
||||
import ControlContext from "./ControlContext";
|
||||
import HostConfigTree from "./HostConfigTree";
|
||||
import InstanceList from "./InstanceList";
|
||||
import LogConsole from "./LogConsole";
|
||||
import { useAccount } from "../model/account";
|
||||
@@ -100,6 +101,7 @@ export default function HostViewPage() {
|
||||
<Title level={5} style={{ marginTop: 16 }}>Console</Title>
|
||||
<LogConsole hosts={[hostId]} />
|
||||
</>}
|
||||
{account.hasPermission("core.host.get_config") && <HostConfigTree id={hostId} available={host.connected} />}
|
||||
<PluginExtra component="HostViewPage" host={host} />
|
||||
</PageLayout>;
|
||||
}
|
||||
Reference in new issue
Block a user