diff --git a/src/renderer/components/kube-object-menu/dependencies/kube-object-menu-items/get-kube-object-menu-items.ts b/src/renderer/components/kube-object-menu/dependencies/kube-object-menu-items/get-kube-object-menu-items.ts new file mode 100644 index 0000000000..f027b8667c --- /dev/null +++ b/src/renderer/components/kube-object-menu/dependencies/kube-object-menu-items/get-kube-object-menu-items.ts @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +import type { KubeObjectMenuRegistry } from "../../../../../extensions/registries"; +import type { KubeObject } from "../../../../../common/k8s-api/kube-object"; + +export interface Dependencies { + kubeObjectMenuRegistry: KubeObjectMenuRegistry; +} + +export interface InstantiationParameter { + kubeObject: KubeObject; +} + +export const getKubeObjectMenuItems = ( + { kubeObjectMenuRegistry }: Dependencies, + { kubeObject }: InstantiationParameter, +) => { + if (!kubeObject) { + return []; + } + + return kubeObjectMenuRegistry + .getItemsForKind(kubeObject.kind, kubeObject.apiVersion) + .map(item => item.components.MenuItem); +}; diff --git a/src/renderer/components/kube-object-menu/dependencies/kube-object-menu-items/kube-object-menu-items.injectable.ts b/src/renderer/components/kube-object-menu/dependencies/kube-object-menu-items/kube-object-menu-items.injectable.ts new file mode 100644 index 0000000000..1ede65feeb --- /dev/null +++ b/src/renderer/components/kube-object-menu/dependencies/kube-object-menu-items/kube-object-menu-items.injectable.ts @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2021 OpenLens Authors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +import { Injectable, lifecycleEnum } from "@ogre-tools/injectable"; +import kubeObjectMenuRegistryInjectable from "../kubeObjectMenuRegistry.injectable"; + +import { + InstantiationParameter, + Dependencies, + getKubeObjectMenuItems, +} from "./get-kube-object-menu-items"; + +const kubeObjectMenuItemsInjectable: Injectable< + ReturnType, + Dependencies, + InstantiationParameter +> = { + getDependencies: di => ({ + kubeObjectMenuRegistry: di.inject(kubeObjectMenuRegistryInjectable), + }), + + instantiate: getKubeObjectMenuItems, + + lifecycle: lifecycleEnum.transient, +}; + +export default kubeObjectMenuItemsInjectable; diff --git a/src/renderer/components/kube-object-menu/kube-object-menu.injectable.tsx b/src/renderer/components/kube-object-menu/kube-object-menu.injectable.tsx index e63d994eef..f212cd8734 100644 --- a/src/renderer/components/kube-object-menu/kube-object-menu.injectable.tsx +++ b/src/renderer/components/kube-object-menu/kube-object-menu.injectable.tsx @@ -18,7 +18,6 @@ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - import React from "react"; import { @@ -31,21 +30,24 @@ import type { KubeObject } from "../../../common/k8s-api/kube-object"; import { lifecycleEnum, Injectable } from "@ogre-tools/injectable"; import apiManagerInjectable from "./dependencies/apiManager.injectable"; import clusterNameInjectable from "./dependencies/clusterName.injectable"; -import kubeObjectMenuRegistryInjectable from "./dependencies/kubeObjectMenuRegistry.injectable"; import editResourceTabInjectable from "./dependencies/editResourceTab.injectable"; import hideDetailsInjectable from "./dependencies/hideDetails.injectable"; +import kubeObjectMenuItemsInjectable from "./dependencies/kube-object-menu-items/kube-object-menu-items.injectable"; const KubeObjectMenuInjectable: Injectable< JSX.Element, KubeObjectMenuDependencies, KubeObjectMenuProps > = { - getDependencies: di => ({ + getDependencies: (di, props) => ({ clusterName: di.inject(clusterNameInjectable), apiManager: di.inject(apiManagerInjectable), - kubeObjectMenuRegistry: di.inject(kubeObjectMenuRegistryInjectable), editResourceTab: di.inject(editResourceTabInjectable), hideDetails: di.inject(hideDetailsInjectable), + + kubeObjectMenuItems: di.inject(kubeObjectMenuItemsInjectable, { + kubeObject: props.object, + }), }), instantiate: (dependencies, props) => ( diff --git a/src/renderer/components/kube-object-menu/kube-object-menu.tsx b/src/renderer/components/kube-object-menu/kube-object-menu.tsx index b5a2c6e851..804140bc86 100644 --- a/src/renderer/components/kube-object-menu/kube-object-menu.tsx +++ b/src/renderer/components/kube-object-menu/kube-object-menu.tsx @@ -24,13 +24,11 @@ import { boundMethod, cssNames } from "../../utils"; import type { KubeObject } from "../../../common/k8s-api/kube-object"; import { MenuActions, MenuActionsProps } from "../menu"; import identity from "lodash/identity"; - -import type { KubeObjectMenuRegistry } from "../../../extensions/registries"; import type { ApiManager } from "../../../common/k8s-api/api-manager"; export interface KubeObjectMenuDependencies { apiManager: ApiManager; - kubeObjectMenuRegistry: KubeObjectMenuRegistry; + kubeObjectMenuItems: React.ElementType[]; clusterName: string; hideDetails: () => void; editResourceTab: (kubeObject: TKubeObject) => void; @@ -103,27 +101,14 @@ export class KubeObjectMenu< getMenuItems(): React.ReactChild[] { const { object, toolbar } = this.props; - if (!object) { - return []; - } - - return this.props.kubeObjectMenuRegistry - .getItemsForKind(object.kind, object.apiVersion) - .map( - ( - { - components: { MenuItem }, - }: { components: { MenuItem: React.ReactType }}, - index: number, - ) => ( - - ), - ); + return this.props.kubeObjectMenuItems.map((MenuItem, index) => ( + + )); } render() {