mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-24 08:23:12 +09:00
Use link and disconnect icons for template inheritance
Overridden values show a disconnect icon. Clicking it stages a link, shown with a link icon, which is applied together with the other changes instead of right away. Part of #200 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b584991f5c
commit
cd04995b65
1 file changed
+53
-45
@@ -1,8 +1,9 @@
|
|||||||
import React, { useContext, useEffect, useState } from "react";
|
import React, { useContext, useEffect, useState } from "react";
|
||||||
import {
|
import {
|
||||||
Button, Card, Checkbox, Form, FormInstance, Input, InputNumber, Popconfirm, Select, Space, Spin, Tooltip, Tree,
|
Button, Card, Checkbox, Form, FormInstance, Input, InputNumber, Select, Space, Spin, Tooltip, Tree, Typography,
|
||||||
Typography,
|
|
||||||
} from "antd";
|
} from "antd";
|
||||||
|
import DisconnectOutlined from "@ant-design/icons/DisconnectOutlined";
|
||||||
|
import LinkOutlined from "@ant-design/icons/LinkOutlined";
|
||||||
import LockOutlined from "@ant-design/icons/LockOutlined";
|
import LockOutlined from "@ant-design/icons/LockOutlined";
|
||||||
import ReloadOutlined from "@ant-design/icons/ReloadOutlined";
|
import ReloadOutlined from "@ant-design/icons/ReloadOutlined";
|
||||||
import UnlockOutlined from "@ant-design/icons/UnlockOutlined";
|
import UnlockOutlined from "@ant-design/icons/UnlockOutlined";
|
||||||
@@ -76,11 +77,14 @@ export type ConfigInheritance = {
|
|||||||
notice?: (config: lib.Config<any>) => React.ReactNode;
|
notice?: (config: lib.Config<any>) => React.ReactNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type Linking = Map<string, [field: string, prop?: string]>;
|
||||||
|
|
||||||
type LockState = {
|
type LockState = {
|
||||||
inheritance?: ConfigInheritance;
|
inheritance?: ConfigInheritance;
|
||||||
unlocked: Set<string>;
|
unlocked: Set<string>;
|
||||||
setUnlocked: (unlocked: Set<string>) => void;
|
setUnlocked: (unlocked: Set<string>) => void;
|
||||||
inherit: (field: string, prop?: string) => void;
|
linking: Linking;
|
||||||
|
setLinking: (linking: Linking) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
type BaseConfigTreeProps = {
|
type BaseConfigTreeProps = {
|
||||||
@@ -99,6 +103,7 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
|||||||
let [errorFields, setErrorFields] = useState(new Map<string, string>());
|
let [errorFields, setErrorFields] = useState(new Map<string, string>());
|
||||||
let [applying, setApplying] = useState(false);
|
let [applying, setApplying] = useState(false);
|
||||||
let [unlocked, setUnlocked] = useState<Set<string>>(new Set());
|
let [unlocked, setUnlocked] = useState<Set<string>>(new Set());
|
||||||
|
let [linking, setLinking] = useState<Linking>(new Map());
|
||||||
const available = props.available ?? true;
|
const available = props.available ?? true;
|
||||||
const control = useContext(ControlContext);
|
const control = useContext(ControlContext);
|
||||||
|
|
||||||
@@ -156,15 +161,16 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function inherit(field: string, prop?: string) {
|
async function applyChanges(fields: Record<string, string | Record<string, string>>) {
|
||||||
props.inheritance!.inherit(field, prop).then(async () => {
|
if (Object.keys(fields).length) {
|
||||||
const newConfig = await updateConfig();
|
await props.setConfig(fields);
|
||||||
const values = getInitialValues(newConfig, props);
|
}
|
||||||
const keys = Object.keys(values).filter(key => key === field || key.startsWith(`${field}.`));
|
for (const [field, prop] of linking.values()) {
|
||||||
form.resetFields(keys);
|
await props.inheritance!.inherit(field, prop);
|
||||||
form.setFieldsValue(Object.fromEntries(keys.map(key => [key, values[key]])));
|
}
|
||||||
setChangedFields(fields => new Set([...fields].filter(key => !keys.includes(key))));
|
setChangedFields(new Set());
|
||||||
}).catch(err => notify(err.message, "error"));
|
setUnlocked(new Set());
|
||||||
|
setLinking(new Map());
|
||||||
}
|
}
|
||||||
|
|
||||||
const [propsMap, treeData] = computeTreeData(
|
const [propsMap, treeData] = computeTreeData(
|
||||||
@@ -176,7 +182,7 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
|||||||
setConfig,
|
setConfig,
|
||||||
props.ConfigClass,
|
props.ConfigClass,
|
||||||
onValuesChange,
|
onValuesChange,
|
||||||
{ inheritance: props.inheritance, unlocked, setUnlocked, inherit },
|
{ inheritance: props.inheritance, unlocked, setUnlocked, linking, setLinking },
|
||||||
);
|
);
|
||||||
|
|
||||||
return <>
|
return <>
|
||||||
@@ -213,9 +219,7 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
|||||||
|
|
||||||
let applied = false;
|
let applied = false;
|
||||||
try {
|
try {
|
||||||
await props.setConfig(fields);
|
await applyChanges(fields);
|
||||||
setChangedFields(new Set());
|
|
||||||
setUnlocked(new Set());
|
|
||||||
applied = true;
|
applied = true;
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
notify(err.message, "error");
|
notify(err.message, "error");
|
||||||
@@ -223,7 +227,7 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
|||||||
|
|
||||||
const newConfig = await updateConfig();
|
const newConfig = await updateConfig();
|
||||||
if (applied) {
|
if (applied) {
|
||||||
// Linking a template can change other values
|
// Linking can change other values
|
||||||
form.setFieldsValue(getInitialValues(newConfig, props));
|
form.setFieldsValue(getInitialValues(newConfig, props));
|
||||||
}
|
}
|
||||||
setApplying(false);
|
setApplying(false);
|
||||||
@@ -232,7 +236,7 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
|||||||
props.onApply();
|
props.onApply();
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
disabled={changedFields.size === 0}
|
disabled={changedFields.size === 0 && linking.size === 0}
|
||||||
>
|
>
|
||||||
Apply
|
Apply
|
||||||
</Button>
|
</Button>
|
||||||
@@ -242,6 +246,7 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
|
|||||||
form.resetFields();
|
form.resetFields();
|
||||||
setChangedFields(new Set());
|
setChangedFields(new Set());
|
||||||
setUnlocked(new Set());
|
setUnlocked(new Set());
|
||||||
|
setLinking(new Map());
|
||||||
setErrorFields(new Map());
|
setErrorFields(new Map());
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -297,42 +302,45 @@ function computeTreeData(
|
|||||||
};
|
};
|
||||||
|
|
||||||
function lockButton(key: string, fieldName: string, prop?: string) {
|
function lockButton(key: string, fieldName: string, prop?: string) {
|
||||||
const { inheritance, unlocked, setUnlocked } = lock;
|
const { inheritance, unlocked, setUnlocked, linking, setLinking } = lock;
|
||||||
if (!inheritance?.canInherit(config, fieldName) || !config.canAccess(fieldName, lib.ConfigAccess.write)) {
|
if (!inheritance?.canInherit(config, fieldName) || !config.canAccess(fieldName, lib.ConfigAccess.write)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
const button = (title: string, icon: React.ReactNode, label: string, onClick: () => void) => <Tooltip
|
||||||
|
title={title}
|
||||||
|
>
|
||||||
|
<Button type="text" size="small" icon={icon} aria-label={`${label} ${key}`} onClick={onClick} />
|
||||||
|
</Tooltip>;
|
||||||
|
const revert = () => {
|
||||||
|
form.setFieldValue(key, initialValues[key]);
|
||||||
|
onValuesChange({ [key]: initialValues[key] });
|
||||||
|
};
|
||||||
if (inheritance.isInherited(config, fieldName, prop)) {
|
if (inheritance.isInherited(config, fieldName, prop)) {
|
||||||
if (!unlocked.has(key)) {
|
if (!unlocked.has(key)) {
|
||||||
return <Tooltip title="Inherited from the template. Click to unlock and override it.">
|
return button(
|
||||||
<Button
|
"Linked to the template. Click to unlock and override it.", <LockOutlined />, "Unlock",
|
||||||
type="text" size="small" icon={<LockOutlined />} aria-label={`Unlock ${key}`}
|
() => setUnlocked(new Set([...unlocked, key])),
|
||||||
onClick={() => setUnlocked(new Set([...unlocked, key]))}
|
);
|
||||||
/>
|
|
||||||
</Tooltip>;
|
|
||||||
}
|
}
|
||||||
return <Tooltip title="Unlocked, applying a change overrides the template. Click to lock it again.">
|
return button(
|
||||||
<Button
|
"Unlocked, applying a change unlinks it from the template. Click to lock it again.",
|
||||||
type="text" size="small" icon={<UnlockOutlined />} aria-label={`Lock ${key}`}
|
<UnlockOutlined />, "Lock",
|
||||||
onClick={() => {
|
() => { setUnlocked(new Set([...unlocked].filter(k => k !== key))); revert(); },
|
||||||
setUnlocked(new Set([...unlocked].filter(k => k !== key)));
|
);
|
||||||
form.setFieldValue(key, initialValues[key]);
|
|
||||||
onValuesChange({ [key]: initialValues[key] });
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</Tooltip>;
|
|
||||||
}
|
}
|
||||||
return <Popconfirm
|
if (!linking.has(key)) {
|
||||||
title="Discard this value and inherit it from the template?"
|
return button(
|
||||||
okText="Inherit"
|
"Not linked to the template. Click to link it again when applied.", <DisconnectOutlined />, "Link",
|
||||||
onConfirm={() => lock.inherit(fieldName, prop)}
|
() => { setLinking(new Map([...linking, [key, [fieldName, prop]]])); revert(); },
|
||||||
>
|
);
|
||||||
<Tooltip title="Overrides the template. Click to inherit it again.">
|
}
|
||||||
<Button type="text" size="small" icon={<UnlockOutlined />} aria-label={`Inherit ${key}`} danger />
|
return button(
|
||||||
</Tooltip>
|
"Linked to the template when applied. Click to keep this value instead.", <LinkOutlined />, "Unlink",
|
||||||
</Popconfirm>;
|
() => setLinking(new Map([...linking].filter(([k]) => k !== key))),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
function isReadonly(canWrite: boolean, key: string, fieldName: string, prop?: string) {
|
function isReadonly(canWrite: boolean, key: string, fieldName: string, prop?: string) {
|
||||||
return !canWrite || Boolean(lock.inheritance?.canInherit(config, fieldName))
|
return !canWrite || lock.linking.has(key) || Boolean(lock.inheritance?.canInherit(config, fieldName))
|
||||||
&& lock.inheritance!.isInherited(config, fieldName, prop)
|
&& lock.inheritance!.isInherited(config, fieldName, prop)
|
||||||
&& !lock.unlocked.has(key);
|
&& !lock.unlocked.has(key);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user