import React, { useContext, useState } from "react"; import { Button, Checkbox, Col, Divider, Input, InputNumber, Row, Select, Space, Switch } from "antd"; import { ControlContext, PageHeader, PageLayout, notifyErrorHandler, useAccount, useRoles } 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) )); } function RoleSelect(props: { value: FeatureValue, disabled: boolean, onChange: (value: FeatureValue) => void }) { const [roles, synced] = useRoles(); return onChange(e.target.value)} style={{ maxWidth: 400 }} />; case "string_list": return