mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 08:13:12 +09:00
Rename bases to templates and add per property inheritance
Review changes for #1142: - base becomes template everywhere (is_template, template_id, --template, --is-template, --templates, InstanceDetails.isTemplate). - Object fields inherit per property. inherited_fields lists the overridden properties for object fields and the template object is merged with them. - Linking a template also inherits fields that hold the default value. The server name stays per instance. - Ports are never inherited. - InstanceDetails carries templateId. Web UI: inherited values are locked until unlocked, overridden values can be inherited again, templates are listed separately, the create modal can create templates and instances from templates, and the instance page has "Create template" which can link the source. Part of #200 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
01cf789f5d
commit
b5c372f109
21 files changed
+865
-312
No files matched your search
@@ -14,12 +14,85 @@ import { useInstanceConfig, useInstances } from "../model/instance";
|
||||
import { useSaves } from "../model/saves";
|
||||
import InstanceList from "./InstanceList";
|
||||
import { notifyErrorHandler } from "../util/notify";
|
||||
import type { Control } from "../util/websocket";
|
||||
|
||||
type SaveMode = "none" | "copy" | "save_and_copy";
|
||||
|
||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||
|
||||
function CreateInstanceButton(props: { instances: ReturnType<typeof useInstances>[0] }) {
|
||||
function ConfigSourceFields(props: {
|
||||
instances: ReturnType<typeof useInstances>[0];
|
||||
templateId?: number | null;
|
||||
source?: Readonly<lib.InstanceDetails>;
|
||||
asTemplate: boolean;
|
||||
}) {
|
||||
const templates = [...props.instances.values()].filter(i => i.isTemplate);
|
||||
const hasTemplate = typeof props.templateId === "number";
|
||||
return <>
|
||||
{(templates.length > 0 || hasTemplate) && <Tooltip
|
||||
title="Inherit config from a template, later changes to the template apply to this instance too"
|
||||
>
|
||||
<Form.Item name="template" label="Template">
|
||||
<Select
|
||||
disabled={props.source !== undefined}
|
||||
options={[{ value: null, label: "None" }, ...templates.map(i => ({ value: i.id, label: i.name }))]}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Tooltip>}
|
||||
<Tooltip title="Perform a one time copy of the config of an existing instance">
|
||||
<Form.Item name="instanceClone" label="Copy Config">
|
||||
<Select
|
||||
disabled={hasTemplate}
|
||||
options={[
|
||||
{ id: -1, name: "Default Config" },
|
||||
...[...props.instances.values()].filter(i => !i.isTemplate),
|
||||
].map(i => ({ value: i.id, label: i.name }))}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Tooltip>
|
||||
{props.asTemplate && props.source && <Form.Item name="linkSource" valuePropName="checked">
|
||||
<Checkbox>Make {props.source.name} inherit from the new template</Checkbox>
|
||||
</Form.Item>}
|
||||
</>;
|
||||
}
|
||||
|
||||
async function linkToTemplate(control: Control, source: lib.InstanceDetails | undefined, templateId: number) {
|
||||
if (source) {
|
||||
await control.send(new lib.InstanceConfigSetFieldRequest(
|
||||
source.id, "instance.template_id", String(templateId),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
function setTemplateFields(
|
||||
config: lib.InstanceConfig,
|
||||
fields: (keyof lib.InstanceConfigFields)[],
|
||||
templateId: unknown,
|
||||
asTemplate: boolean,
|
||||
) {
|
||||
if (typeof templateId === "number") {
|
||||
config.set("instance.template_id", templateId);
|
||||
fields.push("instance.template_id");
|
||||
}
|
||||
if (asTemplate) {
|
||||
config.set("instance.is_template", true);
|
||||
fields.push("instance.is_template");
|
||||
}
|
||||
}
|
||||
|
||||
type CreateInstanceInitial = {
|
||||
instanceClone?: number;
|
||||
template?: number;
|
||||
asTemplate?: boolean;
|
||||
linkSource?: boolean;
|
||||
};
|
||||
|
||||
export function CreateInstanceButton({ buttonText = "Create", buttonType = "primary", ...props }: {
|
||||
instances: ReturnType<typeof useInstances>[0];
|
||||
initial?: CreateInstanceInitial;
|
||||
buttonText?: string;
|
||||
buttonType?: "primary" | "default";
|
||||
}) {
|
||||
let control = useContext(ControlContext);
|
||||
let account = useAccount();
|
||||
let navigate = useNavigate();
|
||||
@@ -31,9 +104,11 @@ function CreateInstanceButton(props: { instances: ReturnType<typeof useInstances
|
||||
|
||||
const canAssign = account.hasPermission("core.instance.assign");
|
||||
const cloneId: number | undefined = Form.useWatch("instanceClone", form);
|
||||
const templateId: number | null | undefined = Form.useWatch("template", form);
|
||||
const asTemplate = Boolean(Form.useWatch("asTemplate", form));
|
||||
const host: number | string | undefined = Form.useWatch("host", form);
|
||||
const saveMode: SaveMode = Form.useWatch("saveMode", form) ?? "none";
|
||||
const hostId = typeof host === "number" ? host : undefined;
|
||||
const hostId = typeof host === "number" && !asTemplate ? host : undefined;
|
||||
const source = cloneId !== undefined && cloneId >= 0 ? props.instances.get(cloneId) : undefined;
|
||||
const sourceRunning = source?.status === "running";
|
||||
const activeSave = source ? [...saves.values()].find(
|
||||
@@ -42,6 +117,7 @@ function CreateInstanceButton(props: { instances: ReturnType<typeof useInstances
|
||||
const sourceConfig = useInstanceConfig(source?.id);
|
||||
const sourceSettings = sourceConfig?.["factorio.settings"] as Record<string, unknown> | undefined;
|
||||
const sourceServerName = typeof sourceSettings?.name === "string" ? sourceSettings.name : undefined;
|
||||
const copySettings = sourceSettings !== undefined && !asTemplate;
|
||||
|
||||
useEffect(() => {
|
||||
if (open && sourceServerName !== undefined) {
|
||||
@@ -68,7 +144,8 @@ function CreateInstanceButton(props: { instances: ReturnType<typeof useInstances
|
||||
let instanceConfig = new lib.InstanceConfig("control");
|
||||
instanceConfig.set("instance.name", values.instanceName);
|
||||
const fields: (keyof lib.InstanceConfigFields)[] = ["instance.id", "instance.name"];
|
||||
if (source && sourceSettings) {
|
||||
setTemplateFields(instanceConfig, fields, values.template, asTemplate);
|
||||
if (copySettings) {
|
||||
if (!values.serverName) {
|
||||
form.setFields([{ name: "serverName", errors: ["Server name is required"] }]);
|
||||
return;
|
||||
@@ -87,6 +164,9 @@ function CreateInstanceButton(props: { instances: ReturnType<typeof useInstances
|
||||
));
|
||||
created = true;
|
||||
|
||||
if (asTemplate && values.linkSource) {
|
||||
await linkToTemplate(control, source, instanceId);
|
||||
}
|
||||
if (hostId !== undefined) {
|
||||
await control.send(new lib.InstanceAssignRequest(instanceId, hostId));
|
||||
}
|
||||
@@ -114,13 +194,13 @@ function CreateInstanceButton(props: { instances: ReturnType<typeof useInstances
|
||||
|
||||
return <>
|
||||
<Button
|
||||
type="primary"
|
||||
type={buttonType}
|
||||
onClick={() => {
|
||||
setOpen(true);
|
||||
}}
|
||||
>Create</Button>
|
||||
>{buttonText}</Button>
|
||||
<Modal
|
||||
title="Create Instance"
|
||||
title={asTemplate ? "Create Template" : "Create Instance"}
|
||||
okText="Create"
|
||||
open={open}
|
||||
confirmLoading={creating}
|
||||
@@ -130,12 +210,20 @@ function CreateInstanceButton(props: { instances: ReturnType<typeof useInstances
|
||||
>
|
||||
<Form
|
||||
form={form}
|
||||
initialValues={{ host: "null", instanceClone: -1, saveMode: "none", startAfter: false }}
|
||||
initialValues={{
|
||||
host: "null", instanceClone: -1, saveMode: "none", startAfter: false,
|
||||
template: null, asTemplate: false, linkSource: true, ...props.initial,
|
||||
}}
|
||||
>
|
||||
<Form.Item name="instanceName" label="Name">
|
||||
<Input />
|
||||
</Form.Item>
|
||||
{canAssign && <Form.Item name="host" label="Host">
|
||||
<Tooltip title="Templates hold shared config for other instances and cannot be started">
|
||||
<Form.Item name="asTemplate" valuePropName="checked">
|
||||
<Checkbox>Create as template</Checkbox>
|
||||
</Form.Item>
|
||||
</Tooltip>
|
||||
{canAssign && !asTemplate && <Form.Item name="host" label="Host">
|
||||
<Select showSearch optionFilterProp="name">
|
||||
<Select.Option value={"null"}>
|
||||
<Typography.Text italic>Unassigned</Typography.Text>
|
||||
@@ -151,16 +239,15 @@ function CreateInstanceButton(props: { instances: ReturnType<typeof useInstances
|
||||
</Select.Option>)}
|
||||
</Select>
|
||||
</Form.Item>}
|
||||
<Tooltip title="Perform a one time copy of the config of an existing instance">
|
||||
<Form.Item name="instanceClone" label="Copy Config">
|
||||
<Select
|
||||
options={[{ id: -1, name: "Default Config" }, ...props.instances.values()]
|
||||
.map(i => ({ value: i.id, label: i.name }))
|
||||
}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Tooltip>
|
||||
{source && sourceSettings && <Tooltip title="Name of the server as shown in the in-game server browser">
|
||||
<ConfigSourceFields
|
||||
instances={props.instances}
|
||||
templateId={templateId}
|
||||
source={source}
|
||||
asTemplate={asTemplate}
|
||||
/>
|
||||
{copySettings && <Tooltip
|
||||
title="Name of the server as shown in the in-game server browser"
|
||||
>
|
||||
<Form.Item name="serverName" label="Server Name">
|
||||
<Input />
|
||||
</Form.Item>
|
||||
@@ -198,6 +285,8 @@ export default function InstancesPage() {
|
||||
let control = useContext(ControlContext);
|
||||
let account = useAccount();
|
||||
let [instances] = useInstances();
|
||||
const plainInstances = new Map([...instances].filter(([, instance]) => !instance.isTemplate));
|
||||
const templates = new Map([...instances].filter(([, instance]) => instance.isTemplate));
|
||||
|
||||
return <PageLayout nav={[{ name: "Instances" }]}>
|
||||
<PageHeader
|
||||
@@ -231,7 +320,11 @@ export default function InstancesPage() {
|
||||
</Space>}
|
||||
/>
|
||||
|
||||
<InstanceList instances={instances} />
|
||||
<InstanceList instances={plainInstances} />
|
||||
{templates.size > 0 && <>
|
||||
<Typography.Title level={5} style={{ marginTop: 16 }}>Templates</Typography.Title>
|
||||
<InstanceList instances={templates} hideAssignedHost tableNamespace="template" />
|
||||
</>}
|
||||
<PluginExtra component="InstancesPage" />
|
||||
</PageLayout>;
|
||||
}
|
||||
Reference in new issue
Block a user