mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 15:33:12 +09:00
Add typed hook system for plugins
Replace invokeHook and the per plugin on* methods with AsyncHook collections on the controller, host, instance, clusterioctl and web control objects. Plugin entrypoints export a default function that receives a load context and attaches handlers to the hooks it needs. Class exports still load through fromContext with a deprecation warning. The create templates and docs use the entrypoint function. This is clusterio/clusterio#921 recreated on top of master after the ESM migration and the PluginDeclaration permissions change. Closes #730 Co-Authored-By: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c03ac15ab
commit
7122cbccf8
64 files changed
+1878
-1184
No files matched your search
@@ -33,11 +33,11 @@ function getInitialValues(config: lib.Config<any>, props: BaseConfigTreeProps) {
|
||||
return initialValues;
|
||||
}
|
||||
|
||||
function renderInput(inputComponents: Record<string, InputComponent>, def: lib.FieldDefinition, readonly: boolean) {
|
||||
if (def.inputComponent && Object.prototype.hasOwnProperty.call(inputComponents, def.inputComponent)) {
|
||||
function renderInput(inputComponents: Map<string, InputComponent>, def: lib.FieldDefinition, readonly: boolean) {
|
||||
if (def.inputComponent && inputComponents.has(def.inputComponent)) {
|
||||
// Field.Item will provide the value and onChange props to the component.
|
||||
type InputPartial = React.ComponentClass<Omit<InputComponentProps, "value" | "onChange">>;
|
||||
const CustomInput = inputComponents[def.inputComponent] as unknown as InputPartial;
|
||||
const CustomInput = inputComponents.get(def.inputComponent) as unknown as InputPartial;
|
||||
return <CustomInput fieldDefinition={def} disabled={readonly} />;
|
||||
}
|
||||
if (def.type === "boolean") {
|
||||
@@ -291,7 +291,7 @@ function computeTreeData(
|
||||
children: [],
|
||||
};
|
||||
|
||||
if (def.type === "object" && !Object.keys(control.inputComponents).includes(def.inputComponent!)) {
|
||||
if (def.type === "object" && !control.inputComponents.has(def.inputComponent!)) {
|
||||
let restartRequiredProps = new Set(def.restartRequiredProps || []);
|
||||
childNode.title = <Form.Item
|
||||
label={<>
|
||||
|
||||
@@ -5,6 +5,7 @@ import LockOutlined from "@ant-design/icons/LockOutlined";
|
||||
import notify from "../util/notify";
|
||||
import logo from "../images/logo.png";
|
||||
import ControlContext from "./ControlContext";
|
||||
import { PluginLoginForm } from "../BaseWebPlugin";
|
||||
|
||||
const { Paragraph } = Typography;
|
||||
|
||||
@@ -36,7 +37,7 @@ function TokenAuth(props: SetTokenProps) {
|
||||
}
|
||||
|
||||
export default function LoginForm(props: SetTokenProps) {
|
||||
let plugins = useContext(ControlContext).plugins;
|
||||
const control = useContext(ControlContext);
|
||||
const [clusterName, setClusterName] = useState<string>("Clusterio");
|
||||
|
||||
useEffect(() => {
|
||||
@@ -55,16 +56,14 @@ export default function LoginForm(props: SetTokenProps) {
|
||||
})();
|
||||
}, []);
|
||||
|
||||
let loginForms = [];
|
||||
for (let plugin of plugins.values()) {
|
||||
loginForms.push(...plugin.loginForms);
|
||||
}
|
||||
|
||||
loginForms.push({
|
||||
name: "core.token",
|
||||
title: "Token",
|
||||
Component: TokenAuth,
|
||||
});
|
||||
const loginForms: PluginLoginForm[] = [
|
||||
...control.loginForms,
|
||||
{
|
||||
name: "core.token",
|
||||
title: "Token",
|
||||
Component: TokenAuth,
|
||||
},
|
||||
];
|
||||
|
||||
return <Card style={{ maxWidth: "30em" }}>
|
||||
<Space style={{ width: "100%" }} direction="vertical" align="center">
|
||||
|
||||
@@ -1,29 +1,17 @@
|
||||
import React, { useContext } from "react";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import BaseWebPlugin from "../BaseWebPlugin";
|
||||
import { PluginExtensionSlot, ExtensionSlotProps } from "../BaseWebPlugin";
|
||||
|
||||
|
||||
type PluginExtraProps = {
|
||||
component: string;
|
||||
search?: string;
|
||||
instance?: lib.InstanceDetails;
|
||||
user?: lib.UserDetails;
|
||||
host?: lib.HostDetails;
|
||||
role?: lib.Role;
|
||||
};
|
||||
export default function PluginExtra(props: PluginExtraProps) {
|
||||
let control = useContext(ControlContext);
|
||||
|
||||
let components = [];
|
||||
for (let [name, plugin] of control.plugins) {
|
||||
if (Object.prototype.hasOwnProperty.call(plugin.componentExtra, props.component)) {
|
||||
type ComponentExtra = {[key: string]: React.ComponentType<PluginExtraProps & {plugin:BaseWebPlugin}>};
|
||||
let ComponentExtra = (plugin.componentExtra as ComponentExtra)[props.component];
|
||||
components.push(<ComponentExtra key={name} plugin={plugin} {...props}/>);
|
||||
}
|
||||
}
|
||||
return components;
|
||||
export default function PluginExtra<
|
||||
C extends PluginExtensionSlot
|
||||
> (
|
||||
props: { component: C } & ExtensionSlotProps[C]
|
||||
) {
|
||||
const control = useContext(ControlContext);
|
||||
const components = control.extensionComponents[props.component];
|
||||
return components && [...components].map(([source, Component]) => (
|
||||
<Component key={source} {...props} />
|
||||
));
|
||||
}
|
||||
@@ -26,7 +26,7 @@ export default function PluginViewPage() {
|
||||
})();
|
||||
}, []);
|
||||
|
||||
const plugin = control.plugins.get(pluginName);
|
||||
const loadedPlugin = control.loadedPlugins.get(pluginName);
|
||||
const pluginInfo = control.pluginInfos.get(pluginName);
|
||||
const pluginTitle = pluginInfo ? pluginInfo.title : pluginName;
|
||||
let nav = [{ name: "Plugins", path: "/plugins" }, { name: pluginTitle }];
|
||||
@@ -46,7 +46,7 @@ export default function PluginViewPage() {
|
||||
}
|
||||
|
||||
const pluginError = pluginMeta.web.error ?? pluginInfo?.error;
|
||||
if (!plugin) {
|
||||
if (!loadedPlugin) {
|
||||
return <PageLayout nav={nav}>
|
||||
<Descriptions bordered size="small" title={pluginTitle}>
|
||||
<Descriptions.Item label="Version">{pluginMeta.version}</Descriptions.Item>
|
||||
@@ -81,22 +81,22 @@ export default function PluginViewPage() {
|
||||
<Descriptions.Item label="Enabled on controller" span={2}>
|
||||
{pluginMeta.enabled ? "Yes" : "No"}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="Description" span={3}>{plugin.info.description}</Descriptions.Item>
|
||||
{plugin.package.homepage ? <Descriptions.Item label="Homepage" span={3}>
|
||||
<a href={plugin.package.homepage}>{plugin.package.homepage}</a>
|
||||
<Descriptions.Item label="Description" span={3}>{loadedPlugin.description}</Descriptions.Item>
|
||||
{loadedPlugin.package.homepage ? <Descriptions.Item label="Homepage" span={3}>
|
||||
<a href={loadedPlugin.package.homepage}>{loadedPlugin.package.homepage}</a>
|
||||
</Descriptions.Item> : null}
|
||||
{plugin.package.author ? <Descriptions.Item label="Author" span={3}>
|
||||
{plugin.package.author}
|
||||
{loadedPlugin.package.author ? <Descriptions.Item label="Author" span={3}>
|
||||
{loadedPlugin.package.author}
|
||||
</Descriptions.Item> : null}
|
||||
<Descriptions.Item label="License" span={3}>{plugin.package.license}</Descriptions.Item>
|
||||
<Descriptions.Item label="License" span={3}>{loadedPlugin.package.license}</Descriptions.Item>
|
||||
</Descriptions>
|
||||
{pluginMeta.version !== plugin.package.version ? <Alert
|
||||
{pluginMeta.version !== loadedPlugin.package.version ? <Alert
|
||||
style={{
|
||||
marginTop: "1em",
|
||||
}}
|
||||
message="Version missmatch detected"
|
||||
description={
|
||||
`The version of the web interface module for this plugin (${plugin.package.version}) `+
|
||||
`The version of the web interface module for this plugin (${loadedPlugin.package.version}) `+
|
||||
`does not match the version running on the controller (${pluginMeta.version}), `+
|
||||
"spurious errors may occur. This usually happens when the plugin is updated but the "+
|
||||
"controller has not been restarted yet, but may also be due to an outdated build."
|
||||
|
||||
@@ -47,17 +47,15 @@ export default function PluginsPage() {
|
||||
|
||||
let tableContents: PluginRow[] = [];
|
||||
for (let meta of pluginList) {
|
||||
if (control.plugins.has(meta.name)) {
|
||||
let plugin = control.plugins.get(meta.name)!;
|
||||
const info = control.loadedPlugins.get(meta.name);
|
||||
if (info) {
|
||||
tableContents.push({
|
||||
meta,
|
||||
info: plugin.info,
|
||||
package: plugin.package,
|
||||
meta, info,
|
||||
package: info.package,
|
||||
});
|
||||
} else {
|
||||
tableContents.push({
|
||||
meta,
|
||||
info: control.pluginInfos.get(meta.name),
|
||||
meta, info: control.pluginInfos.get(meta.name),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import ChangeLogModal from "./ChangeLogModal";
|
||||
import AboutModal from "./AboutModal";
|
||||
import Link from "./Link";
|
||||
|
||||
import { pages } from "../pages";
|
||||
import { saveJson } from "../util/save_file";
|
||||
import { useAccount } from "../model/account";
|
||||
import { DraggingContext } from "../model/is_dragging";
|
||||
@@ -60,7 +59,6 @@ export default function SiteLayout() {
|
||||
const [drawerOpen, setDrawerOpen] = useState(false);
|
||||
const [collapsed, setCollapsed] = useState(false);
|
||||
let account = useAccount();
|
||||
let plugins = useContext(ControlContext).plugins;
|
||||
const control = useContext(ControlContext);
|
||||
const [dragging, setDragging] = useState(0);
|
||||
const [aboutOpen, setAboutOpen] = useState(false);
|
||||
@@ -110,13 +108,9 @@ export default function SiteLayout() {
|
||||
],
|
||||
};
|
||||
|
||||
let combinedPages = [...pages];
|
||||
for (let plugin of plugins.values()) {
|
||||
combinedPages.push(...plugin.pages);
|
||||
}
|
||||
|
||||
let menuItems: MenuItem[] = [];
|
||||
let menuGroups = new Map();
|
||||
const combinedPages = [...control.pages];
|
||||
for (let { sidebarName, sidebarGroup, permission, path } of combinedPages) {
|
||||
if (
|
||||
!sidebarName || permission && (
|
||||
|
||||
Reference in new issue
Block a user