mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 00:13:13 +09:00
Merge pull request #1257 from bbassie/feature/plugin-dropdown-actions
Let plugins add actions to page dropdowns
This commit is contained in:
11 files changed
+185
-12
No files matched your search
@@ -140,6 +140,46 @@ export type ExtensionComponent<K extends PluginExtensionSlot> = React.ComponentT
|
|||||||
|
|
||||||
export type ExtensionComponents = { [K in PluginExtensionSlot]: Map<string, ExtensionComponent<K>> };
|
export type ExtensionComponents = { [K in PluginExtensionSlot]: Map<string, ExtensionComponent<K>> };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Action added by a plugin to the actions dropdown of a page.
|
||||||
|
*/
|
||||||
|
export type PluginPageAction = {
|
||||||
|
/** Unique within the plugin, the web UI prefixes it with the plugin name. */
|
||||||
|
key: string;
|
||||||
|
label: React.ReactNode;
|
||||||
|
icon?: React.ReactNode;
|
||||||
|
disabled?: boolean;
|
||||||
|
danger?: boolean;
|
||||||
|
onClick?: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PageActionsSlotProps = {
|
||||||
|
/** Controller control button on the controller page. */
|
||||||
|
ControllerPage: Record<never, never>;
|
||||||
|
/** Host control button on each host page. */
|
||||||
|
HostViewPage: {
|
||||||
|
host: lib.HostDetails;
|
||||||
|
};
|
||||||
|
/** More dropdown on each instance and template page. */
|
||||||
|
InstanceViewPage: {
|
||||||
|
instance: lib.InstanceDetails;
|
||||||
|
};
|
||||||
|
/** More dropdown on each user page. */
|
||||||
|
UserViewPage: {
|
||||||
|
user: lib.UserDetails;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PageActionsSlot = keyof PageActionsSlotProps;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the actions to add to a page. It is called during render like a
|
||||||
|
* React hook, so it may call hooks such as useAccount or useState.
|
||||||
|
*/
|
||||||
|
export type PageActionsHook<K extends PageActionsSlot> = (props: PageActionsSlotProps[K]) => PluginPageAction[];
|
||||||
|
|
||||||
|
export type PageActions = { [K in PageActionsSlot]: Map<string, PageActionsHook<K>> };
|
||||||
|
|
||||||
export type WebPluginContext = lib.PluginLoadContext<{
|
export type WebPluginContext = lib.PluginLoadContext<{
|
||||||
control: Control;
|
control: Control;
|
||||||
container: any;
|
container: any;
|
||||||
@@ -156,6 +196,7 @@ export class WebHooks extends lib.AsyncHookCollection {
|
|||||||
this.extensionComponents = this.newHook();
|
this.extensionComponents = this.newHook();
|
||||||
this.inputComponents = this.newHook();
|
this.inputComponents = this.newHook();
|
||||||
this.loginForms = this.newHook();
|
this.loginForms = this.newHook();
|
||||||
|
this.pageActions = this.newHook();
|
||||||
this.pages = this.newHook();
|
this.pages = this.newHook();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -225,6 +266,28 @@ export class WebHooks extends lib.AsyncHookCollection {
|
|||||||
*/
|
*/
|
||||||
readonly loginForms: lib.AsyncHook<[], PluginLoginForm[]>;
|
readonly loginForms: lib.AsyncHook<[], PluginLoginForm[]>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Additional actions for the actions dropdown on resource pages.
|
||||||
|
*
|
||||||
|
* Each plugin may return a partial mapping of page names to
|
||||||
|
* functions returning the actions for that page. Plugin actions are
|
||||||
|
* placed after the built in ones and before stop and delete.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* control.hooks.pageActions.attach("my_plugin", () => ({
|
||||||
|
* UserViewPage: ({ user }) => [{
|
||||||
|
* key: "warn",
|
||||||
|
* label: "Warn",
|
||||||
|
* onClick: () => control.send(new WarnUserRequest(user.name)),
|
||||||
|
* }],
|
||||||
|
* }));
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
readonly pageActions: lib.AsyncHook<[], Partial<{
|
||||||
|
[K in PageActionsSlot]: PageActionsHook<K>;
|
||||||
|
}>>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Additional pages accessible on the web ui.
|
* Additional pages accessible on the web ui.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import { InputInstanceTemplate } from "./components/InputInstance";
|
|||||||
import { InputTargetVersion, InputPartialVersion, InputFullVersion } from "./components/InputVersion";
|
import { InputTargetVersion, InputPartialVersion, InputFullVersion } from "./components/InputVersion";
|
||||||
import { Control, ControlConnector } from "./util/websocket";
|
import { Control, ControlConnector } from "./util/websocket";
|
||||||
import { loadedPluginSetKey } from "./util/pluginSet";
|
import { loadedPluginSetKey } from "./util/pluginSet";
|
||||||
|
import { pageActionsFromHooks } from "./util/plugin_actions";
|
||||||
import * as WebPlugin from "./BaseWebPlugin";
|
import * as WebPlugin from "./BaseWebPlugin";
|
||||||
import { pages } from "./pages";
|
import { pages } from "./pages";
|
||||||
|
|
||||||
@@ -209,6 +210,7 @@ export default async function bootstrap() {
|
|||||||
|
|
||||||
control.inputComponents = await inputComponentsFromHooks(control);
|
control.inputComponents = await inputComponentsFromHooks(control);
|
||||||
control.extensionComponents = await extensionComponentsFromHooks(control);
|
control.extensionComponents = await extensionComponentsFromHooks(control);
|
||||||
|
control.pageActions = await pageActionsFromHooks(control.hooks);
|
||||||
control.loginForms = await loginFormsFromHooks(control);
|
control.loginForms = await loginFormsFromHooks(control);
|
||||||
control.pages = await pagesFromHooks(control);
|
control.pages = await pagesFromHooks(control);
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import PageLayout from "./PageLayout";
|
|||||||
import notify, { notifyErrorHandler } from "../util/notify";
|
import notify, { notifyErrorHandler } from "../util/notify";
|
||||||
import webUiPackage from "../../package.json";
|
import webUiPackage from "../../package.json";
|
||||||
import { hasNpmButtonPermission, NpmButton } from "./NpmButton";
|
import { hasNpmButtonPermission, NpmButton } from "./NpmButton";
|
||||||
|
import usePluginActions from "../util/usePluginActions";
|
||||||
import VariableDropdownButton, { VariableDropdownButtonProps } from "./VariableDropdownButton";
|
import VariableDropdownButton, { VariableDropdownButtonProps } from "./VariableDropdownButton";
|
||||||
|
|
||||||
const { Title } = Typography;
|
const { Title } = Typography;
|
||||||
@@ -30,6 +31,7 @@ function ControllerControlButton({ canRestart, restartRequired }: ControllerCont
|
|||||||
const control = useContext(ControlContext);
|
const control = useContext(ControlContext);
|
||||||
const [modal, modalContext] = Modal.useModal();
|
const [modal, modalContext] = Modal.useModal();
|
||||||
const actions: VariableDropdownButtonProps["actions"] = [];
|
const actions: VariableDropdownButtonProps["actions"] = [];
|
||||||
|
const pluginActions = usePluginActions("ControllerPage", {});
|
||||||
|
|
||||||
if (account.hasPermission("core.controller.restart") && canRestart) {
|
if (account.hasPermission("core.controller.restart") && canRestart) {
|
||||||
actions.push({
|
actions.push({
|
||||||
@@ -71,6 +73,8 @@ function ControllerControlButton({ canRestart, restartRequired }: ControllerCont
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
actions.push(...pluginActions);
|
||||||
|
|
||||||
if (account.hasPermission("core.controller.stop")) {
|
if (account.hasPermission("core.controller.stop")) {
|
||||||
actions.push({
|
actions.push({
|
||||||
key: "stop",
|
key: "stop",
|
||||||
|
|||||||
@@ -25,22 +25,25 @@ import {
|
|||||||
import { useSystems } from "../model/system";
|
import { useSystems } from "../model/system";
|
||||||
import useResourceState from "../util/useResourceState";
|
import useResourceState from "../util/useResourceState";
|
||||||
import { hasNpmButtonPermission, NpmButton } from "./NpmButton";
|
import { hasNpmButtonPermission, NpmButton } from "./NpmButton";
|
||||||
|
import usePluginActions from "../util/usePluginActions";
|
||||||
import VariableDropdownButton, { VariableDropdownButtonProps } from "./VariableDropdownButton";
|
import VariableDropdownButton, { VariableDropdownButtonProps } from "./VariableDropdownButton";
|
||||||
|
|
||||||
const { Title } = Typography;
|
const { Title } = Typography;
|
||||||
|
|
||||||
type HostControlButtonProps = {
|
type HostControlButtonProps = {
|
||||||
hostId: number,
|
host: lib.HostDetails,
|
||||||
instances: ReadonlyMap<number, lib.InstanceDetails>,
|
instances: ReadonlyMap<number, lib.InstanceDetails>,
|
||||||
canRestart?: boolean,
|
canRestart?: boolean,
|
||||||
restartRequired?: boolean,
|
restartRequired?: boolean,
|
||||||
};
|
};
|
||||||
|
|
||||||
function HostControlButton({ hostId, instances, canRestart, restartRequired }: HostControlButtonProps) {
|
function HostControlButton({ host, instances, canRestart, restartRequired }: HostControlButtonProps) {
|
||||||
const account = useAccount();
|
const account = useAccount();
|
||||||
const control = useContext(ControlContext);
|
const control = useContext(ControlContext);
|
||||||
const [modal, modalContext] = Modal.useModal();
|
const [modal, modalContext] = Modal.useModal();
|
||||||
|
const hostId = host.id;
|
||||||
const actions: VariableDropdownButtonProps["actions"] = [];
|
const actions: VariableDropdownButtonProps["actions"] = [];
|
||||||
|
const pluginActions = usePluginActions("HostViewPage", { host });
|
||||||
|
|
||||||
if (account.hasPermission("core.host.restart") && canRestart) {
|
if (account.hasPermission("core.host.restart") && canRestart) {
|
||||||
actions.push({
|
actions.push({
|
||||||
@@ -93,6 +96,8 @@ function HostControlButton({ hostId, instances, canRestart, restartRequired }: H
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
actions.push(...pluginActions);
|
||||||
|
|
||||||
if (account.hasPermission("core.host.stop")) {
|
if (account.hasPermission("core.host.stop")) {
|
||||||
actions.push({
|
actions.push({
|
||||||
key: "stop",
|
key: "stop",
|
||||||
@@ -174,7 +179,7 @@ export default function HostViewPage() {
|
|||||||
|
|
||||||
let hostButtons = <Space wrap>
|
let hostButtons = <Space wrap>
|
||||||
<HostControlButton
|
<HostControlButton
|
||||||
hostId={hostId}
|
host={host}
|
||||||
instances={hostInstances}
|
instances={hostInstances}
|
||||||
canRestart={system?.canRestart}
|
canRestart={system?.canRestart}
|
||||||
restartRequired={system?.restartRequired}
|
restartRequired={system?.restartRequired}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ import { useDefaultModPack, useModPacks } from "../model/mod_pack";
|
|||||||
import InstanceModPack from "./InstanceModPack";
|
import InstanceModPack from "./InstanceModPack";
|
||||||
import useLocalStorage from "../util/useLocalStorage";
|
import useLocalStorage from "../util/useLocalStorage";
|
||||||
import useResourceState from "../util/useResourceState";
|
import useResourceState from "../util/useResourceState";
|
||||||
|
import usePluginActions from "../util/usePluginActions";
|
||||||
import { MetricRelativeDate } from "./system_metrics";
|
import { MetricRelativeDate } from "./system_metrics";
|
||||||
|
|
||||||
type MenuItem = Required<MenuProps>["items"][number];
|
type MenuItem = Required<MenuProps>["items"][number];
|
||||||
@@ -150,6 +151,7 @@ function InstanceButtons(props: { instance: lib.InstanceDetails }) {
|
|||||||
let [host] = useHost(instance.assignedHost);
|
let [host] = useHost(instance.assignedHost);
|
||||||
const [instances] = useInstances();
|
const [instances] = useInstances();
|
||||||
const canCreate = account.hasPermission("core.instance.create");
|
const canCreate = account.hasPermission("core.instance.create");
|
||||||
|
const pluginActions = usePluginActions("InstanceViewPage", { instance });
|
||||||
|
|
||||||
let instanceButtonMenuItems: MenuItem[] = [];
|
let instanceButtonMenuItems: MenuItem[] = [];
|
||||||
if (account.hasPermission("core.instance.export_data")) {
|
if (account.hasPermission("core.instance.export_data")) {
|
||||||
@@ -166,6 +168,7 @@ function InstanceButtons(props: { instance: lib.InstanceDetails }) {
|
|||||||
label: "Extract players from save",
|
label: "Extract players from save",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
instanceButtonMenuItems.push(...pluginActions);
|
||||||
if (account.hasPermission("core.instance.delete")) {
|
if (account.hasPermission("core.instance.delete")) {
|
||||||
if (instanceButtonMenuItems.length) {
|
if (instanceButtonMenuItems.length) {
|
||||||
instanceButtonMenuItems.push({ type: "divider" });
|
instanceButtonMenuItems.push({ type: "divider" });
|
||||||
@@ -230,6 +233,10 @@ function InstanceButtons(props: { instance: lib.InstanceDetails }) {
|
|||||||
icon={<DeleteOutlined />}
|
icon={<DeleteOutlined />}
|
||||||
onClick={confirmDelete}
|
onClick={confirmDelete}
|
||||||
>Delete</Button>}
|
>Delete</Button>}
|
||||||
|
{pluginActions.length > 0
|
||||||
|
&& <Dropdown placement="bottomRight" trigger={["click"]} menu={{ items: pluginActions }}>
|
||||||
|
<Button>More <DownOutlined /></Button>
|
||||||
|
</Dropdown>}
|
||||||
</Space>;
|
</Space>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,11 +263,7 @@ function InstanceButtons(props: { instance: lib.InstanceDetails }) {
|
|||||||
buttonText="Create template"
|
buttonText="Create template"
|
||||||
buttonType="default"
|
buttonType="default"
|
||||||
/>}
|
/>}
|
||||||
{account.hasAnyPermission(
|
{instanceButtonMenuItems.length > 0
|
||||||
"core.instance.export_data",
|
|
||||||
"core.instance.extract_players",
|
|
||||||
"core.instance.delete",
|
|
||||||
)
|
|
||||||
&& <Dropdown placement="bottomRight" trigger={["click"]} menu={instanceButtonsMenuProps}>
|
&& <Dropdown placement="bottomRight" trigger={["click"]} menu={instanceButtonsMenuProps}>
|
||||||
<Button>More <DownOutlined /></Button>
|
<Button>More <DownOutlined /></Button>
|
||||||
</Dropdown>}
|
</Dropdown>}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import React, { useEffect, useContext, useState } from "react";
|
import React, { useEffect, useContext, useState } from "react";
|
||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import {
|
import {
|
||||||
Button, Col, Descriptions, Form, Input, Popconfirm,
|
Button, Col, Descriptions, Dropdown, Form, Input, Popconfirm,
|
||||||
Row, Table, Tag, Select, Space, Switch,
|
Row, Table, Tag, Select, Space, Switch,
|
||||||
} from "antd";
|
} from "antd";
|
||||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
||||||
|
import DownOutlined from "@ant-design/icons/DownOutlined";
|
||||||
|
|
||||||
import * as lib from "@clusterio/lib";
|
import * as lib from "@clusterio/lib";
|
||||||
|
|
||||||
@@ -21,6 +22,7 @@ import ResourceNotes from "./ResourceNotes";
|
|||||||
import ResourceState from "./ResourceState";
|
import ResourceState from "./ResourceState";
|
||||||
import useRowNavigation from "../util/useRowNavigation";
|
import useRowNavigation from "../util/useRowNavigation";
|
||||||
import useResourceState from "../util/useResourceState";
|
import useResourceState from "../util/useResourceState";
|
||||||
|
import usePluginActions from "../util/usePluginActions";
|
||||||
import Link from "./Link";
|
import Link from "./Link";
|
||||||
import notify, { notifyErrorHandler } from "../util/notify";
|
import notify, { notifyErrorHandler } from "../util/notify";
|
||||||
import { formatDuration } from "../util/time_format";
|
import { formatDuration } from "../util/time_format";
|
||||||
@@ -29,6 +31,16 @@ import { formatFirstSeen, formatLastSeen, sortFirstSeen, sortLastSeen, useUser }
|
|||||||
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
const strcmp = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }).compare;
|
||||||
|
|
||||||
|
|
||||||
|
function UserPluginActions(props: { user: lib.UserDetails }) {
|
||||||
|
const pluginActions = usePluginActions("UserViewPage", { user: props.user });
|
||||||
|
if (!pluginActions.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return <Dropdown placement="bottomRight" trigger={["click"]} menu={{ items: pluginActions }}>
|
||||||
|
<Button>More <DownOutlined /></Button>
|
||||||
|
</Dropdown>;
|
||||||
|
}
|
||||||
|
|
||||||
// The user page has a lot of optional elements, that does not make it
|
// The user page has a lot of optional elements, that does not make it
|
||||||
// praticularly complicated.
|
// praticularly complicated.
|
||||||
// eslint-disable-next-line complexity
|
// eslint-disable-next-line complexity
|
||||||
@@ -167,6 +179,7 @@ export default function UserViewPage() {
|
|||||||
<DeleteOutlined />
|
<DeleteOutlined />
|
||||||
</Button>
|
</Button>
|
||||||
</Popconfirm>}
|
</Popconfirm>}
|
||||||
|
<UserPluginActions user={user} />
|
||||||
</Space>}
|
</Space>}
|
||||||
/>
|
/>
|
||||||
<Form
|
<Form
|
||||||
|
|||||||
@@ -15,19 +15,20 @@ export default function VariableDropdownButton(props: VariableDropdownButtonProp
|
|||||||
return <></>;
|
return <></>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const mainDisabled = props.disabled || mainAction.disabled;
|
||||||
const buttonProps: DropdownButtonProps = {
|
const buttonProps: DropdownButtonProps = {
|
||||||
...props,
|
...props,
|
||||||
danger: mainAction.danger,
|
danger: mainAction.danger,
|
||||||
onClick: (event) => {
|
onClick: (event) => {
|
||||||
event.stopPropagation();
|
event.stopPropagation();
|
||||||
if (mainAction.onClick) {
|
if (!mainDisabled && mainAction.onClick) {
|
||||||
mainAction.onClick();
|
mainAction.onClick();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
return menuActions.length === 0
|
return menuActions.length === 0
|
||||||
? <Button {...buttonProps}>
|
? <Button {...buttonProps} disabled={mainDisabled}>
|
||||||
{mainAction.label}
|
{mainAction.label}
|
||||||
</Button>
|
</Button>
|
||||||
: <Dropdown.Button
|
: <Dropdown.Button
|
||||||
@@ -35,7 +36,8 @@ export default function VariableDropdownButton(props: VariableDropdownButtonProp
|
|||||||
// stopPropagation is needed to prevent propagation to table rows
|
// stopPropagation is needed to prevent propagation to table rows
|
||||||
menu={{ items: menuActions, onClick: (e: any) => e.domEvent.stopPropagation() }}
|
menu={{ items: menuActions, onClick: (e: any) => e.domEvent.stopPropagation() }}
|
||||||
buttonsRender={([left, right]) => [
|
buttonsRender={([left, right]) => [
|
||||||
left, React.cloneElement(right as any, { onClick: (e: any) => e.stopPropagation() }),
|
React.cloneElement(left as any, { disabled: mainDisabled }),
|
||||||
|
React.cloneElement(right as any, { onClick: (e: any) => e.stopPropagation() }),
|
||||||
]}
|
]}
|
||||||
>
|
>
|
||||||
{mainAction.label}
|
{mainAction.label}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import type {
|
||||||
|
PageActions, PageActionsHook, PageActionsSlot, PageActionsSlotProps, PluginPageAction, WebHooks,
|
||||||
|
} from "../BaseWebPlugin";
|
||||||
|
|
||||||
|
export async function pageActionsFromHooks(hooks: WebHooks) {
|
||||||
|
const pageActions: Partial<PageActions> = {};
|
||||||
|
for (const [source, record] of await hooks.pageActions.collectEntries()) {
|
||||||
|
for (const [slot, useActions] of Object.entries(record) as [PageActionsSlot, PageActionsHook<any>][]) {
|
||||||
|
if (!pageActions[slot]) {
|
||||||
|
pageActions[slot] = new Map();
|
||||||
|
}
|
||||||
|
pageActions[slot].set(source, useActions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return pageActions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call each plugin's actions hook for a page and prefix the keys with the
|
||||||
|
* plugin name. Must be called during render, the set of plugins is fixed
|
||||||
|
* after bootstrap so the hooks are called in the same order every time.
|
||||||
|
*/
|
||||||
|
export function callPageActions<K extends PageActionsSlot>(
|
||||||
|
hooks: Map<string, PageActionsHook<K>> | undefined,
|
||||||
|
props: PageActionsSlotProps[K],
|
||||||
|
) {
|
||||||
|
const actions: PluginPageAction[] = [];
|
||||||
|
for (const [source, useActions] of hooks ?? []) {
|
||||||
|
for (const action of useActions(props)) {
|
||||||
|
actions.push({ ...action, key: `${source}.${action.key}` });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return actions;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { useContext } from "react";
|
||||||
|
|
||||||
|
import ControlContext from "../components/ControlContext";
|
||||||
|
import type { PageActionsSlot, PageActionsSlotProps, PluginPageAction } from "../BaseWebPlugin";
|
||||||
|
import { callPageActions } from "./plugin_actions";
|
||||||
|
|
||||||
|
/** Actions plugins add to the dropdown of a page. */
|
||||||
|
export default function usePluginActions<K extends PageActionsSlot>(
|
||||||
|
slot: K,
|
||||||
|
props: PageActionsSlotProps[K],
|
||||||
|
): PluginPageAction[] {
|
||||||
|
const control = useContext(ControlContext);
|
||||||
|
return callPageActions(control.pageActions[slot], props);
|
||||||
|
}
|
||||||
@@ -64,6 +64,7 @@ export class Control extends lib.Link {
|
|||||||
public loadedPlugins = new Map<string, lib.PluginWebEnvInfo>();
|
public loadedPlugins = new Map<string, lib.PluginWebEnvInfo>();
|
||||||
public inputComponents = new Map<string, WebPlugin.InputComponent>();
|
public inputComponents = new Map<string, WebPlugin.InputComponent>();
|
||||||
public extensionComponents: Partial<WebPlugin.ExtensionComponents> = {};
|
public extensionComponents: Partial<WebPlugin.ExtensionComponents> = {};
|
||||||
|
public pageActions: Partial<WebPlugin.PageActions> = {};
|
||||||
public loginForms: WebPlugin.PluginLoginForm[] = [];
|
public loginForms: WebPlugin.PluginLoginForm[] = [];
|
||||||
public pages: WebPlugin.PluginPage[] = [];
|
public pages: WebPlugin.PluginPage[] = [];
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
|
||||||
|
import * as lib from "@clusterio/lib";
|
||||||
|
import { callPageActions, pageActionsFromHooks } from "../../packages/web_ui/src/util/plugin_actions.ts";
|
||||||
|
|
||||||
|
// BaseWebPlugin.ts uses parameter properties, which Node can't strip
|
||||||
|
class WebHooks extends lib.AsyncHookCollection {
|
||||||
|
pageActions = this.newHook();
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("web_ui/util/plugin_actions", function() {
|
||||||
|
it("should prefix keys with the plugin name and keep plugin order", async function() {
|
||||||
|
const hooks = new WebHooks(lib.logger);
|
||||||
|
hooks.pageActions.attach("alpha", () => ({
|
||||||
|
UserViewPage: ({ user }) => [{ key: "greet", label: `Hello ${user.name}` }],
|
||||||
|
}));
|
||||||
|
hooks.pageActions.attach("beta", () => ({
|
||||||
|
UserViewPage: () => [{ key: "a", label: "A" }, { key: "b", label: "B", danger: true }],
|
||||||
|
HostViewPage: () => [{ key: "c", label: "C" }],
|
||||||
|
}));
|
||||||
|
hooks.pageActions.attach("gamma", () => ({}));
|
||||||
|
|
||||||
|
const pageActions = await pageActionsFromHooks(hooks);
|
||||||
|
assert.deepEqual(Object.keys(pageActions).sort(), ["HostViewPage", "UserViewPage"]);
|
||||||
|
assert.deepEqual(callPageActions(pageActions.UserViewPage, { user: { name: "bob" } }), [
|
||||||
|
{ key: "alpha.greet", label: "Hello bob" },
|
||||||
|
{ key: "beta.a", label: "A" },
|
||||||
|
{ key: "beta.b", label: "B", danger: true },
|
||||||
|
]);
|
||||||
|
assert.deepEqual(callPageActions(pageActions.InstanceViewPage, { instance: {} }), []);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user