1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/routes/extension-route-registrator.injectable.tsx
Janne Savolainen d66e6c23c5
Expose reactive ways to hide items in cluster frame using Extension API - PART 7 (#5824)
* Kludge "isEnabledForCluster" work again for cluster pages

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Kludge "isEnabledForCluster" work again for kube object details

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Expose reactive way to hide kube object detail items in Extension API

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Expose reactive way to hide kube object menu items in Extension API

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Expose reactive way to hide kube object status items in Extension API

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Expose reactive way to hide workload overview detail items in Extension API

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Expose reactive way to disable pages in Extension API

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Expose a way to access active cluster from Extension API

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>

* Deprecate "isEnabledForCluster" in favor of individual enabled or visible properties for each registration

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
2022-07-13 13:47:03 -04:00

127 lines
4.4 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { DiContainerForInjection } from "@ogre-tools/injectable";
import { getInjectable } from "@ogre-tools/injectable";
import type { LensRendererExtension } from "../../extensions/lens-renderer-extension";
import { observer } from "mobx-react";
import React from "react";
import { isEmpty, matches } from "lodash/fp";
import type { PageRegistration } from "../../extensions/registries";
import { extensionRegistratorInjectionToken } from "../../extensions/extension-loader/extension-registrator-injection-token";
import { SiblingsInTabLayout } from "../components/layout/siblings-in-tab-layout";
import extensionPageParametersInjectable from "./extension-page-parameters.injectable";
import { routeSpecificComponentInjectionToken } from "./route-specific-component-injection-token";
import type { IComputedValue } from "mobx";
import { computed } from "mobx";
import { frontEndRouteInjectionToken } from "../../common/front-end-routing/front-end-route-injection-token";
import { getExtensionRoutePath } from "./for-extension";
import extensionShouldBeEnabledForClusterFrameInjectable from "../extension-loader/extension-should-be-enabled-for-cluster-frame.injectable";
const extensionRouteRegistratorInjectable = getInjectable({
id: "extension-route-registrator",
instantiate: (di) => {
return (ext) => {
const extension = ext as LensRendererExtension;
const toRouteInjectable = toRouteInjectableFor(di, extension);
const extensionShouldBeEnabledForClusterFrame = di.inject(
extensionShouldBeEnabledForClusterFrameInjectable,
extension,
);
return [
...extension.globalPages.map(
toRouteInjectable(false, (registration) =>
computed(() =>
registration.enabled ? registration.enabled.get() : true,
),
),
),
...extension.clusterPages.map(
toRouteInjectable(true, (registration) =>
computed(() => {
if (!extensionShouldBeEnabledForClusterFrame.value.get()) {
return false;
}
return registration.enabled ? registration.enabled.get() : true;
}),
),
),
].flat();
};
},
injectionToken: extensionRegistratorInjectionToken,
});
export default extensionRouteRegistratorInjectable;
const toRouteInjectableFor =
(
di: DiContainerForInjection,
extension: LensRendererExtension,
) =>
(clusterFrame: boolean, getIsEnabled: (registration: PageRegistration) => IComputedValue<boolean>) =>
(registration: PageRegistration) => {
const routeInjectable = getInjectable({
id: `route-${registration.id}-for-extension-${extension.sanitizedExtensionId}`,
instantiate: () => ({
path: getExtensionRoutePath(extension, registration.id),
clusterFrame,
isEnabled: getIsEnabled(registration),
extension,
}),
injectionToken: frontEndRouteInjectionToken,
});
const normalizedParams = di.inject(extensionPageParametersInjectable, {
extension,
registration,
});
const currentSidebarRegistration = extension.clusterPageMenus.find(
matches({ target: { pageId: registration.id }}),
);
const siblingRegistrations = currentSidebarRegistration?.parentId
? extension.clusterPageMenus.filter(
matches({ parentId: currentSidebarRegistration.parentId }),
)
: [];
const ObserverPage = observer(registration.components.Page);
const Component = () => {
if (isEmpty(siblingRegistrations)) {
return <ObserverPage params={normalizedParams} />;
}
return (
<SiblingsInTabLayout>
<ObserverPage params={normalizedParams} />
</SiblingsInTabLayout>
);
};
const routeSpecificComponentInjectable = getInjectable({
id: `route-${registration.id}-component-for-extension-${extension.sanitizedExtensionId}`,
instantiate: (di) => ({
route: di.inject(routeInjectable),
Component,
}),
injectionToken: routeSpecificComponentInjectionToken,
});
return [routeInjectable, routeSpecificComponentInjectable];
};