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

Fix actual application and typings

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2022-06-20 16:55:08 -04:00
parent 450ac378e1
commit c2aa186d16
15 changed files with 75 additions and 53 deletions

View File

@ -75,7 +75,7 @@ describe("navigating between routes", () => {
}); });
it("does not have path parameters", () => { it("does not have path parameters", () => {
const pathParameters = windowDi.inject(routePathParametersInjectable, route); const pathParameters = windowDi.inject(routePathParametersInjectable)(route);
expect(pathParameters.get()).toEqual({}); expect(pathParameters.get()).toEqual({});
}); });
@ -116,7 +116,10 @@ describe("navigating between routes", () => {
}); });
describe("when navigating to route with path parameters", () => { describe("when navigating to route with path parameters", () => {
let route: Route<any>; let route: Route<{
someParameter?: string | undefined;
someOtherParameter?: string | undefined;
}>;
beforeEach(() => { beforeEach(() => {
route = windowDi.inject(routeWithOptionalPathParametersInjectable); route = windowDi.inject(routeWithOptionalPathParametersInjectable);
@ -150,7 +153,7 @@ describe("navigating between routes", () => {
}); });
it("knows path parameters", () => { it("knows path parameters", () => {
const pathParameters = windowDi.inject(routePathParametersInjectable, route); const pathParameters = windowDi.inject(routePathParametersInjectable)(route);
expect(pathParameters.get()).toEqual({ expect(pathParameters.get()).toEqual({
someParameter: "some-value", someParameter: "some-value",
@ -160,7 +163,10 @@ describe("navigating between routes", () => {
}); });
describe("when navigating to route without path parameters", () => { describe("when navigating to route without path parameters", () => {
let route: Route<any>; let route: Route<{
someParameter?: string | undefined;
someOtherParameter?: string | undefined;
}>;
beforeEach(() => { beforeEach(() => {
route = windowDi.inject(routeWithOptionalPathParametersInjectable); route = windowDi.inject(routeWithOptionalPathParametersInjectable);
@ -183,7 +189,7 @@ describe("navigating between routes", () => {
}); });
it("knows path parameters", () => { it("knows path parameters", () => {
const pathParameters = windowDi.inject(routePathParametersInjectable, route); const pathParameters = windowDi.inject(routePathParametersInjectable)(route);
expect(pathParameters.get()).toEqual({ expect(pathParameters.get()).toEqual({
someParameter: undefined, someParameter: undefined,
@ -232,7 +238,7 @@ const routeWithOptionalPathParametersComponentInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const route = di.inject(routeWithOptionalPathParametersInjectable); const route = di.inject(routeWithOptionalPathParametersInjectable);
const pathParameters = di.inject(routePathParametersInjectable, route); const pathParameters = di.inject(routePathParametersInjectable)(route);
return { return {
route, route,

View File

@ -15,8 +15,8 @@ const catalogRouteParametersInjectable = getInjectable({
const pathParameters = di.inject(routePathParametersInjectable)(route); const pathParameters = di.inject(routePathParametersInjectable)(route);
return { return {
group: computed(() => pathParameters.get().group), group: computed(() => pathParameters.get()?.group),
kind: computed(() => pathParameters.get().kind), kind: computed(() => pathParameters.get()?.kind),
}; };
}, },
}); });

View File

@ -15,8 +15,8 @@ const customResourcesRouteParametersInjectable = getInjectable({
const pathParameters = di.inject(routePathParametersInjectable)(route); const pathParameters = di.inject(routePathParametersInjectable)(route);
return { return {
group: computed(() => pathParameters.get().group), group: computed(() => pathParameters.get()?.group),
name: computed(() => pathParameters.get().name), name: computed(() => pathParameters.get()?.name),
}; };
}, },
}); });

View File

@ -24,7 +24,7 @@ const sidebarItemsForDefinitionGroupsInjectable = getInjectable({
const crdRoute = di.inject(customResourcesRouteInjectable); const crdRoute = di.inject(customResourcesRouteInjectable);
const crdRouteIsActive = di.inject(routeIsActiveInjectable, crdRoute); const crdRouteIsActive = di.inject(routeIsActiveInjectable, crdRoute);
const crdListRoute = di.inject(crdListRouteInjectable); const crdListRoute = di.inject(crdListRouteInjectable);
const pathParameters = di.inject(routePathParametersInjectable, crdRoute); const pathParameters = di.inject(routePathParametersInjectable)(crdRoute);
const navigateToCustomResources = di.inject(navigateToCustomResourcesInjectable); const navigateToCustomResources = di.inject(navigateToCustomResourcesInjectable);
return computed((): SidebarItemRegistration[] => { return computed((): SidebarItemRegistration[] => {
@ -51,9 +51,15 @@ const sidebarItemsForDefinitionGroupsInjectable = getInjectable({
onClick: () => navigateToCustomResources(crdPathParameters), onClick: () => navigateToCustomResources(crdPathParameters),
isActive: computed( isActive: computed(
() => () => {
crdRouteIsActive.get() && const params = pathParameters.get();
matches(crdPathParameters, pathParameters.get()),
if (!crdRouteIsActive.get() || !params) {
return false;
}
return matches(crdPathParameters, params);
},
), ),
isVisible: crdListRoute.isEnabled, isVisible: crdListRoute.isEnabled,

View File

@ -15,7 +15,7 @@ const entitySettingsRouteParametersInjectable = getInjectable({
const pathParameters = di.inject(routePathParametersInjectable)(route); const pathParameters = di.inject(routePathParametersInjectable)(route);
return { return {
entityId: computed(() => pathParameters.get().entityId), entityId: computed(() => pathParameters.get()?.entityId),
}; };
}, },
}); });

View File

@ -15,8 +15,8 @@ const helmChartsRouteParametersInjectable = getInjectable({
const pathParameters = di.inject(routePathParametersInjectable)(route); const pathParameters = di.inject(routePathParametersInjectable)(route);
return { return {
chartName: computed(() => pathParameters.get().chartName), chartName: computed(() => pathParameters.get()?.chartName),
repo: computed(() => pathParameters.get().repo), repo: computed(() => pathParameters.get()?.repo),
}; };
}, },
}); });

View File

@ -15,8 +15,8 @@ const helmReleasesRouteParametersInjectable = getInjectable({
const pathParameters = di.inject(routePathParametersInjectable)(route); const pathParameters = di.inject(routePathParametersInjectable)(route);
return { return {
namespace: computed(() => pathParameters.get().namespace), namespace: computed(() => pathParameters.get()?.namespace),
name: computed(() => pathParameters.get().name), name: computed(() => pathParameters.get()?.name),
}; };
}, },
}); });

View File

@ -15,7 +15,7 @@ const portForwardsRouteParametersInjectable = getInjectable({
const pathParameters = di.inject(routePathParametersInjectable)(route); const pathParameters = di.inject(routePathParametersInjectable)(route);
return { return {
forwardport: computed(() => pathParameters.get().forwardport), forwardport: computed(() => pathParameters.get()?.forwardport),
}; };
}, },
}); });

View File

@ -7,14 +7,14 @@ import getClusterByIdInjectable from "../../../common/cluster/get-by-id.injectab
import loggerInjectable from "../../../common/logger.injectable"; import loggerInjectable from "../../../common/logger.injectable";
import sendSetVisibleClusterInjectable from "../../cluster/send-set-visible.injectable"; import sendSetVisibleClusterInjectable from "../../cluster/send-set-visible.injectable";
import { ClusterFrameHandler } from "./cluster-frame-handler"; import { ClusterFrameHandler } from "./cluster-frame-handler";
import clusterFrameParentElementInjectable from "./parent-element.injectable"; import getElementByIdInjectable from "./get-element-by-id.injectable";
const clusterFrameHandlerInjectable = getInjectable({ const clusterFrameHandlerInjectable = getInjectable({
id: "cluster-frame-handler", id: "cluster-frame-handler",
instantiate: (di) => new ClusterFrameHandler({ instantiate: (di) => new ClusterFrameHandler({
getClusterById: di.inject(getClusterByIdInjectable), getClusterById: di.inject(getClusterByIdInjectable),
sendSetVisibleCluster: di.inject(sendSetVisibleClusterInjectable), sendSetVisibleCluster: di.inject(sendSetVisibleClusterInjectable),
parentElem: di.inject(clusterFrameParentElementInjectable), getElementById: di.inject(getElementByIdInjectable),
logger: di.inject(loggerInjectable), logger: di.inject(loggerInjectable),
}), }),
}); });

View File

@ -11,6 +11,7 @@ import assert from "assert";
import type { GetClusterById } from "../../../common/cluster/get-by-id.injectable"; import type { GetClusterById } from "../../../common/cluster/get-by-id.injectable";
import type { SendSetVisibleCluster } from "../../cluster/send-set-visible.injectable"; import type { SendSetVisibleCluster } from "../../cluster/send-set-visible.injectable";
import type { Logger } from "../../../common/logger"; import type { Logger } from "../../../common/logger";
import type { GetElementById } from "./get-element-by-id.injectable";
export interface LensView { export interface LensView {
isLoaded: boolean; isLoaded: boolean;
@ -20,7 +21,7 @@ export interface LensView {
export interface ClusterFrameHandlerDependencies { export interface ClusterFrameHandlerDependencies {
getClusterById: GetClusterById; getClusterById: GetClusterById;
sendSetVisibleCluster: SendSetVisibleCluster; sendSetVisibleCluster: SendSetVisibleCluster;
readonly parentElem: HTMLElement; getElementById: GetElementById;
readonly logger: Logger; readonly logger: Logger;
} }
@ -38,6 +39,7 @@ export class ClusterFrameHandler {
@action @action
public initView(clusterId: ClusterId) { public initView(clusterId: ClusterId) {
const cluster = this.dependencies.getClusterById(clusterId); const cluster = this.dependencies.getClusterById(clusterId);
const parentElem = this.dependencies.getElementById("lens-views");
if (!cluster || this.views.has(clusterId)) { if (!cluster || this.views.has(clusterId)) {
return; return;
@ -57,7 +59,7 @@ export class ClusterFrameHandler {
view.isLoaded = true; view.isLoaded = true;
}), { once: true }); }), { once: true });
this.views.set(clusterId, { frame: iframe, isLoaded: false }); this.views.set(clusterId, { frame: iframe, isLoaded: false });
this.dependencies.parentElem.appendChild(iframe); parentElem.appendChild(iframe);
this.dependencies.logger.info(`[LENS-VIEW]: waiting cluster to be ready, clusterId=${clusterId}`); this.dependencies.logger.info(`[LENS-VIEW]: waiting cluster to be ready, clusterId=${clusterId}`);
@ -79,7 +81,7 @@ export class ClusterFrameHandler {
() => { () => {
this.dependencies.logger.info(`[LENS-VIEW]: remove dashboard, clusterId=${clusterId}`); this.dependencies.logger.info(`[LENS-VIEW]: remove dashboard, clusterId=${clusterId}`);
this.views.delete(clusterId); this.views.delete(clusterId);
this.dependencies.parentElem.removeChild(iframe); parentElem.removeChild(iframe);
dispose(); dispose();
}, },
); );

View File

@ -15,7 +15,7 @@ const clusterViewRouteParametersInjectable = getInjectable({
const pathParameters = di.inject(routePathParametersInjectable)(route); const pathParameters = di.inject(routePathParametersInjectable)(route);
return { return {
clusterId: computed(() => pathParameters.get().clusterId), clusterId: computed(() => pathParameters.get()?.clusterId),
}; };
}, },
}); });

View File

@ -0,0 +1,23 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
export type GetElementById = (id: string) => HTMLElement;
const getElementByIdInjectable = getInjectable({
id: "get-element-by-id",
instantiate: (): GetElementById => (id) => {
const element = document.getElementById(id);
if (!element) {
throw new Error(`Failed to find element: ${id}`);
}
return element;
},
causesSideEffects: true,
});
export default getElementByIdInjectable;

View File

@ -1,20 +0,0 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import assert from "assert";
const clusterFrameParentElementInjectable = getInjectable({
id: "cluster-frame-parent-element",
instantiate: () => {
const elem = document.getElementById("#lens-view");
assert(elem, "DOM with #lens-views must be present");
return elem;
},
causesSideEffects: true,
});
export default clusterFrameParentElementInjectable;

View File

@ -65,7 +65,8 @@ import setupSystemCaInjectable from "./frames/root-frame/setup-system-ca.injecta
import extensionShouldBeEnabledForClusterFrameInjectable from "./extension-loader/extension-should-be-enabled-for-cluster-frame.injectable"; import extensionShouldBeEnabledForClusterFrameInjectable from "./extension-loader/extension-should-be-enabled-for-cluster-frame.injectable";
import { asyncComputed } from "@ogre-tools/injectable-react"; import { asyncComputed } from "@ogre-tools/injectable-react";
import forceUpdateModalRootFrameComponentInjectable from "./application-update/force-update-modal/force-update-modal-root-frame-component.injectable"; import forceUpdateModalRootFrameComponentInjectable from "./application-update/force-update-modal/force-update-modal-root-frame-component.injectable";
import clusterFrameParentElementInjectable from "./components/cluster-manager/parent-element.injectable"; import getElementByIdInjectable from "./components/cluster-manager/get-element-by-id.injectable";
import { getOrInsertWith } from "./utils";
import legacyOnChannelListenInjectable from "./ipc/legacy-channel-listen.injectable"; import legacyOnChannelListenInjectable from "./ipc/legacy-channel-listen.injectable";
import getEntitySettingCommandsInjectable from "./components/command-palette/registered-commands/get-entity-setting-commands.injectable"; import getEntitySettingCommandsInjectable from "./components/command-palette/registered-commands/get-entity-setting-commands.injectable";
import storageSaveDelayInjectable from "./utils/create-storage/storage-save-delay.injectable"; import storageSaveDelayInjectable from "./utils/create-storage/storage-save-delay.injectable";
@ -109,7 +110,11 @@ export const getDiForUnitTesting = (opts: { doGeneralOverrides?: boolean } = {})
di.override(terminalSpawningPoolInjectable, () => document.createElement("div")); di.override(terminalSpawningPoolInjectable, () => document.createElement("div"));
di.override(hostedClusterIdInjectable, () => undefined); di.override(hostedClusterIdInjectable, () => undefined);
di.override(clusterFrameParentElementInjectable, () => document.createElement("div")); di.override(getElementByIdInjectable, () => {
const elements = new Map<string, HTMLElement>();
return (id) => getOrInsertWith(elements, id, () => document.createElement("div"));
});
di.override(getAbsolutePathInjectable, () => getAbsolutePathFake); di.override(getAbsolutePathInjectable, () => getAbsolutePathFake);
di.override(joinPathsInjectable, () => joinPathsFake); di.override(joinPathsInjectable, () => joinPathsFake);

View File

@ -15,15 +15,15 @@ const routePathParametersInjectable = getInjectable({
instantiate: (di) => { instantiate: (di) => {
const currentPath = di.inject(currentPathInjectable); const currentPath = di.inject(currentPathInjectable);
const pathParametersCache = new Map<Route<unknown>, IComputedValue<Partial<Record<string, string>>>>(); const pathParametersCache = new Map<Route<unknown>, IComputedValue<unknown>>();
return <Param>(route: Route<Param>): IComputedValue<Partial<Param>> => ( return <Param>(route: Route<Param>): IComputedValue<Param | null> => (
getOrInsertWith(pathParametersCache, route, () => computed(() => ( getOrInsertWith(pathParametersCache, route, () => computed(() => (
matchPath(currentPath.get(), { matchPath<Param>(currentPath.get(), {
path: route.path, path: route.path,
exact: true, exact: true,
})?.params ?? {} })?.params ?? null
))) ))) as IComputedValue<Param | null>
); );
}, },
}); });