import React, { useContext, useState } from "react"; import { Button, Checkbox, Col, ConfigProvider, Divider, Input, InputNumber, Row, Select, Space, Switch } from "antd"; 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"; import { useFeatures } from "../index"; type FeatureState = { enabled: boolean, values: Record }; type PendingChanges = Record }>; // Same colours as the role and permission group pages const borderColor = "#424242"; const modifiedColor = "#2a1912"; function sameValue(a: FeatureValue | undefined, b: FeatureValue | undefined) { if (Array.isArray(a) && Array.isArray(b)) { return a.length === b.length && a.every((item, index) => item === b[index]); } return a === b; } /** The stored state of a feature, with defaults for values which were not set. */ function storedState(feature: Feature, record: FeatureRecord | undefined): FeatureState { return { enabled: record?.enabled ?? true, values: { ...Object.fromEntries(feature.fields.map(field => [field.name, field.default])), ...record?.values, }, }; } /** Group fields by the section in their name, "turrets.enabled" is in the "turrets" section. */ function groupFields(fields: FeatureField[]) { const groups = new Map(); for (const field of fields) { const section = field.name.includes(".") ? field.name.slice(0, field.name.lastIndexOf(".")) : ""; if (!groups.has(section)) { groups.set(section, []); } groups.get(section)!.push(field); } return [...groups.entries()]; } /** Converts a section name into Title Case, same as the group titles on the role page */ function formatSectionTitle(name: string) { return name .replace(/\./g, " / ") .replace(/_/g, " ") .split(" ") .filter(Boolean) .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .join(" "); } function matches(text: string, query: string) { return text.toLowerCase().includes(query); } /** The fields of a feature which match the search, every field if the feature itself matches. */ function searchFields(feature: Feature, query: string) { if (!query || matches(feature.title, query) || matches(feature.description, query) || matches(feature.name, query)) { return feature.fields; } return feature.fields.filter(field => ( matches(field.title, query) || matches(field.description, query) || matches(field.name, query) )); } /** * 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 } onChange={onChange} disabled={disabled} />; } switch (field.type) { case "number": return onChange(next ?? (field.optional ? null : field.default))} />; case "string": if (field.enum) { return onChange(e.target.value)} style={{ width: 300 }} />; case "string_list": return