mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 08:13:12 +09:00
Update webui config to use bulk set
This commit is contained in:
4 files changed
+33
-91
No files matched your search
@@ -9,6 +9,7 @@ import * as lib from "@clusterio/lib";
|
|||||||
import ControlContext from "./ControlContext";
|
import ControlContext from "./ControlContext";
|
||||||
import { InputComponent, InputComponentProps } from "../BaseWebPlugin";
|
import { InputComponent, InputComponentProps } from "../BaseWebPlugin";
|
||||||
import type { Control } from "../util/websocket";
|
import type { Control } from "../util/websocket";
|
||||||
|
import { notifyErrorHandler } from "../util/notify";
|
||||||
|
|
||||||
const { Title } = Typography;
|
const { Title } = Typography;
|
||||||
|
|
||||||
@@ -67,8 +68,7 @@ function renderInput(inputComponents: Record<string, InputComponent>, def: lib.F
|
|||||||
type BaseConfigTreeProps = {
|
type BaseConfigTreeProps = {
|
||||||
ConfigClass: typeof lib.ControllerConfig | typeof lib.HostConfig | typeof lib.InstanceConfig;
|
ConfigClass: typeof lib.ControllerConfig | typeof lib.HostConfig | typeof lib.InstanceConfig;
|
||||||
retrieveConfig: () => Promise<lib.ConfigSchema>;
|
retrieveConfig: () => Promise<lib.ConfigSchema>;
|
||||||
setField: (field:string, value:any) => Promise<void>;
|
setConfig: (fields: Record<string, string | Record<string, string>>) => Promise<void>;
|
||||||
setProp: (field:string, prop:string, value:any) => Promise<void>;
|
|
||||||
id?: number;
|
id?: number;
|
||||||
onApply?: () => void;
|
onApply?: () => void;
|
||||||
available?: boolean;
|
available?: boolean;
|
||||||
@@ -157,32 +157,25 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
|||||||
loading={applying}
|
loading={applying}
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
setApplying(true);
|
setApplying(true);
|
||||||
for (let field of changedFields) {
|
const fields: Record<string, string | Record<string, string>> = {};
|
||||||
let value = form.getFieldValue(field);
|
for (const field of changedFields) {
|
||||||
let request;
|
const value = form.getFieldValue(field);
|
||||||
if (propsMap.has(field)) {
|
if (propsMap.has(field)) {
|
||||||
let [fieldName, prop] = propsMap.get(field)!;
|
const [fieldName, prop] = propsMap.get(field)!;
|
||||||
request = props.setProp(fieldName, prop, value);
|
const fieldValue = (fields[fieldName] ?? {}) as Record<string, string>;
|
||||||
|
fields[fieldName] = fieldValue;
|
||||||
|
fieldValue[prop] = value ? JSON.parse(value) : undefined;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
request = props.setField(field, value === null ? "" : String(value));
|
fields[field] = value === null ? "" : String(value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await request;
|
await props.setConfig(fields);
|
||||||
setChangedFields(changed => {
|
setChangedFields(new Set());
|
||||||
let newChanged = new Set(changed);
|
} catch (err: any) {
|
||||||
newChanged.delete(field);
|
notifyErrorHandler(err.message);
|
||||||
return newChanged;
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (err: any) {
|
|
||||||
setErrorFields(errors => {
|
|
||||||
let newErrors = new Map(errors);
|
|
||||||
newErrors.set(field, err.message);
|
|
||||||
return newErrors;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await updateConfig();
|
await updateConfig();
|
||||||
@@ -325,26 +318,23 @@ function computeTreeData(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!def.validator) { return; }
|
if (!def.validator) { return; }
|
||||||
|
config.stageProp(fieldName, prop, fieldValue);
|
||||||
// TODO this needs to use stage and stageProp to avoid deadlocks
|
|
||||||
const staging = config.staging;
|
|
||||||
staging.setProp(fieldName, prop, fieldValue);
|
|
||||||
|
|
||||||
for (const dependsOn of def.dependsOn ?? []) {
|
for (const dependsOn of def.dependsOn ?? []) {
|
||||||
const dependsOnDef = ConfigClass.fieldDefinitions[dependsOn as any] as typeof def;
|
const dependsOnDef = ConfigClass.fieldDefinitions[dependsOn as any] as typeof def;
|
||||||
try {
|
try {
|
||||||
if (propsMap.has(dependsOn)) {
|
if (propsMap.has(dependsOn)) {
|
||||||
const [name, otherProp] = propsMap.get(dependsOn)!;
|
const [name, otherProp] = propsMap.get(dependsOn)!;
|
||||||
staging.setProp(name, otherProp, JSON.parse(getFieldValue(dependsOn)));
|
config.stageProp(name, otherProp, JSON.parse(getFieldValue(dependsOn)));
|
||||||
} else if (dependsOnDef.type !== "object") {
|
} else if (dependsOnDef.type !== "object") {
|
||||||
staging.set(dependsOn, getFieldValue(dependsOn));
|
config.stage(dependsOn, getFieldValue(dependsOn));
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(`Failed to fetch field: ${dependsOn}`);
|
throw new Error(`Failed to fetch field: ${dependsOn}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def.validator(fieldValue, staging);
|
def.validator(fieldValue, config.staging);
|
||||||
}})]}
|
}})]}
|
||||||
>
|
>
|
||||||
<Input className="json-input" disabled={!canWrite} />
|
<Input className="json-input" disabled={!canWrite} />
|
||||||
@@ -418,26 +408,23 @@ function computeTreeData(
|
|||||||
rules={[({getFieldValue}) => ({
|
rules={[({getFieldValue}) => ({
|
||||||
validator: async (_, fieldValue) => {
|
validator: async (_, fieldValue) => {
|
||||||
if (!def.validator) { return; }
|
if (!def.validator) { return; }
|
||||||
|
config.stage(fieldName, fieldValue);
|
||||||
// TODO this needs to use stage and stageProp to avoid deadlocks
|
|
||||||
const staging = config.staging;
|
|
||||||
staging.set(fieldName, fieldValue);
|
|
||||||
|
|
||||||
for (const dependsOn of def.dependsOn ?? []) {
|
for (const dependsOn of def.dependsOn ?? []) {
|
||||||
const dependsOnDef = ConfigClass.fieldDefinitions[dependsOn as any] as typeof def;
|
const dependsOnDef = ConfigClass.fieldDefinitions[dependsOn as any] as typeof def;
|
||||||
try {
|
try {
|
||||||
if (propsMap.has(dependsOn)) {
|
if (propsMap.has(dependsOn)) {
|
||||||
const [name, prop] = propsMap.get(dependsOn)!;
|
const [name, prop] = propsMap.get(dependsOn)!;
|
||||||
staging.setProp(name, prop, JSON.parse(getFieldValue(dependsOn)));
|
config.stageProp(name, prop, JSON.parse(getFieldValue(dependsOn)));
|
||||||
} else if (dependsOnDef.type !== "object") {
|
} else if (dependsOnDef.type !== "object") {
|
||||||
staging.set(dependsOn, getFieldValue(dependsOn));
|
config.stage(dependsOn, getFieldValue(dependsOn));
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(`Failed to fetch field: ${dependsOn}`);
|
throw new Error(`Failed to fetch field: ${dependsOn}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def.validator(fieldValue, staging);
|
def.validator(fieldValue, config.staging);
|
||||||
},
|
},
|
||||||
})]}
|
})]}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -13,30 +13,13 @@ export default function ControllerConfigTree() {
|
|||||||
return await control.send(new lib.ControllerConfigGetRequest());
|
return await control.send(new lib.ControllerConfigGetRequest());
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setField(field: string, value: any) {
|
async function setConfig(fields: Record<string, string | Record<string, string>>) {
|
||||||
await control.send(new lib.ControllerConfigSetFieldRequest(
|
await control.send(new lib.ControllerConfigSetRequest(fields));
|
||||||
field,
|
|
||||||
String(value),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function setProp(field: string, prop: string, value: any) {
|
|
||||||
if (value) {
|
|
||||||
try {
|
|
||||||
value = JSON.parse(value);
|
|
||||||
} catch (err) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
value = undefined;
|
|
||||||
}
|
|
||||||
await control.send(new lib.ControllerConfigSetPropRequest(field, prop, value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return <BaseConfigTree
|
return <BaseConfigTree
|
||||||
ConfigClass={lib.ControllerConfig}
|
ConfigClass={lib.ControllerConfig}
|
||||||
retrieveConfig={retrieveConfig}
|
retrieveConfig={retrieveConfig}
|
||||||
setField={setField}
|
setConfig={setConfig}
|
||||||
setProp={setProp}
|
|
||||||
/>;
|
/>;
|
||||||
}
|
}
|
||||||
@@ -13,28 +13,14 @@ export default function HostConfigTree(props: { id: number, available: boolean }
|
|||||||
return await control.sendTo({ hostId: props.id }, new lib.HostConfigGetRequest());
|
return await control.sendTo({ hostId: props.id }, new lib.HostConfigGetRequest());
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setField(field: string, value: any) {
|
async function setConfig(fields: Record<string, string | Record<string, string>>) {
|
||||||
await control.sendTo({ hostId: props.id }, new lib.HostConfigSetFieldRequest(field, value));
|
await control.send(new lib.HostConfigSetRequest(fields));
|
||||||
}
|
|
||||||
|
|
||||||
async function setProp(field: string, prop: string, value: any) {
|
|
||||||
if (value) {
|
|
||||||
try {
|
|
||||||
value = JSON.parse(value);
|
|
||||||
} catch (err) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
value = undefined;
|
|
||||||
}
|
|
||||||
await control.sendTo({ hostId: props.id }, new lib.HostConfigSetPropRequest(field, prop, value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return <BaseConfigTree
|
return <BaseConfigTree
|
||||||
ConfigClass={lib.HostConfig}
|
ConfigClass={lib.HostConfig}
|
||||||
retrieveConfig={retrieveConfig}
|
retrieveConfig={retrieveConfig}
|
||||||
setField={setField}
|
setConfig={setConfig}
|
||||||
setProp={setProp}
|
|
||||||
id={props.id}
|
id={props.id}
|
||||||
available={props.available}
|
available={props.available}
|
||||||
/>;
|
/>;
|
||||||
|
|||||||
@@ -17,28 +17,14 @@ export default function InstanceConfigTree(props: InstanceConfigTreeProps) {
|
|||||||
return await control.send(new lib.InstanceConfigGetRequest(props.id));
|
return await control.send(new lib.InstanceConfigGetRequest(props.id));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setField(field: string, value: any) {
|
async function setConfig(fields: Record<string, string | Record<string, string>>) {
|
||||||
await control.send(new lib.InstanceConfigSetFieldRequest(props.id, field, value));
|
await control.send(new lib.InstanceConfigSetRequest(props.id, fields));
|
||||||
}
|
|
||||||
|
|
||||||
async function setProp(field: string, prop: string, value: any) {
|
|
||||||
if (value) {
|
|
||||||
try {
|
|
||||||
value = JSON.parse(value);
|
|
||||||
} catch (err) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
value = undefined;
|
|
||||||
}
|
|
||||||
await control.send(new lib.InstanceConfigSetPropRequest(props.id, field, prop, value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return <BaseConfigTree
|
return <BaseConfigTree
|
||||||
ConfigClass={lib.InstanceConfig}
|
ConfigClass={lib.InstanceConfig}
|
||||||
retrieveConfig={retrieveConfig}
|
retrieveConfig={retrieveConfig}
|
||||||
setField={setField}
|
setConfig={setConfig}
|
||||||
setProp={setProp}
|
|
||||||
id={props.id}
|
id={props.id}
|
||||||
/>;
|
/>;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user