Add network events and switch to tree select

This commit is contained in:
Cooldude2606 committed 2026-01-24 16:27:28 +00:00
1 parent 07f501a67a
commit 6f68cf74a7
13 files changed
+184 -79

No files matched your search

+3 -1
View File
@@ -10,6 +10,7 @@ import BaseWebPlugin, { InputComponent } from "./BaseWebPlugin";
import InputRole from "./components/InputRole";
import InputModPack from "./components/InputModPack";
import InputTargetVersion from "./components/InputTargetVersion";
import InputPartialVersion from "./components/InputPartialVersion";
import { Control, ControlConnector } from "./util/websocket";
const { ConsoleTransport, WebConsoleFormat, logger } = lib;
@@ -110,9 +111,10 @@ async function loadPlugins(pluginInfos: lib.PluginWebpackEnvInfo[], control: Con
function inputComponentsFromPlugins(plugins: Map<string, BaseWebPlugin>) {
const inputComponents: Record<string, InputComponent> = {
"partial_version": InputPartialVersion,
"target_version": InputTargetVersion,
"mod_pack": InputModPack,
"role": InputRole,
"target_version": InputTargetVersion,
};
for (let [pluginName, plugin] of plugins) {
for (const [name, Component] of Object.entries(plugin.inputComponents)) {
@@ -1,47 +1,68 @@
import React from "react";
import { Select } from "antd";
import React, { useContext, useEffect, useState } from "react";
import { TreeSelect } from "antd";
import { InputComponentProps } from "../BaseWebPlugin";
import * as lib from "@clusterio/lib";
import { InputComponentProps } from "../BaseWebPlugin";
import { useAccount } from "../model/account";
import ControlContext from "./ControlContext";
// Hardcoded list for step 1
const FACTORIO_VERSIONS = [
"2.0",
"1.1",
"1.0",
"0.18",
"0.17",
"0.16",
"0.15",
"0.14",
"0.13",
] as const;
const defaultFieldDefinition: lib.FieldDefinition = { type: "string", optional: false };
const defaultFieldDefinition: lib.FieldDefinition = {
type: "string",
optional: false,
};
export default function InputPartialVersion(
props: Omit<InputComponentProps, "fieldDefinition"> & { fieldDefinition?: lib.FieldDefinition; className?: string }
export default function InputPartialVersion (
props: Omit<InputComponentProps, "fieldDefinition"> & {
fieldDefinition?: lib.FieldDefinition;
className?: string;
}
) {
const fieldDefinition = props.fieldDefinition || defaultFieldDefinition;
const [versions, setVersions] = useState<readonly lib.PartialVersion[]>(lib.ApiVersions);
const control = useContext(ControlContext);
const account = useAccount();
const options = FACTORIO_VERSIONS.map(version => ({
label: version,
value: version,
}));
useEffect(() => {
(async () => {
if (account.hasPermission("core.external.get_factorio_versions")) {
const res = await control.factorioVersions.get(5 * 60 * 1000);
setVersions(res.map(v => v.version));
}
})();
}, [control]);
return <Select
// Split the versions into groups based on major minor
const groups = new Map<lib.PartialVersion, lib.PartialVersion[]>();
for (const version of versions) {
const [major, minor] = version.split(".");
const key = `${major}.${minor}` as lib.PartialVersion;
if (!groups.has(key)) {
groups.set(key, []);
}
groups.get(key)!.push(version);
}
// Construct the tree to be disabled
const tree = [...groups.entries()].map(([majorMinor, patchVersions]) => (
{
title: majorMinor,
value: majorMinor,
key: majorMinor,
children: patchVersions.map((v) => ({
title: v,
value: v,
key: v,
})),
}
));
return <TreeSelect
showSearch
optionFilterProp="label"
style={{ minWidth: 175 }}
className={props.className}
onChange={(value) => props.onChange(value ?? null)}
value={props.value}
options={options}
treeData={tree}
allowClear={fieldDefinition.optional}
className={props.className}
disabled={props.disabled}
placeholder="Select Factorio version"
/>;
}
@@ -1,50 +1,71 @@
import React from "react";
import { Select } from "antd";
import React, { useContext, useEffect, useState } from "react";
import { TreeSelect } from "antd";
import { InputComponentProps } from "../BaseWebPlugin";
import * as lib from "@clusterio/lib";
import { InputComponentProps } from "../BaseWebPlugin";
import { useAccount } from "../model/account";
import ControlContext from "./ControlContext";
// Hardcoded list for step 1
const FACTORIO_VERSIONS = [
"2.0",
"1.1",
"1.0",
"0.18",
"0.17",
"0.16",
"0.15",
"0.14",
"0.13",
] as const;
const defaultFieldDefinition: lib.FieldDefinition = { type: "string", optional: false };
const defaultFieldDefinition: lib.FieldDefinition = {
type: "string",
optional: false,
};
export default function InputTargetVersion(
props: Omit<InputComponentProps, "fieldDefinition"> & { fieldDefinition?: lib.FieldDefinition; className?: string }
export default function InputTargetVersion (
props: Omit<InputComponentProps, "fieldDefinition"> & {
fieldDefinition?: lib.FieldDefinition;
className?: string;
}
) {
const fieldDefinition = props.fieldDefinition || defaultFieldDefinition;
const [versions, setVersions] = useState<readonly lib.TargetVersion[]>(lib.ApiVersions);
const control = useContext(ControlContext);
const account = useAccount();
const options = FACTORIO_VERSIONS.map(version => ({
label: version,
value: version,
}));
useEffect(() => {
(async () => {
if (account.hasPermission("core.external.get_factorio_versions")) {
const res = await control.factorioVersions.get(5 * 60 * 1000);
setVersions(res.map(v => v.version));
}
})();
}, [control]);
// Split the versions into groups based on major minor
const groups = new Map<lib.TargetVersion, lib.TargetVersion[]>();
for (const version of versions) {
const [major, minor] = version.split(".");
const key = `${major}.${minor}` as lib.PartialVersion;
if (!groups.has(key)) {
groups.set(key, []);
}
groups.get(key)!.push(version);
}
// Construct the tree to be disabled
const tree = [...groups.entries()].map(([majorMinor, patchVersions]) => (
{
title: majorMinor,
value: majorMinor,
key: majorMinor,
children: patchVersions.map((v) => ({
title: v,
value: v,
key: v,
})),
}
));
// Add "latest" option for TargetVersion
options.unshift({ label: "latest", value: "latest" });
tree.unshift({ title: "latest", value: "latest", key: "latest", children: [] });
return <Select
return <TreeSelect
showSearch
optionFilterProp="label"
style={{ minWidth: 175 }}
className={props.className}
onChange={(value) => props.onChange(value ?? null)}
value={props.value}
options={options}
treeData={tree}
allowClear={fieldDefinition.optional}
className={props.className}
disabled={props.disabled}
placeholder="Select Factorio version"
/>;
}
+7
View File
@@ -69,6 +69,9 @@ export class Control extends lib.Link {
users = new lib.EventSubscriber(lib.UserUpdatesEvent, this);
roles = new lib.EventSubscriber(lib.RoleUpdatesEvent, this);
/** Cache for factorio versions to avoid repeat calls to the controller */
factorioVersions = new lib.ValueCache(this.requestFactorioVersions.bind(this));
declare connector: ControlConnector;
constructor(
@@ -105,6 +108,10 @@ export class Control extends lib.Link {
this.handle(lib.DebugWsMessageEvent, this.handleDebugWsMessageEvent.bind(this));
}
requestFactorioVersions() {
return this.send(new lib.FactorioVersionsRequest());
}
async handleAccountUpdateEvent(event: lib.AccountUpdateEvent) {
this.accountRoles = event.roles??null;
this.emitAccountUpdate();