mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-17 22:33:13 +09:00
Rework features into a Feature class with per field events
Review points from Cooldude: - Feature.register(name[, config]) is required again and returns a feature object. Each feature has its config, its fields, and its overrides, so one registry holds all the data. Only the overrides are stored. On load each feature points at its own stored table again. - The methods are feature:guard(lib), feature:update_config(enabled, values), and feature:is_enabled(). Feature.get(name) looks up a feature, and commands flag the feature object. - update_config raises on_config_updated once per changed value, with the feature name, path, old value, and new value. The gui refresh only reacts to the "enabled" path. - apply only sets values. on_apply is gone. Lookup sets built from lists use Feature.to_set, which is cached by the list itself. A replaced list gets a new set, so the sets are also correct after a load. Protection exposes getters for its protected names and types. Web: settings use small inputs, the same size as the config pages. Fields are shaped like clusterio config field definitions. A field with an inputComponent renders the component registered with the web interface, so the role setting now uses core's role input, and other plugin inputs can be used later. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4bc1773c82
commit
628e4df514
26 files changed
+468
-430
No files matched your search
@@ -1,7 +1,8 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Button, Checkbox, Col, Divider, Input, InputNumber, Row, Select, Space, Switch } from "antd";
|
||||
import { Button, Checkbox, Col, ConfigProvider, Divider, Input, InputNumber, Row, Select, Space, Switch } from "antd";
|
||||
|
||||
import { ControlContext, PageHeader, PageLayout, notifyErrorHandler, useAccount, useRoles } from "@clusterio/web_ui";
|
||||
import * as lib from "@clusterio/lib";
|
||||
import { ControlContext, PageHeader, PageLayout, notifyErrorHandler, useAccount } from "@clusterio/web_ui";
|
||||
|
||||
import { Feature, FeatureField, FeatureValue, features, isDefaultValue } from "../../features.js";
|
||||
import { FeatureRecord, FeatureUpdateRequest } from "../../messages.js";
|
||||
@@ -70,24 +71,30 @@ function searchFields(feature: Feature, query: string) {
|
||||
));
|
||||
}
|
||||
|
||||
function RoleSelect(props: { value: FeatureValue, disabled: boolean, onChange: (value: FeatureValue) => void }) {
|
||||
const [roles, synced] = useRoles();
|
||||
return <Select
|
||||
showSearch
|
||||
allowClear
|
||||
placeholder="No role"
|
||||
optionFilterProp="label"
|
||||
style={{ minWidth: 200 }}
|
||||
value={props.value ?? undefined}
|
||||
onChange={value => props.onChange(value ?? null)}
|
||||
options={[...roles.values()].map(role => ({ label: role.name, value: role.id }))}
|
||||
disabled={props.disabled || !synced}
|
||||
loading={!synced}
|
||||
/>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the input for a setting the same way the config pages do, input components registered
|
||||
* with the web interface take priority so plugins can provide inputs for other types
|
||||
*/
|
||||
function FieldInput(props: { field: FeatureField, value: FeatureValue, disabled: boolean, onChange: (value: FeatureValue) => void }) {
|
||||
const { field, value, disabled, onChange } = props;
|
||||
const control = useContext(ControlContext);
|
||||
const CustomInput = field.inputComponent ? control.inputComponents.get(field.inputComponent) : undefined;
|
||||
if (CustomInput) {
|
||||
const fieldDefinition = {
|
||||
type: field.type === "string_list" ? "object" : field.type,
|
||||
title: field.title,
|
||||
description: field.description,
|
||||
optional: field.optional,
|
||||
inputComponent: field.inputComponent,
|
||||
} as lib.FieldDefinition;
|
||||
return <CustomInput
|
||||
fieldDefinition={fieldDefinition}
|
||||
value={value as Exclude<FeatureValue, string[]>}
|
||||
onChange={onChange}
|
||||
disabled={disabled}
|
||||
/>;
|
||||
}
|
||||
|
||||
switch (field.type) {
|
||||
case "number":
|
||||
return <InputNumber
|
||||
@@ -95,10 +102,21 @@ function FieldInput(props: { field: FeatureField, value: FeatureValue, disabled:
|
||||
min={field.min}
|
||||
addonAfter={field.unit}
|
||||
disabled={disabled}
|
||||
onChange={next => onChange(next ?? field.default)}
|
||||
onChange={next => onChange(next ?? (field.optional ? null : field.default))}
|
||||
/>;
|
||||
case "string":
|
||||
return <Input value={value as string} disabled={disabled} onChange={e => onChange(e.target.value)} style={{ maxWidth: 400 }} />;
|
||||
if (field.enum) {
|
||||
return <Select
|
||||
showSearch
|
||||
style={{ minWidth: 175 }}
|
||||
value={value as string}
|
||||
options={field.enum.map(option => ({ label: option, value: option }))}
|
||||
allowClear={field.optional}
|
||||
disabled={disabled}
|
||||
onChange={next => onChange(next ?? null)}
|
||||
/>;
|
||||
}
|
||||
return <Input value={value as string} disabled={disabled} onChange={e => onChange(e.target.value)} style={{ width: 300 }} />;
|
||||
case "string_list":
|
||||
return <Select
|
||||
mode="tags"
|
||||
@@ -108,8 +126,6 @@ function FieldInput(props: { field: FeatureField, value: FeatureValue, disabled:
|
||||
open={false}
|
||||
style={{ width: "100%", maxWidth: 600 }}
|
||||
/>;
|
||||
case "role":
|
||||
return <RoleSelect value={value} disabled={disabled} onChange={onChange} />;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@@ -160,7 +176,7 @@ function FeatureSection(props: {
|
||||
borderRadius: 4,
|
||||
background: state.enabled !== stored.enabled ? modifiedColor : undefined,
|
||||
}}>
|
||||
<Switch checked={state.enabled} disabled={!canEdit} onChange={props.onEnabled} />
|
||||
<Switch size="default" checked={state.enabled} disabled={!canEdit} onChange={props.onEnabled} />
|
||||
</div>
|
||||
<div style={{ flexGrow: 1 }}>
|
||||
<strong>{feature.title}</strong>
|
||||
@@ -301,23 +317,26 @@ export default function FeaturesPage() {
|
||||
</div>
|
||||
</Col>
|
||||
<Col flex="auto" style={{ minWidth: 0 }}>
|
||||
<Space direction="vertical" style={{ width: "100%" }}>
|
||||
{shown.map(({ feature, fields }) => <FeatureSection
|
||||
key={feature.name}
|
||||
feature={feature}
|
||||
fields={fields}
|
||||
state={current(feature.name)}
|
||||
stored={stored[feature.name]}
|
||||
canEdit={canEdit}
|
||||
onEnabled={enabled => change(feature.name, entry => { entry.enabled = enabled; })}
|
||||
onValue={(key, value) => change(feature.name, entry => { entry.values[key] = value; })}
|
||||
onReset={() => change(feature.name, entry => {
|
||||
for (const field of feature.fields) {
|
||||
entry.values[field.name] = field.default;
|
||||
}
|
||||
})}
|
||||
/>)}
|
||||
</Space>
|
||||
{/* Small inputs match the line height of the text, same as the config pages */}
|
||||
<ConfigProvider componentSize="small">
|
||||
<Space direction="vertical" style={{ width: "100%" }}>
|
||||
{shown.map(({ feature, fields }) => <FeatureSection
|
||||
key={feature.name}
|
||||
feature={feature}
|
||||
fields={fields}
|
||||
state={current(feature.name)}
|
||||
stored={stored[feature.name]}
|
||||
canEdit={canEdit}
|
||||
onEnabled={enabled => change(feature.name, entry => { entry.enabled = enabled; })}
|
||||
onValue={(key, value) => change(feature.name, entry => { entry.values[key] = value; })}
|
||||
onReset={() => change(feature.name, entry => {
|
||||
for (const field of feature.fields) {
|
||||
entry.values[field.name] = field.default;
|
||||
}
|
||||
})}
|
||||
/>)}
|
||||
</Space>
|
||||
</ConfigProvider>
|
||||
</Col>
|
||||
</Row>
|
||||
</PageLayout>;
|
||||
|
||||
Reference in new issue
Block a user