1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/command-palette/command-container.tsx
Alex Andreev c1a2c1e849 Fix: preventing <App/> to render on cluster refresh (#2253)
* Removing @observer decorator from <App/>

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Add observer wrapper to <MainLayoutHeader/>

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Fix eslint claim

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Moving extension route renderers to components

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Clean up

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Removing external observables out from App render()

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Fetching hosted cluster inside Command Palette

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Setting route lists explicitly
To avoid using observable data within tabRoutes arrays

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>

* Review fixes

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
2021-03-04 14:13:32 +02:00

87 lines
2.4 KiB
TypeScript

import "./command-container.scss";
import { action, observable } from "mobx";
import { observer } from "mobx-react";
import React from "react";
import { Dialog } from "../dialog";
import { EventEmitter } from "../../../common/event-emitter";
import { subscribeToBroadcast } from "../../../common/ipc";
import { CommandDialog } from "./command-dialog";
import { CommandRegistration, commandRegistry } from "../../../extensions/registries/command-registry";
import { clusterStore } from "../../../common/cluster-store";
import { workspaceStore } from "../../../common/workspace-store";
export type CommandDialogEvent = {
component: React.ReactElement
};
const commandDialogBus = new EventEmitter<[CommandDialogEvent]>();
export class CommandOverlay {
static open(component: React.ReactElement) {
commandDialogBus.emit({ component });
}
static close() {
commandDialogBus.emit({ component: null });
}
}
@observer
export class CommandContainer extends React.Component<{ clusterId?: string }> {
@observable.ref commandComponent: React.ReactElement;
private escHandler(event: KeyboardEvent) {
if (event.key === "Escape") {
event.stopPropagation();
this.closeDialog();
}
}
@action
private closeDialog() {
this.commandComponent = null;
}
private findCommandById(commandId: string) {
return commandRegistry.getItems().find((command) => command.id === commandId);
}
private runCommand(command: CommandRegistration) {
command.action({
cluster: clusterStore.active,
workspace: workspaceStore.currentWorkspace
});
}
componentDidMount() {
if (this.props.clusterId) {
subscribeToBroadcast(`command-palette:run-action:${this.props.clusterId}`, (event, commandId: string) => {
const command = this.findCommandById(commandId);
if (command) {
this.runCommand(command);
}
});
} else {
subscribeToBroadcast("command-palette:open", () => {
CommandOverlay.open(<CommandDialog />);
});
}
window.addEventListener("keyup", (e) => this.escHandler(e), true);
commandDialogBus.addListener((event) => {
this.commandComponent = event.component;
});
}
render() {
return (
<Dialog isOpen={!!this.commandComponent} animated={false} onClose={() => this.commandComponent = null}>
<div id="command-container">
{this.commandComponent}
</div>
</Dialog>
);
}
}