diff --git a/src/extensions/common-api/registrations.ts b/src/extensions/common-api/registrations.ts index be3e8d87cd..794f203036 100644 --- a/src/extensions/common-api/registrations.ts +++ b/src/extensions/common-api/registrations.ts @@ -2,7 +2,7 @@ * Copyright (c) OpenLens Authors. All rights reserved. * Licensed under MIT License. See LICENSE in root directory for more information. */ -export type { StatusBarRegistration } from "../../renderer/components/cluster-manager/status-bar-registration"; +export type { StatusBarRegistration } from "../../renderer/components/status-bar/status-bar-registration"; export type { KubeObjectMenuRegistration, KubeObjectMenuComponents } from "../../renderer/components/kube-object-menu/dependencies/kube-object-menu-items/kube-object-menu-registration"; export type { AppPreferenceRegistration, AppPreferenceComponents } from "../../renderer/components/+preferences/app-preferences/app-preference-registration"; export type { KubeObjectDetailRegistration, KubeObjectDetailComponents } from "../registries/kube-object-detail-registry"; diff --git a/src/extensions/lens-renderer-extension.ts b/src/extensions/lens-renderer-extension.ts index 9d69b4485a..b61cd9a2c8 100644 --- a/src/extensions/lens-renderer-extension.ts +++ b/src/extensions/lens-renderer-extension.ts @@ -18,7 +18,7 @@ import type { CommandRegistration } from "../renderer/components/command-palette import type { AppPreferenceRegistration } from "../renderer/components/+preferences/app-preferences/app-preference-registration"; import type { AdditionalCategoryColumnRegistration } from "../renderer/components/+catalog/custom-category-columns"; import type { CustomCategoryViewRegistration } from "../renderer/components/+catalog/custom-views"; -import type { StatusBarRegistration } from "../renderer/components/cluster-manager/status-bar-registration"; +import type { StatusBarRegistration } from "../renderer/components/status-bar/status-bar-registration"; import type { KubeObjectMenuRegistration } from "../renderer/components/kube-object-menu/dependencies/kube-object-menu-items/kube-object-menu-registration"; export class LensRendererExtension extends LensExtension { diff --git a/src/renderer/components/cluster-manager/bottom-bar-items.injectable.ts b/src/renderer/components/cluster-manager/bottom-bar-items.injectable.ts deleted file mode 100644 index 9f32831ca6..0000000000 --- a/src/renderer/components/cluster-manager/bottom-bar-items.injectable.ts +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable"; -import { computed } from "mobx"; -import rendererExtensionsInjectable from "../../../extensions/renderer-extensions.injectable"; -import type { StatusBarRegistration } from "./status-bar-registration"; - -const bottomBarItemsInjectable = getInjectable({ - instantiate: (di) => { - const extensions = di.inject(rendererExtensionsInjectable); - - return computed(() => - extensions - .get() - .flatMap((extension) => extension.statusBarItems) - .sort(leftItemsBeforeRight), - ); - }, - - lifecycle: lifecycleEnum.singleton, -}); - -export default bottomBarItemsInjectable; - -const leftItemsBeforeRight = (firstItem: StatusBarRegistration, secondItem: StatusBarRegistration) => - firstItem.components?.position?.localeCompare(secondItem.components?.position); diff --git a/src/renderer/components/cluster-manager/bottom-bar.module.scss b/src/renderer/components/cluster-manager/bottom-bar.module.scss deleted file mode 100644 index f6857220d5..0000000000 --- a/src/renderer/components/cluster-manager/bottom-bar.module.scss +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ - -.BottomBar { - @apply flex px-2 text-white; - - grid-area: bottom-bar; - background-color: var(--blue); - height: var(--bottom-bar-height); - font-size: var(--font-size-small); -} - -.item { - @apply flex items-center mr-2 h-full px-2 last:mr-0; - - &:hover { - @apply cursor-pointer; - - background-color: #ffffff33; - } -} - -.onLeft + .onRight { - @apply ml-auto; -} - -.onRight {} diff --git a/src/renderer/components/cluster-manager/bottom-bar.tsx b/src/renderer/components/cluster-manager/bottom-bar.tsx deleted file mode 100644 index ecbb1634cc..0000000000 --- a/src/renderer/components/cluster-manager/bottom-bar.tsx +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ - -import styles from "./bottom-bar.module.scss"; - -import React from "react"; -import { observer } from "mobx-react"; -import { cssNames } from "../../utils"; -import { withInjectables } from "@ogre-tools/injectable-react"; -import bottomBarItemsInjectable from "./bottom-bar-items.injectable"; -import type { IComputedValue } from "mobx"; -import type { StatusBarRegistration } from "./status-bar-registration"; - -interface Dependencies { - items: IComputedValue -} - -@observer -class NonInjectedBottomBar extends React.Component { - renderRegisteredItem(registration: StatusBarRegistration) { - const { item } = registration; - - if (item) { - return typeof item === "function" ? item() : item; - } - - return ; - } - - renderRegisteredItems() { - return ( - <> - {this.props.items.get().map((registration, index) => { - if (!registration?.item && !registration?.components?.Item) { - return null; - } - - return ( -
- {this.renderRegisteredItem(registration)} -
- ); - })} - - ); - } - - render() { - return ( -
- {this.renderRegisteredItems()} -
- ); - } -} - -export const BottomBar = withInjectables( - NonInjectedBottomBar, - - { - getProps: (di, props) => ({ - items: di.inject(bottomBarItemsInjectable), - ...props, - }), - }, -); diff --git a/src/renderer/components/cluster-manager/cluster-manager.scss b/src/renderer/components/cluster-manager/cluster-manager.scss index 0c4230f850..e6f5bb43e2 100644 --- a/src/renderer/components/cluster-manager/cluster-manager.scss +++ b/src/renderer/components/cluster-manager/cluster-manager.scss @@ -4,14 +4,13 @@ */ .ClusterManager { - --bottom-bar-height: 21px; --hotbar-width: 75px; display: grid; grid-template-areas: "topbar topbar" "menu main" - "bottom-bar bottom-bar"; + "status-bar status-bar"; grid-template-rows: auto 1fr min-content; grid-template-columns: min-content 1fr; @@ -26,10 +25,6 @@ grid-area: menu; } - .BottomBar { - grid-area: bottom-bar; - } - #lens-views { position: absolute; left: 0; diff --git a/src/renderer/components/cluster-manager/cluster-manager.tsx b/src/renderer/components/cluster-manager/cluster-manager.tsx index f7fe8bb40a..10e1905a0d 100644 --- a/src/renderer/components/cluster-manager/cluster-manager.tsx +++ b/src/renderer/components/cluster-manager/cluster-manager.tsx @@ -8,7 +8,7 @@ import "./cluster-manager.scss"; import React from "react"; import { Redirect, Route, Switch } from "react-router"; import { disposeOnUnmount, observer } from "mobx-react"; -import { BottomBar } from "./bottom-bar"; +import { StatusBar } from "../status-bar/status-bar"; import { Catalog } from "../+catalog"; import { Preferences } from "../+preferences"; import { AddCluster } from "../+add-cluster"; @@ -71,7 +71,7 @@ class NonInjectedClusterManager extends React.Component { - + ); diff --git a/src/renderer/components/cluster-manager/status-bar-registration.ts b/src/renderer/components/cluster-manager/status-bar-registration.ts deleted file mode 100644 index 2faea55cc5..0000000000 --- a/src/renderer/components/cluster-manager/status-bar-registration.ts +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright (c) OpenLens Authors. All rights reserved. - * Licensed under MIT License. See LICENSE in root directory for more information. - */ -interface StatusBarComponents { - Item?: React.ComponentType; - /** - * The side of the bottom bar to place this component. - * - * @default "right" - */ - position?: "left" | "right"; -} - -interface StatusBarRegistrationV2 { - components?: StatusBarComponents; // has to be optional for backwards compatability -} - -export interface StatusBarRegistration extends StatusBarRegistrationV2 { - /** - * @deprecated use components.Item instead - */ - item?: React.ReactNode; -} diff --git a/src/renderer/components/layout/main-layout.module.scss b/src/renderer/components/layout/main-layout.module.scss index 6a8ce8e296..fad743041c 100644 --- a/src/renderer/components/layout/main-layout.module.scss +++ b/src/renderer/components/layout/main-layout.module.scss @@ -25,7 +25,7 @@ .contents { grid-area: contents; overflow: auto; - height: calc(100vh - var(--bottom-bar-height) - var(--main-layout-header)); + height: calc(100vh - var(--status-bar-height) - var(--main-layout-header)); } .footer { diff --git a/src/renderer/components/status-bar/registered-status-bar-items.injectable.tsx b/src/renderer/components/status-bar/registered-status-bar-items.injectable.tsx new file mode 100644 index 0000000000..b99edba382 --- /dev/null +++ b/src/renderer/components/status-bar/registered-status-bar-items.injectable.tsx @@ -0,0 +1,73 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import React from "react"; +import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable"; +import { computed, IComputedValue } from "mobx"; +import type { StatusBarItemProps, StatusBarRegistration } from "./status-bar-registration"; +import statusBarItemsInjectable from "./status-bar-items.injectable"; + +export interface RegisteredStatusBarItems { + right: React.ComponentType[]; + left: React.ComponentType[]; +} + +interface Dependencies { + registrations: IComputedValue; +} + +function getRegisteredStatusBarItems({ registrations }: Dependencies): IComputedValue { + return computed(() => { + const res: RegisteredStatusBarItems = { + left: [], + right: [], + }; + + for (const registration of registrations.get()) { + if (!registration || typeof registration !== "object") { + continue; + } + + if (registration.item) { + const { item } = registration; + + // default for old API is "right" + res.right.push( + () => ( + <> + { + typeof item === "function" + ? item() + : item + } + + ), + ); + } else if (registration.components) { + const { position = "right", Item } = registration.components; + + if (position !== "left" && position !== "right") { + throw new TypeError("StatusBarRegistration.components.position must be either 'right' or 'left'"); + } + + res[position].push(Item); + } + } + + // This is done so that the first ones registered are closest to the corner + res.right.reverse(); + + return res; + }); +} + +const registeredStatusBarItemsInjectable = getInjectable({ + instantiate: (di) => getRegisteredStatusBarItems({ + registrations: di.inject(statusBarItemsInjectable), + }), + + lifecycle: lifecycleEnum.singleton, +}); + +export default registeredStatusBarItemsInjectable; diff --git a/src/renderer/components/status-bar/status-bar-items.injectable.ts b/src/renderer/components/status-bar/status-bar-items.injectable.ts new file mode 100644 index 0000000000..33d04cb0fe --- /dev/null +++ b/src/renderer/components/status-bar/status-bar-items.injectable.ts @@ -0,0 +1,21 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ +import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable"; +import { computed } from "mobx"; +import rendererExtensionsInjectable from "../../../extensions/renderer-extensions.injectable"; + +const statusBarItemsInjectable = getInjectable({ + instantiate: (di) => { + const extensions = di.inject(rendererExtensionsInjectable); + + return computed(() => ( + extensions.get() + .flatMap(ext => ext.statusBarItems) + )); + }, + lifecycle: lifecycleEnum.singleton, +}); + +export default statusBarItemsInjectable; diff --git a/src/renderer/components/status-bar/status-bar-registration.ts b/src/renderer/components/status-bar/status-bar-registration.ts new file mode 100644 index 0000000000..8e7219b953 --- /dev/null +++ b/src/renderer/components/status-bar/status-bar-registration.ts @@ -0,0 +1,41 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +/** + * The props for StatusBar item component + */ +export interface StatusBarItemProps {} + +/** + * The type defining the registration of a status bar item + */ +export interface StatusBarComponents { + /** + * The component for this registrations + */ + Item: React.ComponentType; + + /** + * The side of the bottom bar to place this component. + * + * @default "right" + */ + position?: "left" | "right"; +} + +/** + * The type for registering status bar items from the LensRendererExtension + */ +export interface StatusBarRegistration { + /** + * @deprecated use {@link StatusBarRegistration.components} instead + */ + item?: React.ReactNode | (() => React.ReactNode); + + /** + * The newer API, allows for registering a component instead of a ReactNode + */ + components?: StatusBarComponents; +} diff --git a/src/renderer/components/status-bar/status-bar.module.scss b/src/renderer/components/status-bar/status-bar.module.scss new file mode 100644 index 0000000000..04f07fa31b --- /dev/null +++ b/src/renderer/components/status-bar/status-bar.module.scss @@ -0,0 +1,40 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +.StatusBar { + --status-bar-height: 21px; + color: white; + + grid-area: status-bar; + background-color: var(--blue); + height: var(--status-bar-height); + font-size: var(--font-size-small); + + display: inline-grid; + grid-template-columns: 1fr 1fr; +} + +.leftSide { + display: flex; + align-items: center; +} + +.rightSide { + display: flex; + align-items: center; + justify-content: flex-end; +} + +.item { + height: 100%; + padding: 0 4px; + display: flex; + align-items: center; + + &:hover { + cursor: pointer; + background-color: #ffffff33; + } +} diff --git a/src/renderer/components/cluster-manager/bottom-bar.test.tsx b/src/renderer/components/status-bar/status-bar.test.tsx similarity index 51% rename from src/renderer/components/cluster-manager/bottom-bar.test.tsx rename to src/renderer/components/status-bar/status-bar.test.tsx index 523500629c..6055edf403 100644 --- a/src/renderer/components/cluster-manager/bottom-bar.test.tsx +++ b/src/renderer/components/status-bar/status-bar.test.tsx @@ -5,21 +5,18 @@ import React from "react"; import "@testing-library/jest-dom/extend-expect"; -import { BottomBar } from "./bottom-bar"; +import { StatusBar } from "./status-bar"; import { getDiForUnitTesting } from "../../getDiForUnitTesting"; import type { DiRender } from "../test-utils/renderFor"; import { renderFor } from "../test-utils/renderFor"; -import directoryForUserDataInjectable from "../../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable"; +import { computed, IObservableArray, observable } from "mobx"; +import type { ConfigurableDependencyInjectionContainer } from "@ogre-tools/injectable"; +import statusBarItemsInjectable from "./status-bar-items.injectable"; +import type { StatusBarRegistration } from "./status-bar-registration"; import { LensRendererExtension } from "../../../extensions/lens-renderer-extension"; -import { computed, IObservableArray, observable, runInAction } from "mobx"; +import directoryForUserDataInjectable from "../../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable"; import rendererExtensionsInjectable from "../../../extensions/renderer-extensions.injectable"; -jest.mock("electron", () => ({ - app: { - getPath: () => "/foo", - }, -})); - class SomeTestExtension extends LensRendererExtension { constructor(statusBarItems: IObservableArray) { super({ @@ -36,36 +33,28 @@ class SomeTestExtension extends LensRendererExtension { } } -describe("", () => { +describe("", () => { let render: DiRender; + let di: ConfigurableDependencyInjectionContainer; let statusBarItems: IObservableArray; beforeEach(async () => { - statusBarItems = observable.array([]); - - const someTestExtension = new SomeTestExtension(statusBarItems); - - const di = getDiForUnitTesting({ doGeneralOverrides: true }); + di = getDiForUnitTesting({ doGeneralOverrides: true }); + render = renderFor(di); di.override(directoryForUserDataInjectable, () => "some-directory-for-user-data"); - - di.override(rendererExtensionsInjectable, () => { - return computed(() => [someTestExtension]); - }); - - render = renderFor(di); + di.override(rendererExtensionsInjectable, () => computed(() => [new SomeTestExtension(statusBarItems)])); await di.runSetups(); }); it("renders w/o errors", () => { - const { container } = render(); + const { container } = render(); expect(container).toBeInstanceOf(HTMLElement); }); - it.each([ undefined, "hello", @@ -74,25 +63,21 @@ describe("", () => { [], [{}], {}, - ])("renders w/o errors when .getItems() returns not type compliant (%p)", val => { - runInAction(() => { - statusBarItems.replace([val]); - }); + ])("renders w/o errors when registrations are not type compliant (%p)", val => { + statusBarItems.replace([val]); - expect(() => render()).not.toThrow(); + expect(() => render()).not.toThrow(); }); it("renders items [{item: React.ReactNode}] (4.0.0-rc.1)", () => { const testId = "testId"; const text = "heee"; - runInAction(() => { - statusBarItems.replace([ - { item: {text} }, - ]); - }); + di.override(statusBarItemsInjectable, () => computed(() => [ + { item: {text} }, + ] as StatusBarRegistration[])); - const { getByTestId } = render(); + const { getByTestId } = render(); expect(getByTestId(testId)).toHaveTextContent(text); }); @@ -101,51 +86,47 @@ describe("", () => { const testId = "testId"; const text = "heee"; - runInAction(() => { - statusBarItems.replace([ - { item: () => {text} }, - ]); - }); + statusBarItems.replace([{ + item: () => {text}, + }]); - const { getByTestId } = render(); + const { getByTestId } = render(); expect(getByTestId(testId)).toHaveTextContent(text); }); it("sort positioned items properly", () => { - runInAction(() => { - statusBarItems.replace([ - { - components: { - Item: () =>
right
, - }, + statusBarItems.replace([ + { + components: { + Item: () =>
right1
, }, - { - components: { - Item: () =>
right
, - position: "right", - }, + }, + { + components: { + Item: () =>
right2
, + position: "right", }, - { - components: { - Item: () =>
left
, - position: "left", - }, + }, + { + components: { + Item: () =>
left1
, + position: "left", }, - { - components: { - Item: () =>
left
, - position: "left", - }, + }, + { + components: { + Item: () =>
left2
, + position: "left", }, - ]); - }); + }, + ]); - const { getAllByTestId } = render(); + const { getAllByTestId } = render(); const elems = getAllByTestId("sortedElem"); const positions = elems.map(elem => elem.textContent); - expect(positions).toEqual(["left", "left", "right", "right"]); + expect(positions).toEqual(["left1", "left2", "right2", "right1"]); }); }); diff --git a/src/renderer/components/status-bar/status-bar.tsx b/src/renderer/components/status-bar/status-bar.tsx new file mode 100644 index 0000000000..a9e2f5991b --- /dev/null +++ b/src/renderer/components/status-bar/status-bar.tsx @@ -0,0 +1,49 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import styles from "./status-bar.module.scss"; + +import React from "react"; +import { observer } from "mobx-react"; +import { withInjectables } from "@ogre-tools/injectable-react"; +import registeredStatusBarItemsInjectable, { RegisteredStatusBarItems } from "./registered-status-bar-items.injectable"; +import type { IComputedValue } from "mobx"; + +export interface StatusBarProps {} + +interface Dependencies { + items: IComputedValue +} + +const NonInjectedStatusBar = observer(({ items }: Dependencies & StatusBarProps) => { + const { left, right } = items.get(); + + return ( +
+
+ {left.map((Item, index) => ( +
+ +
+ ))} +
+
+ {right.map((Item, index) => ( +
+ +
+ ))} +
+
+ ); + +}); + +export const StatusBar = withInjectables(NonInjectedStatusBar, { + getProps: (di, props) => ({ + items: di.inject(registeredStatusBarItemsInjectable), + ...props, + }), +});