mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 07:24:53 +09:00
Make the control buttons consistent
This commit is contained in:
4 files changed
+226
-155
No files matched your search
@@ -1,14 +1,14 @@
|
||||
import React, { useContext, useState } from "react";
|
||||
import { Button, MenuProps } from "antd";
|
||||
import { Popconfirm } from "antd";
|
||||
|
||||
import * as lib from "@clusterio/lib";
|
||||
|
||||
import ControlContext from "./ControlContext";
|
||||
import { useAccount } from "../model/account";
|
||||
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;
|
||||
onFinish?: () => void;
|
||||
};
|
||||
@@ -34,94 +34,89 @@ export default function InstanceControlButton(props: InstanceControlButtonProps)
|
||||
}
|
||||
}
|
||||
|
||||
const onStopStartClick: DropdownButtonProps["onClick"] = (event) => {
|
||||
event.stopPropagation();
|
||||
setSwitching(true);
|
||||
const actions: VariableDropdownButtonProps["actions"] = [];
|
||||
|
||||
if (showStart) {
|
||||
control.sendTo(
|
||||
{ instanceId },
|
||||
new lib.InstanceStartRequest(undefined),
|
||||
).catch(
|
||||
notifyErrorHandler("Error starting instance")
|
||||
).finally(actionFinish);
|
||||
if (account.hasPermission("core.instance.start") && showStart !== switching) {
|
||||
actions.push({
|
||||
key: "start",
|
||||
label: "Start",
|
||||
onClick: () => {
|
||||
setSwitching(true);
|
||||
control.sendTo(
|
||||
{ instanceId },
|
||||
new lib.InstanceStartRequest(undefined),
|
||||
).catch(
|
||||
notifyErrorHandler("Error starting instance")
|
||||
).finally(actionFinish);
|
||||
},
|
||||
});
|
||||
|
||||
} else {
|
||||
control.sendTo(
|
||||
{ instanceId },
|
||||
new lib.InstanceStopRequest(),
|
||||
).catch(
|
||||
notifyErrorHandler("Error stopping instance")
|
||||
).finally(actionFinish);
|
||||
} 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(
|
||||
{ instanceId },
|
||||
new lib.InstanceStopRequest(),
|
||||
).catch(
|
||||
notifyErrorHandler("Error stopping instance")
|
||||
).finally(actionFinish);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
const onMenuClick: MenuProps["onClick"] = (event) => {
|
||||
event.domEvent.stopPropagation();
|
||||
setSwitching(true);
|
||||
|
||||
switch (event.key) {
|
||||
case "restart":
|
||||
if (account.hasPermission("core.instance.restart")) {
|
||||
actions.push({
|
||||
key: "restart",
|
||||
label: "Restart",
|
||||
disabled: instanceStatus !== "running",
|
||||
onClick: () => {
|
||||
setSwitching(true);
|
||||
control.sendTo(
|
||||
{ instanceId },
|
||||
new lib.InstanceRestartRequest(undefined),
|
||||
).catch(
|
||||
notifyErrorHandler("Error restarting instance")
|
||||
).finally(actionFinish);
|
||||
break;
|
||||
|
||||
case "kill":
|
||||
control.sendTo(
|
||||
{ instanceId },
|
||||
new lib.InstanceKillRequest(),
|
||||
).catch(
|
||||
notifyErrorHandler("Error killing instance")
|
||||
).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({
|
||||
actions.push({
|
||||
key: "kill",
|
||||
label: "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(
|
||||
{ instanceId },
|
||||
new lib.InstanceKillRequest(),
|
||||
).catch(
|
||||
notifyErrorHandler("Error killing instance")
|
||||
).finally(actionFinish);
|
||||
}}
|
||||
>
|
||||
Kill
|
||||
</Popconfirm>),
|
||||
});
|
||||
}
|
||||
|
||||
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"}
|
||||
</Dropdown.Button>
|
||||
: <Button {...buttonProps}>
|
||||
{showStart !== switching ? "Start" : "Stop"}
|
||||
</Button>;
|
||||
|
||||
return <VariableDropdownButton
|
||||
{...props}
|
||||
type="primary"
|
||||
actions={actions}
|
||||
loading={switching}
|
||||
disabled={!(showStart || showStop)}
|
||||
/>;
|
||||
}
|
||||
Reference in new issue
Block a user