1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Organize menu-related code more neatly

Signed-off-by: Alex Culliere <alozhkin@mirantis.com>
This commit is contained in:
Alex Culliere 2021-02-04 13:24:08 +02:00
parent 6a70d89f3b
commit 5e2d77d358
4 changed files with 79 additions and 97 deletions

View File

@ -0,0 +1,46 @@
import React from "react";
import { clusterSettingsURL } from "../+cluster-settings";
import { landingURL } from "../+landing-page";
import { clusterStore } from "../../../common/cluster-store";
import { broadcastMessage, requestMain } from "../../../common/ipc";
import { clusterDisconnectHandler } from "../../../common/cluster-ipc";
import { ConfirmDialog } from "../confirm-dialog";
import { Cluster } from "../../../main/cluster";
const navigate = (route: string) =>
broadcastMessage("renderer:navigate", route);
const deactivateCluster = (clusterId: string) => {
if (clusterStore.isActive(clusterId)) {
navigate(landingURL());
clusterStore.setActive(null);
}
};
/**
* Creates hanndlers for high-level actions
* that could be performed on an individual cluster
* @param cluster Cluster>
*/
export const ClusterActions = (cluster: Cluster) => ({
"SHOW_SETTINGS": () => navigate(clusterSettingsURL({
params: { clusterId: cluster.id }
})),
"DISCONNECT": async () => {
deactivateCluster(cluster.id);
await requestMain(clusterDisconnectHandler, cluster.id);
},
"REMOVE": () => ConfirmDialog.open({
okButtonProps: {
primary: false,
accent: true,
label: "Remove"
},
ok: () => {
deactivateCluster(cluster.id);
clusterStore.removeById(cluster.id);
},
message: <p>Are you sure want to remove cluster <b title={cluster.id}>{cluster.contextName}</b>?</p>,
})
});

View File

@ -2,7 +2,6 @@ import "./clusters-menu.scss";
import React from "react"; import React from "react";
import { remote } from "electron"; import { remote } from "electron";
import { requestMain } from "../../../common/ipc";
import type { Cluster } from "../../../main/cluster"; import type { Cluster } from "../../../main/cluster";
import { DragDropContext, Draggable, DraggableProvided, Droppable, DroppableProvided, DropResult } from "react-beautiful-dnd"; import { DragDropContext, Draggable, DraggableProvided, Droppable, DroppableProvided, DropResult } from "react-beautiful-dnd";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
@ -13,12 +12,10 @@ import { Icon } from "../icon";
import { autobind, cssNames, IClassName } from "../../utils"; import { autobind, cssNames, IClassName } from "../../utils";
import { isActiveRoute, navigate } from "../../navigation"; import { isActiveRoute, navigate } from "../../navigation";
import { addClusterURL } from "../+add-cluster"; import { addClusterURL } from "../+add-cluster";
import { clusterSettingsURL } from "../+cluster-settings";
import { landingURL } from "../+landing-page"; import { landingURL } from "../+landing-page";
import { ConfirmDialog } from "../confirm-dialog";
import { clusterViewURL } from "./cluster-view.route"; import { clusterViewURL } from "./cluster-view.route";
import { ClusterActions } from "./cluster-actions";
import { getExtensionPageUrl, globalPageMenuRegistry, globalPageRegistry } from "../../../extensions/registries"; import { getExtensionPageUrl, globalPageMenuRegistry, globalPageRegistry } from "../../../extensions/registries";
import { clusterDisconnectHandler } from "../../../common/cluster-ipc";
import { commandRegistry } from "../../../extensions/registries/command-registry"; import { commandRegistry } from "../../../extensions/registries/command-registry";
import { CommandOverlay } from "../command-palette/command-container"; import { CommandOverlay } from "../command-palette/command-container";
import { computed, observable } from "mobx"; import { computed, observable } from "mobx";
@ -40,51 +37,24 @@ export class ClustersMenu extends React.Component<Props> {
showContextMenu = (cluster: Cluster) => { showContextMenu = (cluster: Cluster) => {
const { Menu, MenuItem } = remote; const { Menu, MenuItem } = remote;
const menu = new Menu(); const menu = new Menu();
const actions = ClusterActions(cluster);
menu.append(new MenuItem({ menu.append(new MenuItem({
label: `Settings`, label: `Settings`,
click: () => { click: actions.SHOW_SETTINGS
navigate(clusterSettingsURL({
params: {
clusterId: cluster.id
}
}));
}
})); }));
if (cluster.online) { if (cluster.online) {
menu.append(new MenuItem({ menu.append(new MenuItem({
label: `Disconnect`, label: `Disconnect`,
click: async () => { click: actions.DISCONNECT
if (clusterStore.isActive(cluster.id)) {
navigate(landingURL());
clusterStore.setActive(null);
}
await requestMain(clusterDisconnectHandler, cluster.id);
}
})); }));
} }
if (!cluster.isManaged) { if (!cluster.isManaged) {
menu.append(new MenuItem({ menu.append(new MenuItem({
label: `Remove`, label: `Remove`,
click: () => { click: actions.REMOVE
ConfirmDialog.open({
okButtonProps: {
primary: false,
accent: true,
label: `Remove`,
},
ok: () => {
if (clusterStore.activeClusterId === cluster.id) {
navigate(landingURL());
clusterStore.setActive(null);
}
clusterStore.removeById(cluster.id);
},
message: <p>Are you sure want to remove cluster <b title={cluster.id}>{cluster.contextName}</b>?</p>,
});
}
})); }));
} }
menu.popup({ menu.popup({

View File

@ -1 +1,2 @@
export * from "./cluster-manager"; export * from "./cluster-manager";
export * from "./cluster-actions";

View File

@ -1,16 +1,12 @@
import React, { useState } from "react";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import React from "react"; import { uniqueId } from "lodash";
import { remote } from "electron";
import { clusterSettingsURL } from "../+cluster-settings"; import { ClusterActions } from "../cluster-manager";
import { landingURL } from "../+landing-page";
import { ConfirmDialog } from "../confirm-dialog";
import { clusterStore } from "../../../common/cluster-store";
import { clusterDisconnectHandler } from "../../../common/cluster-ipc";
import { broadcastMessage, requestMain } from "../../../common/ipc";
import { Cluster } from "../../../main/cluster"; import { Cluster } from "../../../main/cluster";
import { cssNames } from "../../utils"; import { cssNames } from "../../utils";
import { Icon } from "../icon"; import { Icon } from "../icon";
import { Menu, MenuItem } from "../menu";
interface Props { interface Props {
cluster: Cluster cluster: Cluster
@ -18,70 +14,39 @@ interface Props {
} }
export const MainLayoutHeader = observer(({ cluster, className }: Props) => { export const MainLayoutHeader = observer(({ cluster, className }: Props) => {
const showContextMenu = () => { const [isMenuOpen, setMenuOpen] = useState(false);
const { Menu, MenuItem } = remote; const id = uniqueId("cluster_actions_");
const menu = new Menu(); const actions = ClusterActions(cluster);
menu.append(new MenuItem({ const renderMenu = () => <Menu
label: "Settings", usePortal
click: () => { isOpen={isMenuOpen}
broadcastMessage("renderer:navigate", clusterSettingsURL({ open={() => setMenuOpen(true)}
params: { close={() => setMenuOpen(false)}
clusterId: cluster.id className="ClusterActionsMenu"
} htmlFor={id}
})); toggleEvent="click">
} <MenuItem onClick={actions.SHOW_SETTINGS}>
})); <span>Settings</span>
</MenuItem>
if (cluster.online) { { cluster.online && <MenuItem onClick={actions.DISCONNECT}>
menu.append(new MenuItem({ <span>Disconnect</span>
label: "Disconnect", </MenuItem> }
click: async () => { { cluster.online && !cluster.isManaged && <MenuItem onClick={actions.REMOVE}>
if (clusterStore.isActive(cluster.id)) { <span>Remove</span>
broadcastMessage("renderer:navigate", landingURL()); </MenuItem> }
clusterStore.setActive(null); </Menu>;
}
await requestMain(clusterDisconnectHandler, cluster.id);
}
}));
if (!cluster.isManaged) {
menu.append(new MenuItem({
label: "Remove",
click: () => {
ConfirmDialog.open({
okButtonProps: {
primary: false,
accent: true,
label: "Remove"
},
ok: () => {
if (clusterStore.activeClusterId === cluster.id) {
broadcastMessage("renderer:navigate", landingURL());
clusterStore.setActive(null);
}
clusterStore.removeById(cluster.id);
},
message: <p>Are you sure want to remove cluster <b title={cluster.id}>{cluster.contextName}</b>?</p>,
});
}
}));
}
}
menu.popup({
window: remote.getCurrentWindow()
});
};
return ( return (
<header className={cssNames("flex gaps align-center justify-space-between", className)}> <header className={cssNames("flex gaps align-center justify-space-between", className)}>
<span className="cluster">{cluster.name}</span> <span className="cluster">{cluster.name}</span>
<Icon <Icon
id={id}
material="more_vert" material="more_vert"
tooltip="Cluster actions" tooltip="Cluster actions"
interactive interactive
onClick={showContextMenu}
/> />
{renderMenu()}
</header> </header>
); );
}); });