Make the control buttons consistent

This commit is contained in:
Cooldude2606 committed 2025-07-15 00:49:33 +01:00
1 parent f11d91bd6b
commit 59256e77d1
4 files changed
+202 -131

No files matched your search

@@ -1,5 +1,5 @@
import React, {useContext, useState} from "react"; import React, {useContext, useState} from "react";
import { Button, Descriptions, Flex, Popconfirm, Space, Tooltip, Typography } from "antd"; import { Descriptions, Flex, Popconfirm, Space, Tooltip, Typography } from "antd";
import { ExclamationCircleOutlined } from "@ant-design/icons"; import { ExclamationCircleOutlined } from "@ant-design/icons";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
@@ -19,24 +19,39 @@ import PageLayout from "./PageLayout";
import { notifyErrorHandler } from "../util/notify"; import { 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 VariableDropdownButton, { VariableDropdownButtonProps } from "./VariableDropdownButton";
const { Title } = Typography; const { Title } = Typography;
type ControllerControlButtonProps = { canRestart?: boolean, restartRequired?: boolean };
export default function ControllerPage() { function ControllerControlButton({ canRestart, restartRequired }: ControllerControlButtonProps) {
let account = useAccount(); const account = useAccount();
const control = useContext(ControlContext); const control = useContext(ControlContext);
const [systems] = useSystems(); const actions: VariableDropdownButtonProps["actions"] = [];
const system = systems.get("controller");
const [maxLevel, setMaxLevel] = useState<keyof typeof lib.levels>("info");
return <PageLayout nav={[{ name: "Controller" }]}> if (account.hasPermission("core.controller.restart") && canRestart) {
<PageHeader actions.push({
title="Controller" key: "restart",
extra={<Space> label: (
{ <Tooltip title={restartRequired ? "Restart Required" : null}>
account.hasPermission("core.controller.stop") Restart {restartRequired ? <ExclamationCircleOutlined style={{ color: "yellow" }}/> : undefined}
&& <Popconfirm </Tooltip>
),
onClick: () => {
control.send(
new lib.ControllerRestartRequest()
).catch(notifyErrorHandler("Error restarting controller"));
},
});
}
if (account.hasPermission("core.controller.stop")) {
actions.push({
key: "stop",
danger: true,
label: (
<Popconfirm
title={<> title={<>
Stopping the controller will leave the cluster inoperable until someone with Stopping the controller will leave the cluster inoperable until someone with
access to the system it runs on manually starts it again.<br /> access to the system it runs on manually starts it again.<br />
@@ -51,26 +66,27 @@ export default function ControllerPage() {
).catch(notifyErrorHandler("Error stopping controller")); ).catch(notifyErrorHandler("Error stopping controller"));
}} }}
> >
<Button danger>Stop</Button> Stop
</Popconfirm> </Popconfirm>
),
});
} }
{
account.hasPermission("core.controller.restart") return <VariableDropdownButton actions={actions} />;
&& <Tooltip title={system?.restartRequired ? "Restart Required" : null}> }
<Button
disabled={system?.canRestart === false}
onClick={() => { export default function ControllerPage() {
control.send( const account = useAccount();
new lib.ControllerRestartRequest() const [systems] = useSystems();
).catch(notifyErrorHandler("Error restarting controller")); const system = systems.get("controller");
}} const [maxLevel, setMaxLevel] = useState<keyof typeof lib.levels>("info");
>
Restart return <PageLayout nav={[{ name: "Controller" }]}>
{!system?.restartRequired ? undefined <PageHeader
: <ExclamationCircleOutlined style={{ color: "yellow" }}/>} title="Controller"
</Button> extra={<Space>
</Tooltip> <ControllerControlButton canRestart={system?.canRestart} restartRequired={system?.restartRequired}/>
}
{ {
hasNpmButtonPermission(true) hasNpmButtonPermission(true)
&& <NpmButton target="controller" canRestart={system?.canRestart}/> && <NpmButton target="controller" canRestart={system?.canRestart}/>
+56 -39
View File
@@ -22,9 +22,64 @@ import {
import { formatTimestamp } from "../util/time_format"; import { formatTimestamp } from "../util/time_format";
import { useSystems } from "../model/system"; import { useSystems } from "../model/system";
import { hasNpmButtonPermission, NpmButton } from "./NpmButton"; import { hasNpmButtonPermission, NpmButton } from "./NpmButton";
import VariableDropdownButton, { VariableDropdownButtonProps } from "./VariableDropdownButton";
const { Title } = Typography; const { Title } = Typography;
type HostControlButtonProps = { hostId: number, canRestart?: boolean, restartRequired?: boolean };
function HostControlButton({ hostId, canRestart, restartRequired }: HostControlButtonProps) {
const account = useAccount();
const control = useContext(ControlContext);
const actions: VariableDropdownButtonProps["actions"] = [];
if (account.hasPermission("core.host.restart") && canRestart) {
actions.push({
key: "restart",
label: (
<Tooltip title={restartRequired ? "Restart Required" : null}>
Restart {restartRequired ? <ExclamationCircleOutlined style={{ color: "yellow" }}/> : undefined}
</Tooltip>
),
onClick: () => {
control.sendTo(
{ hostId },
new lib.HostRestartRequest()
).catch(notifyErrorHandler("Error restarting host"));
},
});
}
if (account.hasPermission("core.host.stop")) {
actions.push({
key: "stop",
danger: true,
label: (
<Popconfirm
title={<>
Stopping this host will leave it inaccessible until someone with
access to the system it runs on manually starts it again.<br />
Are you sure you want to stop the host?
</>}
placement="bottomRight"
okText="Stop"
okButtonProps={{ danger: true }}
onConfirm={() => {
control.sendTo(
{ hostId },
new lib.HostStopRequest()
).catch(notifyErrorHandler("Error stopping host"));
}}
>
Stop
</Popconfirm>
),
});
}
return <VariableDropdownButton actions={actions} />;
}
export default function HostViewPage() { export default function HostViewPage() {
let params = useParams(); let params = useParams();
let hostId = Number(params.id); let hostId = Number(params.id);
@@ -50,6 +105,7 @@ export default function HostViewPage() {
} }
let hostButtons = <Space> let hostButtons = <Space>
<HostControlButton hostId={hostId} canRestart={system?.canRestart} restartRequired={system?.restartRequired}/>
{ {
account.hasPermission("core.host.revoke_token") account.hasPermission("core.host.revoke_token")
&& <Popconfirm && <Popconfirm
@@ -68,45 +124,6 @@ export default function HostViewPage() {
</Button> </Button>
</Popconfirm> </Popconfirm>
} }
{
account.hasPermission("core.host.stop")
&& <Popconfirm
title={<>
Stopping this host will leave it inaccessible until someone with
access to the system it runs on manually starts it again.<br />
Are you sure you want to stop the host?
</>}
placement="bottomRight"
okText="Stop"
okButtonProps={{ danger: true }}
onConfirm={() => {
control.sendTo(
{ hostId },
new lib.HostStopRequest()
).catch(notifyErrorHandler("Error stopping host"));
}}
>
<Button danger>Stop</Button>
</Popconfirm>
}
{
account.hasPermission("core.host.restart")
&& <Tooltip title={system?.restartRequired ? "Restart Required" : null}>
<Button
disabled={system?.canRestart === false}
onClick={() => {
control.sendTo(
{ hostId },
new lib.HostRestartRequest()
).catch(notifyErrorHandler("Error restarting host"));
}}
>
Restart
{!system?.restartRequired ? undefined
: <ExclamationCircleOutlined style={{ color: "yellow" }} />}
</Button>
</Tooltip>
}
{ {
hasNpmButtonPermission(false) hasNpmButtonPermission(false)
&& <NpmButton target={{ hostId }} canRestart={system?.canRestart} disabled={!host["connected"]}/> && <NpmButton target={{ hostId }} canRestart={system?.canRestart} disabled={!host["connected"]}/>
@@ -1,14 +1,14 @@
import React, { useContext, useState } from "react"; import React, { useContext, useState } from "react";
import { Button, MenuProps } from "antd"; import { Popconfirm } from "antd";
import * as lib from "@clusterio/lib"; import * as lib from "@clusterio/lib";
import ControlContext from "./ControlContext"; import ControlContext from "./ControlContext";
import { useAccount } from "../model/account"; import { useAccount } from "../model/account";
import { notifyErrorHandler } from "../util/notify"; import { notifyErrorHandler } from "../util/notify";
import Dropdown, { DropdownButtonProps } from "antd/es/dropdown"; import VariableDropdownButton, { VariableDropdownButtonProps } from "./VariableDropdownButton";
type InstanceControlButtonProps = DropdownButtonProps & { type InstanceControlButtonProps = Omit<VariableDropdownButtonProps, "actions"> & {
instance: lib.InstanceDetails; instance: lib.InstanceDetails;
onFinish?: () => void; onFinish?: () => void;
}; };
@@ -34,94 +34,89 @@ export default function InstanceControlButton(props: InstanceControlButtonProps)
} }
} }
const onStopStartClick: DropdownButtonProps["onClick"] = (event) => { const actions: VariableDropdownButtonProps["actions"] = [];
event.stopPropagation();
setSwitching(true);
if (showStart) { if (account.hasPermission("core.instance.start") && showStart !== switching) {
actions.push({
key: "start",
label: "Start",
onClick: () => {
setSwitching(true);
control.sendTo( control.sendTo(
{ instanceId }, { instanceId },
new lib.InstanceStartRequest(undefined), new lib.InstanceStartRequest(undefined),
).catch( ).catch(
notifyErrorHandler("Error starting instance") notifyErrorHandler("Error starting instance")
).finally(actionFinish); ).finally(actionFinish);
},
});
} else { } else if (account.hasPermission("core.instance.stop")) { // else if because it is stop or start
actions.push({
key: "stop",
label: "Stop",
danger: true,
onClick: () => {
setSwitching(true);
control.sendTo( control.sendTo(
{ instanceId }, { instanceId },
new lib.InstanceStopRequest(), new lib.InstanceStopRequest(),
).catch( ).catch(
notifyErrorHandler("Error stopping instance") notifyErrorHandler("Error stopping instance")
).finally(actionFinish); ).finally(actionFinish);
},
});
} }
};
const onMenuClick: MenuProps["onClick"] = (event) => { if (account.hasPermission("core.instance.restart")) {
event.domEvent.stopPropagation(); actions.push({
key: "restart",
label: "Restart",
disabled: instanceStatus !== "running",
onClick: () => {
setSwitching(true); setSwitching(true);
switch (event.key) {
case "restart":
control.sendTo( control.sendTo(
{ instanceId }, { instanceId },
new lib.InstanceRestartRequest(undefined), new lib.InstanceRestartRequest(undefined),
).catch( ).catch(
notifyErrorHandler("Error restarting instance") notifyErrorHandler("Error restarting instance")
).finally(actionFinish); ).finally(actionFinish);
break; },
});
}
case "kill": if (account.hasPermission("core.instance.kill")) {
actions.push({
key: "kill",
danger: true,
disabled: instanceStatus === "stopped",
label: (<Popconfirm
title={<>
Killing this instance may leave you save and player data corrupted.
Are you sure you want to kill the instance?
</>}
placement="bottomRight"
okText="Kill"
okButtonProps={{ danger: true }}
onConfirm={() => {
control.sendTo( control.sendTo(
{ instanceId }, { instanceId },
new lib.InstanceKillRequest(), new lib.InstanceKillRequest(),
).catch( ).catch(
notifyErrorHandler("Error killing instance") notifyErrorHandler("Error killing instance")
).finally(actionFinish); ).finally(actionFinish);
break; }}
default:
notifyErrorHandler("Unknown control action");
}
};
const extraControls: MenuProps["items"] = [];
if (account.hasPermission("core.instance.restart")) {
extraControls.push({
key: "restart",
label: "Restart",
disabled: instanceStatus !== "running",
});
}
if (account.hasPermission("core.instance.kill")) {
extraControls.push({
key: "kill",
label: "Kill",
disabled: instanceStatus === "stopped",
});
}
const buttonProps: DropdownButtonProps = {
...props,
type: "primary",
loading: switching,
onClick: onStopStartClick,
disabled: !(showStart || showStop),
danger: showStop !== switching, // Used as an XOR, also below
};
return extraControls.length > 0
? <Dropdown.Button
{...buttonProps}
menu={{ items: extraControls, onClick: onMenuClick }}
buttonsRender={([left, right]) => [ // This is needed to prevent propagation to the table row
left, React.cloneElement(right as any, { onClick: (e: any) => e.stopPropagation() }),
]}
> >
{showStart !== switching ? "Start" : "Stop"} Kill
</Dropdown.Button> </Popconfirm>),
: <Button {...buttonProps}> });
{showStart !== switching ? "Start" : "Stop"} }
</Button>;
return <VariableDropdownButton
{...props}
type="primary"
actions={actions}
loading={switching}
disabled={!(showStart || showStop)}
/>;
} }
@@ -0,0 +1,43 @@
import React from "react";
import { Button, Dropdown } from "antd";
import { MenuItemType } from "antd/es/menu/interface";
import { DropdownButtonProps } from "antd/es/dropdown";
export type VariableDropdownButtonProps = DropdownButtonProps & {
actions: (MenuItemType & { onClick?: () => void })[],
};
export default function VariableDropdownButton(props: VariableDropdownButtonProps) {
const menuActions = props.actions;
const mainAction = menuActions.shift();
if (!mainAction) {
return <></>;
}
const buttonProps: DropdownButtonProps = {
...props,
danger: mainAction.danger,
onClick: (event) => {
event.stopPropagation();
if (mainAction.onClick) {
mainAction.onClick();
}
},
};
return menuActions.length === 0
? <Button {...buttonProps}>
{mainAction.label}
</Button>
: <Dropdown.Button
{...buttonProps}
// stopPropagation is needed to prevent propagation to table rows
menu={{ items: menuActions, onClick: (e: any) => e.domEvent.stopPropagation() }}
buttonsRender={([left, right]) => [
left, React.cloneElement(right as any, { onClick: (e: any) => e.stopPropagation() }),
]}
>
{mainAction.label}
</Dropdown.Button>;
}