From 6cf0000c25a78571b7382e284302168169430b57 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Thu, 3 Nov 2022 16:58:15 +0300 Subject: [PATCH] Using clusterFrameChildComponentsInjectionToken for specific extension elements Signed-off-by: Alex Andreev --- src/extensions/lens-renderer-extension.ts | 3 +- ...-frame-component-registrator.injectable.ts | 42 +++++++++++++++++++ .../frames/cluster-frame/cluster-frame.tsx | 22 ++++++---- 3 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/renderer/frames/cluster-frame/cluster-frame-component-registrator.injectable.ts diff --git a/src/extensions/lens-renderer-extension.ts b/src/extensions/lens-renderer-extension.ts index d26d2ed88c..d0a2a979e6 100644 --- a/src/extensions/lens-renderer-extension.ts +++ b/src/extensions/lens-renderer-extension.ts @@ -28,12 +28,13 @@ import type { LensRendererExtensionDependencies } from "./lens-extension-set-dep import type { KubeObjectHandlerRegistration } from "../renderer/kube-object/handler"; import type { AppPreferenceTabRegistration } from "../features/preferences/renderer/compliance-for-legacy-extension-api/app-preference-tab-registration"; import type { KubeObjectDetailRegistration } from "../renderer/components/kube-object-details/kube-object-detail-registration"; +import type { ClusterFrameChildComponent } from "../renderer/frames/cluster-frame/cluster-frame-child-component-injection-token"; export class LensRendererExtension extends LensExtension { globalPages: registries.PageRegistration[] = []; clusterPages: registries.PageRegistration[] = []; clusterPageMenus: registries.ClusterPageMenuRegistration[] = []; - clusterModals: registries.ClusterModalRegistration[] = []; + clusterFrameComponents: ClusterFrameChildComponent[] = []; kubeObjectStatusTexts: KubeObjectStatusRegistration[] = []; appPreferences: AppPreferenceRegistration[] = []; appPreferenceTabs: AppPreferenceTabRegistration[] = []; diff --git a/src/renderer/frames/cluster-frame/cluster-frame-component-registrator.injectable.ts b/src/renderer/frames/cluster-frame/cluster-frame-component-registrator.injectable.ts new file mode 100644 index 0000000000..31a2db819d --- /dev/null +++ b/src/renderer/frames/cluster-frame/cluster-frame-component-registrator.injectable.ts @@ -0,0 +1,42 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { pipeline } from "@ogre-tools/fp"; +import { getInjectable } from "@ogre-tools/injectable"; +import { map } from "lodash/fp"; +import { extensionRegistratorInjectionToken } from "../../../extensions/extension-loader/extension-registrator-injection-token"; +import type { ExtensionRegistrator } from "../../../extensions/extension-loader/extension-registrator-injection-token"; +import type { LensRendererExtension } from "../../../extensions/lens-renderer-extension"; +import { clusterFrameChildComponentInjectionToken } from "./cluster-frame-child-component-injection-token"; + +const clusterFrameComponentRegistratorInjectable = getInjectable({ + id: "cluster-frame-component-registrator", + + instantiate: (): ExtensionRegistrator => { + return (ext) => { + const extension = ext as LensRendererExtension; + + return pipeline( + extension.clusterFrameComponents, + + map((clusterFrameComponentRegistration) => { + const id = `${extension.sanitizedExtensionId}-${clusterFrameComponentRegistration.id}`; + + return getInjectable({ + id, + injectionToken: clusterFrameChildComponentInjectionToken, + instantiate: () => ({ + id, + shouldRender: clusterFrameComponentRegistration.shouldRender, + Component: clusterFrameComponentRegistration.Component, + }), + }); + }), + ); + }; + }, + injectionToken: extensionRegistratorInjectionToken, +}); + +export default clusterFrameComponentRegistratorInjectable; diff --git a/src/renderer/frames/cluster-frame/cluster-frame.tsx b/src/renderer/frames/cluster-frame/cluster-frame.tsx index d1ce64683f..a057394da4 100755 --- a/src/renderer/frames/cluster-frame/cluster-frame.tsx +++ b/src/renderer/frames/cluster-frame/cluster-frame.tsx @@ -14,11 +14,13 @@ import subscribeStoresInjectable from "../../kube-watch-api/subscribe-stores.inj import type { ClusterFrameChildComponent } from "./cluster-frame-child-component-injection-token"; import { clusterFrameChildComponentInjectionToken } from "./cluster-frame-child-component-injection-token"; import watchHistoryStateInjectable from "../../remote-helpers/watch-history-state.injectable"; +import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx"; +import type { IComputedValue } from "mobx"; interface Dependencies { namespaceStore: NamespaceStore; subscribeStores: SubscribeStores; - childComponents: ClusterFrameChildComponent[]; + childComponents: IComputedValue; watchHistoryState: () => () => void; } @@ -37,7 +39,7 @@ export const NonInjectedClusterFrame = observer(({ return ( - {childComponents + {childComponents.get() .map((child) => ( {() => (child.shouldRender.get() ? : null) } @@ -48,12 +50,16 @@ export const NonInjectedClusterFrame = observer(({ }); export const ClusterFrame = withInjectables(NonInjectedClusterFrame, { - getProps: di => ({ - namespaceStore: di.inject(namespaceStoreInjectable), - subscribeStores: di.inject(subscribeStoresInjectable), - childComponents: di.injectMany(clusterFrameChildComponentInjectionToken), - watchHistoryState: di.inject(watchHistoryStateInjectable), - }), + getProps: di => { + const computedInjectMany = di.inject(computedInjectManyInjectable); + + return { + namespaceStore: di.inject(namespaceStoreInjectable), + subscribeStores: di.inject(subscribeStoresInjectable), + childComponents: computedInjectMany(clusterFrameChildComponentInjectionToken), + watchHistoryState: di.inject(watchHistoryStateInjectable), + } + }, }); ClusterFrame.displayName = "ClusterFrame";