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

Merge pull request #4613 from jansav/top-bar-registry

Replace TopBarRegistry with reactive solution
This commit is contained in:
Janne Savolainen 2021-12-31 12:03:51 +02:00 committed by GitHub
commit 58ea0dc822
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 124 additions and 79 deletions

View File

@ -264,7 +264,6 @@ export class ExtensionLoader {
registries.StatusBarRegistry.getInstance().add(extension.statusBarItems), registries.StatusBarRegistry.getInstance().add(extension.statusBarItems),
registries.CommandRegistry.getInstance().add(extension.commands), registries.CommandRegistry.getInstance().add(extension.commands),
registries.CatalogEntityDetailRegistry.getInstance().add(extension.catalogEntityDetailItems), registries.CatalogEntityDetailRegistry.getInstance().add(extension.catalogEntityDetailItems),
registries.TopBarRegistry.getInstance().add(extension.topBarItems),
]; ];
this.events.on("remove", (removedExtension: LensRendererExtension) => { this.events.on("remove", (removedExtension: LensRendererExtension) => {

View File

@ -26,6 +26,7 @@ import type { CatalogEntity } from "../common/catalog";
import type { Disposer } from "../common/utils"; import type { Disposer } from "../common/utils";
import { catalogEntityRegistry, EntityFilter } from "../renderer/api/catalog-entity-registry"; import { catalogEntityRegistry, EntityFilter } from "../renderer/api/catalog-entity-registry";
import { catalogCategoryRegistry, CategoryFilter } from "../renderer/api/catalog-category-registry"; import { catalogCategoryRegistry, CategoryFilter } from "../renderer/api/catalog-category-registry";
import type { TopBarRegistration } from "../renderer/components/layout/top-bar/top-bar-registration";
import type { KubernetesCluster } from "../common/catalog-entities"; import type { KubernetesCluster } from "../common/catalog-entities";
import type { WelcomeMenuRegistration } from "../renderer/components/+welcome/welcome-menu-items/welcome-menu-registration"; import type { WelcomeMenuRegistration } from "../renderer/components/+welcome/welcome-menu-items/welcome-menu-registration";
import type { WelcomeBannerRegistration } from "../renderer/components/+welcome/welcome-banner-items/welcome-banner-registration"; import type { WelcomeBannerRegistration } from "../renderer/components/+welcome/welcome-banner-items/welcome-banner-registration";
@ -45,7 +46,7 @@ export class LensRendererExtension extends LensExtension {
welcomeMenus: WelcomeMenuRegistration[] = []; welcomeMenus: WelcomeMenuRegistration[] = [];
welcomeBanners: WelcomeBannerRegistration[] = []; welcomeBanners: WelcomeBannerRegistration[] = [];
catalogEntityDetailItems: registries.CatalogEntityDetailRegistration<CatalogEntity>[] = []; catalogEntityDetailItems: registries.CatalogEntityDetailRegistration<CatalogEntity>[] = [];
topBarItems: registries.TopBarRegistration[] = []; topBarItems: TopBarRegistration[] = [];
async navigate<P extends object>(pageId?: string, params?: P) { async navigate<P extends object>(pageId?: string, params?: P) {
const { navigate } = await import("../renderer/navigation"); const { navigate } = await import("../renderer/navigation");

View File

@ -32,5 +32,4 @@ export * from "./command-registry";
export * from "./entity-setting-registry"; export * from "./entity-setting-registry";
export * from "./catalog-entity-detail-registry"; export * from "./catalog-entity-detail-registry";
export * from "./workloads-overview-detail-registry"; export * from "./workloads-overview-detail-registry";
export * from "./topbar-registry";
export * from "./protocol-handler"; export * from "./protocol-handler";

View File

@ -24,7 +24,6 @@ import { screen } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect"; import "@testing-library/jest-dom/extend-expect";
import { defaultWidth, Welcome } from "../welcome"; import { defaultWidth, Welcome } from "../welcome";
import { computed } from "mobx"; import { computed } from "mobx";
import { TopBarRegistry } from "../../../../extensions/registries";
import { getDiForUnitTesting } from "../../getDiForUnitTesting"; import { getDiForUnitTesting } from "../../getDiForUnitTesting";
import type { DiRender } from "../../test-utils/renderFor"; import type { DiRender } from "../../test-utils/renderFor";
import { renderFor } from "../../test-utils/renderFor"; import { renderFor } from "../../test-utils/renderFor";
@ -62,12 +61,6 @@ describe("<Welcome/>", () => {
}), }),
]), ]),
); );
TopBarRegistry.createInstance();
});
afterEach(() => {
TopBarRegistry.resetInstance();
}); });
it("renders <Banner /> registered in WelcomeBannerRegistry and hide logo", async () => { it("renders <Banner /> registered in WelcomeBannerRegistry and hide logo", async () => {

View File

@ -39,8 +39,8 @@ import { DeleteClusterDialog } from "../delete-cluster-dialog";
import { reaction } from "mobx"; import { reaction } from "mobx";
import { navigation } from "../../navigation"; import { navigation } from "../../navigation";
import { setEntityOnRouteMatch } from "../../../main/catalog-sources/helpers/general-active-sync"; import { setEntityOnRouteMatch } from "../../../main/catalog-sources/helpers/general-active-sync";
import { TopBar } from "../layout/topbar";
import { catalogURL, getPreviousTabUrl } from "../../../common/routes"; import { catalogURL, getPreviousTabUrl } from "../../../common/routes";
import { TopBar } from "../layout/top-bar/top-bar";
@observer @observer
export class ClusterManager extends React.Component { export class ClusterManager extends React.Component {

View File

@ -0,0 +1,37 @@
/**
* Copyright (c) 2021 OpenLens Authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import { computed } from "mobx";
import rendererExtensionsInjectable from "../../../../../extensions/renderer-extensions.injectable";
const topBarItemsInjectable = getInjectable({
instantiate: (di) => {
const extensions = di.inject(rendererExtensionsInjectable);
return computed(() =>
extensions.get().flatMap((extension) => extension.topBarItems),
);
},
lifecycle: lifecycleEnum.singleton,
});
export default topBarItemsInjectable;

View File

@ -18,10 +18,6 @@
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import type React from "react";
import { BaseRegistry } from "./base-registry";
interface TopBarComponents { interface TopBarComponents {
Item: React.ComponentType; Item: React.ComponentType;
} }
@ -29,6 +25,3 @@ interface TopBarComponents {
export interface TopBarRegistration { export interface TopBarRegistration {
components: TopBarComponents; components: TopBarComponents;
} }
export class TopBarRegistry extends BaseRegistry<TopBarRegistration> {
}

View File

@ -20,23 +20,29 @@
*/ */
import React from "react"; import React from "react";
import { render, fireEvent } from "@testing-library/react"; import { fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect"; import "@testing-library/jest-dom/extend-expect";
import { TopBar } from "../topbar"; import { TopBar } from "./top-bar";
import { TopBarRegistry } from "../../../../extensions/registries";
import { IpcMainWindowEvents } from "../../../../main/window-manager"; import { IpcMainWindowEvents } from "../../../../main/window-manager";
import { broadcastMessage } from "../../../../common/ipc"; import { broadcastMessage } from "../../../../common/ipc";
import * as vars from "../../../../common/vars"; import * as vars from "../../../../common/vars";
import { getDiForUnitTesting } from "../../getDiForUnitTesting";
import { DiRender, renderFor } from "../../test-utils/renderFor";
const mockConfig = vars as { isWindows: boolean, isLinux: boolean }; const mockConfig = vars as { isWindows: boolean; isLinux: boolean };
jest.mock("../../../../common/ipc"); jest.mock("../../../../common/ipc");
jest.mock("../../../../common/vars", () => { jest.mock("../../../../common/vars", () => {
const SemVer = require("semver").SemVer;
const versionStub = new SemVer("1.0.0");
return { return {
__esModule: true, __esModule: true,
isWindows: null, isWindows: null,
isLinux: null, isLinux: null,
appSemVer: versionStub,
}; };
}); });
@ -57,13 +63,13 @@ jest.mock("@electron/remote", () => {
}; };
}); });
describe("<Tobar/> in Windows and Linux", () => { describe("<TopBar/> in Windows and Linux", () => {
beforeEach(() => { let render: DiRender;
TopBarRegistry.createInstance();
});
afterEach(() => { beforeEach(() => {
TopBarRegistry.resetInstance(); const di = getDiForUnitTesting();
render = renderFor(di);
}); });
it("shows window controls on Windows", () => { it("shows window controls on Windows", () => {

View File

@ -20,14 +20,23 @@
*/ */
import React from "react"; import React from "react";
import { render, fireEvent } from "@testing-library/react"; import { fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect"; import "@testing-library/jest-dom/extend-expect";
import { TopBar } from "../topbar"; import { TopBar } from "./top-bar";
import { TopBarRegistry } from "../../../../extensions/registries"; import { getDiForUnitTesting } from "../../getDiForUnitTesting";
import type { ConfigurableDependencyInjectionContainer } from "@ogre-tools/injectable";
import { DiRender, renderFor } from "../../test-utils/renderFor";
import topBarItemsInjectable from "./top-bar-items/top-bar-items.injectable";
import { computed } from "mobx";
jest.mock("../../../../common/vars", () => { jest.mock("../../../../common/vars", () => {
const SemVer = require("semver").SemVer;
const versionStub = new SemVer("1.0.0");
return { return {
isMac: true, isMac: true,
appSemVer: versionStub,
}; };
}); });
@ -76,12 +85,13 @@ jest.mock("@electron/remote", () => {
}); });
describe("<TopBar/>", () => { describe("<TopBar/>", () => {
beforeEach(() => { let di: ConfigurableDependencyInjectionContainer;
TopBarRegistry.createInstance(); let render: DiRender;
});
afterEach(() => { beforeEach(() => {
TopBarRegistry.resetInstance(); di = getDiForUnitTesting();
render = renderFor(di);
}); });
it("renders w/o errors", () => { it("renders w/o errors", () => {
@ -129,13 +139,13 @@ describe("<TopBar/>", () => {
const testId = "testId"; const testId = "testId";
const text = "an item"; const text = "an item";
TopBarRegistry.getInstance().getItems = jest.fn().mockImplementationOnce(() => [ di.override(topBarItemsInjectable, () => computed(() => [
{ {
components: { components: {
Item: () => <span data-testid={testId}>{text}</span>, Item: () => <span data-testid={testId}>{text}</span>,
}, },
}, },
]); ]));
const { getByTestId } = render(<TopBar/>); const { getByTestId } = render(<TopBar/>);

View File

@ -19,22 +19,28 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import styles from "./topbar.module.scss"; import styles from "./top-bar.module.scss";
import React, { useEffect, useMemo, useRef } from "react"; import React, { useEffect, useMemo, useRef } from "react";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import { TopBarRegistry } from "../../../extensions/registries"; import type { IComputedValue } from "mobx";
import { Icon } from "../icon"; import { Icon } from "../../icon";
import { webContents, getCurrentWindow } from "@electron/remote"; import { webContents, getCurrentWindow } from "@electron/remote";
import { observable } from "mobx"; import { observable } from "mobx";
import { broadcastMessage, ipcRendererOn } from "../../../common/ipc"; import { broadcastMessage, ipcRendererOn } from "../../../../common/ipc";
import { watchHistoryState } from "../../remote-helpers/history-updater"; import { watchHistoryState } from "../../../remote-helpers/history-updater";
import { isActiveRoute, navigate } from "../../navigation"; import { isActiveRoute, navigate } from "../../../navigation";
import { catalogRoute, catalogURL } from "../../../common/routes"; import { catalogRoute, catalogURL } from "../../../../common/routes";
import { IpcMainWindowEvents } from "../../../main/window-manager"; import { IpcMainWindowEvents } from "../../../../main/window-manager";
import { isLinux, isWindows } from "../../../common/vars"; import { isLinux, isWindows } from "../../../../common/vars";
import { cssNames } from "../../utils"; import { cssNames } from "../../../utils";
import topBarItemsInjectable from "./top-bar-items/top-bar-items.injectable";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { TopBarRegistration } from "./top-bar-registration";
interface Props extends React.HTMLAttributes<any> { interface Props extends React.HTMLAttributes<any> {}
interface Dependencies {
items: IComputedValue<TopBarRegistration[]>;
} }
const prevEnabled = observable.box(false); const prevEnabled = observable.box(false);
@ -48,34 +54,10 @@ ipcRendererOn("history:can-go-forward", (event, state: boolean) => {
nextEnabled.set(state); nextEnabled.set(state);
}); });
export const TopBar = observer(({ children, ...rest }: Props) => { const NonInjectedTopBar = (({ items, children, ...rest }: Props & Dependencies) => {
const elem = useRef<HTMLDivElement>(); const elem = useRef<HTMLDivElement>();
const window = useMemo(() => getCurrentWindow(), []); const window = useMemo(() => getCurrentWindow(), []);
const renderRegisteredItems = () => {
const items = TopBarRegistry.getInstance().getItems();
if (!Array.isArray(items)) {
return null;
}
return (
<div>
{items.map((registration, index) => {
if (!registration?.components?.Item) {
return null;
}
return (
<div key={index}>
<registration.components.Item />
</div>
);
})}
</div>
);
};
const openContextMenu = () => { const openContextMenu = () => {
broadcastMessage(IpcMainWindowEvents.OPEN_CONTEXT_MENU); broadcastMessage(IpcMainWindowEvents.OPEN_CONTEXT_MENU);
}; };
@ -156,7 +138,7 @@ export const TopBar = observer(({ children, ...rest }: Props) => {
/> />
</div> </div>
<div className={styles.controls}> <div className={styles.controls}>
{renderRegisteredItems()} {renderRegisteredItems(items.get())}
{children} {children}
{(isWindows || isLinux) && ( {(isWindows || isLinux) && (
<div className={cssNames(styles.windowButtons, { [styles.linuxButtons]: isLinux })}> <div className={cssNames(styles.windowButtons, { [styles.linuxButtons]: isLinux })}>
@ -174,3 +156,29 @@ export const TopBar = observer(({ children, ...rest }: Props) => {
</div> </div>
); );
}); });
const renderRegisteredItems = (items: TopBarRegistration[]) => (
<div>
{items.map((registration, index) => {
if (!registration?.components?.Item) {
return null;
}
return (
<div key={index}>
<registration.components.Item />
</div>
);
})}
</div>
);
export const TopBar = withInjectables(observer(NonInjectedTopBar), {
getProps: (di, props) => ({
items: di.inject(topBarItemsInjectable),
...props,
}),
});

View File

@ -34,5 +34,4 @@ export function initRegistries() {
registries.KubeObjectStatusRegistry.createInstance(); registries.KubeObjectStatusRegistry.createInstance();
registries.StatusBarRegistry.createInstance(); registries.StatusBarRegistry.createInstance();
registries.WorkloadsOverviewDetailRegistry.createInstance(); registries.WorkloadsOverviewDetailRegistry.createInstance();
registries.TopBarRegistry.createInstance();
} }