import React, { useContext, useState } from "react";
import { Button, Card, Col, Empty, Form, Input, InputNumber, Row, Space, Switch, Typography, theme } from "antd";
import { ControlContext, PageHeader, PageLayout, notifyErrorHandler, useAccount } from "@clusterio/web_ui";
import { Feature, FeatureField, FeatureValue, features } from "../../features.js";
import { FeatureRecord, FeatureUpdateRequest } from "../../messages.js";
import { useFeatures } from "../index";
function FeatureListItem(props: { feature: Feature, enabled: boolean, selected: boolean, onClick: () => void }) {
const { token } = theme.useToken();
return
{props.feature.title}
;
}
/** Returned as an element rather than a component so Form.Item can pass its value and onChange to it. */
function fieldInput(field: FeatureField, disabled: boolean) {
switch (field.type) {
case "boolean":
return ;
case "number":
return ;
case "string":
return ;
}
}
function defaultValues(feature: Feature) {
return Object.fromEntries(feature.fields.map(field => [field.name, field.default]));
}
function FeatureForm(props: { feature: Feature, record: FeatureRecord | undefined }) {
const { feature, record } = props;
const control = useContext(ControlContext);
const account = useAccount();
const [form] = Form.useForm();
const [dirty, setDirty] = useState(false);
const [saving, setSaving] = useState(false);
const canEdit = account.hasPermission("exp_scenario.config.edit");
const initialValues = {
enabled: record?.enabled ?? true,
...defaultValues(feature),
...record?.values,
};
function save() {
const { enabled, ...values } = form.getFieldsValue() as { enabled: boolean } & Record;
setSaving(true);
control.send(new FeatureUpdateRequest(feature.name, enabled, values))
.then(() => setDirty(false))
.catch(notifyErrorHandler(`Error saving ${feature.title}`))
.finally(() => setSaving(false));
}
return
{feature.title}
{feature.description}
>}
>
{feature.fields.map(field =>
{fieldInput(field, !canEdit)}
)}
{canEdit &&
}
;
}
/** Enable, disable, and configure the scenario features listed in features.ts. */
export default function FeaturesPage() {
const [records] = useFeatures();
const [selected, setSelected] = useState(features[0]?.name);
const feature = features.find(f => f.name === selected);
const record = selected ? records.get(selected) : undefined;
return
{features.map(f => setSelected(f.name)}
/>)}
{feature
// Remount on save from anywhere so the form shows the stored values
?
: }
;
}