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

Demonstrate even step further to have an interface as dependency instead of concrete instance

Signed-off-by: Janne Savolainen <janne.savolainen@houston-inc.com>
This commit is contained in:
Janne Savolainen 2021-11-04 10:09:03 +02:00 committed by Janne Savolainen
parent 91b14ee3fc
commit 29d0634729
2 changed files with 18 additions and 10 deletions

View File

@ -32,10 +32,13 @@ export interface KubeObjectMenuRegistration {
components: KubeObjectMenuComponents;
}
export class KubeObjectMenuRegistry extends BaseRegistry<KubeObjectMenuRegistration> {
getItemsForKind(kind: string, apiVersion: string) {
return this.getItems().filter((item) => {
return item.kind === kind && item.apiVersions.includes(apiVersion);
});
}
export interface IHasGettableItemsForKind {
getItemsForKind(kind: string, apiVersion: string): any;
}
export class KubeObjectMenuRegistry extends BaseRegistry<KubeObjectMenuRegistration> implements IHasGettableItemsForKind {
getItemsForKind = (kind: string, apiVersion: string) =>
this.getItems().filter((item) =>
item.kind === kind && item.apiVersions.includes(apiVersion),
);
}

View File

@ -25,7 +25,10 @@ import type { KubeObject } from "../../../common/k8s-api/kube-object";
import { MenuActions, MenuActionsProps } from "../menu/menu-actions";
import identity from "lodash/identity";
import type { KubeObjectMenuRegistry } from "../../../extensions/registries";
import type {
IHasGettableItemsForKind,
} from "../../../extensions/registries";
import type { CatalogEntityRegistry } from "../../api/catalog-entity-registry";
import type { IGettableStore } from "../../../common/k8s-api/api-manager";
@ -38,10 +41,10 @@ export interface KubeObjectMenuProps<T> extends MenuActionsProps {
interface KubeObjectMenuDependencies<T> extends KubeObjectMenuProps<T>{
apiManager: IGettableStore,
kubeObjectMenuRegistry: IHasGettableItemsForKind
hideDetails: Function,
editResourceTab: Function,
catalogEntityRegistry: CatalogEntityRegistry,
kubeObjectMenuRegistry: KubeObjectMenuRegistry
}
export class KubeObjectMenu<T extends KubeObject> extends React.Component<KubeObjectMenuDependencies<T>> {
@ -125,11 +128,13 @@ export class KubeObjectMenu<T extends KubeObject> extends React.Component<KubeOb
return this.dependencies.kubeObjectMenuRegistry
.getItemsForKind(object.kind, object.apiVersion)
.map(({ components: { MenuItem }}, index) => (
.map(({ components: { MenuItem }}: { components: { MenuItem: React.ReactType<any> }}, index: number) => (
<MenuItem
object={object}
key={`menu-item-${index}`}
toolbar={toolbar}
// TODO: Fix misuse of index in key
key={`menu-item-${index}`}
/>
));
}