Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for controlling entity settings visibility during runtime #7816

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { extensionRegistratorInjectionToken } from "../../../extensions/extensio
import type { LensRendererExtension } from "../../../extensions/lens-renderer-extension";
import type { CatalogEntity } from "../../api/catalog-entity";
import { entitySettingInjectionToken } from "./token";
import type { IComputedValue } from "mobx";
import { computed } from "mobx";

export interface EntitySettingViewProps {
entity: CatalogEntity;
Expand All @@ -25,6 +27,7 @@ export interface EntitySettingRegistration {
id?: string;
priority?: number;
group?: string;
visible?: IComputedValue<boolean>;
}

export interface RegisteredEntitySetting {
Expand All @@ -36,6 +39,7 @@ export interface RegisteredEntitySetting {
components: EntitySettingComponents;
source?: string;
group: string;
isShown: IComputedValue<boolean>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call this also visible?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to replicate the existing behavior that was implemented with AppPreferenceTabRegistration. I think it's a convention in this code base for Showable and such

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be optional to not break something..

Copy link
Contributor Author

@jweak jweak Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The EntitySettingRegistration from extension has optional visible. When the extension settings are read the extension settings are converted to RegisteredEntitySetting and then we can make this not optional which is nicer for the users of RegisteredEntitySetting. We default to visible then if the visible property is not set

}

const entitySettingExtensionRegistratorInjectable = getInjectable({
Expand All @@ -59,6 +63,7 @@ const getInjectableForEntitySettingRegistrationFor = (extension: LensRendererExt
id = btoa(title),
priority,
source,
visible,
}: EntitySettingRegistration) => getInjectable({
id: `${extension.manifest.name}:${group}/${kind}:${id}`,
instantiate: () => ({
Expand All @@ -70,6 +75,7 @@ const getInjectableForEntitySettingRegistrationFor = (extension: LensRendererExt
title,
group,
source,
isShown: computed(() => visible?.get() ?? true),
}),
injectionToken: entitySettingInjectionToken,
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ClusterKubeconfig } from "../../cluster-settings/kubeconfig";
import { ClusterNameSetting } from "../../cluster-settings/name-setting";
import type { EntitySettingViewProps } from "../extension-registrator.injectable";
import { entitySettingInjectionToken } from "../token";
import { computed } from "mobx";

interface Dependencies {
getClusterById: GetClusterById;
Expand Down Expand Up @@ -64,6 +65,7 @@ const generalKubernetesClusterEntitySettingsInjectable = getInjectable({
components: {
View: GeneralKubernetesClusterSettings,
},
isShown: computed(() => true),
}),
injectionToken: entitySettingInjectionToken,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ClusterPrometheusSetting } from "../../cluster-settings/prometheus-sett
import { ShowMetricsSetting } from "../../cluster-settings/show-metrics";
import type { EntitySettingViewProps } from "../extension-registrator.injectable";
import { entitySettingInjectionToken } from "../token";
import { computed } from "mobx";

interface Dependencies {
getClusterById: GetClusterById;
Expand Down Expand Up @@ -57,6 +58,7 @@ const metricsKubernetesClusterEntitySettingsInjectable = getInjectable({
components: {
View: MetricsKubernetesClusterSettings,
},
isShown: computed(() => true),
}),
injectionToken: entitySettingInjectionToken,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import getClusterByIdInjectable from "../../../../features/cluster/storage/commo
import { ClusterAccessibleNamespaces } from "../../cluster-settings/accessible-namespaces";
import type { EntitySettingViewProps } from "../extension-registrator.injectable";
import { entitySettingInjectionToken } from "../token";
import { computed } from "mobx";

interface Dependencies {
getClusterById: GetClusterById;
Expand Down Expand Up @@ -48,6 +49,7 @@ const namespaceKubernetesClusterEntitySettingsInjectable = getInjectable({
components: {
View: NamespaceKubernetesClusterSettings,
},
isShown: computed(() => true),
}),
injectionToken: entitySettingInjectionToken,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import getClusterByIdInjectable from "../../../../features/cluster/storage/commo
import { ClusterNodeShellSetting } from "../../cluster-settings/node-shell-setting";
import type { EntitySettingViewProps } from "../extension-registrator.injectable";
import { entitySettingInjectionToken } from "../token";
import { computed } from "mobx";

interface Dependencies {
getClusterById: GetClusterById;
Expand Down Expand Up @@ -48,6 +49,7 @@ const nodeShellKubernetesClusterEntitySettingsInjectable = getInjectable({
components: {
View: NodeShellKubernetesClusterSettings,
},
isShown: computed(() => true),
}),
injectionToken: entitySettingInjectionToken,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import getClusterByIdInjectable from "../../../../features/cluster/storage/commo
import { ClusterProxySetting } from "../../cluster-settings/proxy-setting";
import type { EntitySettingViewProps } from "../extension-registrator.injectable";
import { entitySettingInjectionToken } from "../token";
import { computed } from "mobx";

interface Dependencies {
getClusterById: GetClusterById;
Expand Down Expand Up @@ -48,6 +49,7 @@ const proxyKubernetesClusterEntitySettingsInjectable = getInjectable({
components: {
View: ProxyKubernetesClusterSettings,
},
isShown: computed(() => true),
}),
injectionToken: entitySettingInjectionToken,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import getClusterByIdInjectable from "../../../../features/cluster/storage/commo
import { ClusterLocalTerminalSetting } from "../../cluster-settings/local-terminal-settings";
import type { EntitySettingViewProps } from "../extension-registrator.injectable";
import { entitySettingInjectionToken } from "../token";
import { computed } from "mobx";

interface Dependencies {
getClusterById: GetClusterById;
Expand Down Expand Up @@ -48,6 +49,7 @@ const terminalKubernetesClusterEntitySettingsInjectable = getInjectable({
components: {
View: TerminalKubernetesClusterSettings,
},
isShown: computed(() => true),
}),
injectionToken: entitySettingInjectionToken,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const catalogEntitySettingItemsInjectable = getInjectable({
item.apiVersions.has(entity.apiVersion)
&& item.kind === entity.kind
&& (!item.source || item.source === entity.metadata.source)
&& item.isShown.get()
))
.sort(byOrderNumber)
));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/

import type { ApplicationBuilder } from "../test-utils/get-application-builder";
import { getApplicationBuilder } from "../test-utils/get-application-builder";
import type { FakeExtensionOptions } from "../test-utils/get-extension-fake";
import type { EntitySettingRegistration, RegisteredEntitySetting } from "./extension-registrator.injectable";
import type { DiContainer } from "@ogre-tools/injectable";
import catalogEntitySettingItemsInjectable from "./settings.injectable";
import { KubernetesCluster } from "../../../common/catalog-entities";
import type { IComputedValue, IObservableValue } from "mobx";
import { observable } from "mobx";

describe("entity-settings", () => {
let builder: ApplicationBuilder;
let windowDi: DiContainer;
let items: IComputedValue<RegisteredEntitySetting[]>;

beforeEach(async () => {
builder = getApplicationBuilder();

await builder.render();

windowDi = builder.applicationWindow.only.di;

const entity = new KubernetesCluster({
metadata: {
labels: {},
name: "some-kubernetes-cluster",
uid: "some-entity-id",
},
spec: {
kubeconfigContext: "some-context",
kubeconfigPath: "/some/path/to/kubeconfig",
},
status: {
phase: "connecting",
},
});

items = windowDi.inject(catalogEntitySettingItemsInjectable, entity);

});

it("given extension does not specify entity settings visibility, it will be shown by default", () => {
const entitySetting: EntitySettingRegistration = {
title: "some-title",
kind: "KubernetesCluster",
apiVersions: ["entity.k8slens.dev/v1alpha1"],
components: {
View: () => null,
},
};

const testExtensionOptions: FakeExtensionOptions = {
id: "some-extension",
name: "some-extension-name",

rendererOptions: {
entitySettings: [entitySetting],
},
};
const itemCountBefore = items.get().length;

builder.extensions.enable(testExtensionOptions);

expect(items.get().length).toBe(itemCountBefore + 1);
});

describe("given extension entity settings has visibility set to false", () => {
let settingVisible: IObservableValue<boolean>;

beforeEach(() => {
settingVisible = observable.box(false);
const entitySetting: EntitySettingRegistration = {
title: "some-title",
kind: "KubernetesCluster",
apiVersions: ["entity.k8slens.dev/v1alpha1"],
components: {
View: () => null,
},
visible: settingVisible,
};

const testExtensionOptions: FakeExtensionOptions = {
id: "some-extension",
name: "some-extension-name",

rendererOptions: {
entitySettings: [entitySetting],
},
};

builder.extensions.enable(testExtensionOptions);
});

it("it won't be shown initially", () => {
const settingsItem = items.get().find(item => item.title === "some-title");

expect(settingsItem).toBe(undefined);
});

it("visibility is changed, it is shown", () => {
settingVisible.set(true);
const settingsItem = items.get().find(item => item.title === "some-title");

expect(settingsItem).toBeDefined();
});
});
});