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

@@ -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"
/>;
}