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

Extract menu items as own dependency

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>
Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne Savolainen 2021-11-29 09:40:07 +02:00
parent 667e90ba0e
commit 0f4f8dd8d5
4 changed files with 102 additions and 28 deletions

View File

@ -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);
};

View File

@ -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<typeof getKubeObjectMenuItems>,
Dependencies,
InstantiationParameter
> = {
getDependencies: di => ({
kubeObjectMenuRegistry: di.inject(kubeObjectMenuRegistryInjectable),
}),
instantiate: getKubeObjectMenuItems,
lifecycle: lifecycleEnum.transient,
};
export default kubeObjectMenuItemsInjectable;

View File

@ -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<KubeObject>,
KubeObjectMenuProps<KubeObject>
> = {
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) => (

View File

@ -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<TKubeObject> {
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<any> }},
index: number,
) => (
<MenuItem
object={object}
toolbar={toolbar}
// TODO: Fix misuse of index in key
key={`menu-item-${index}`}
/>
),
);
return this.props.kubeObjectMenuItems.map((MenuItem, index) => (
<MenuItem
object={object}
toolbar={toolbar}
// TODO: Fix misuse of index in key
key={`menu-item-${index}`}
/>
));
}
render() {