Restructure to monorepo (#6907)

* wip: restructure to monorepo

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* refactor create-release-pr to a package

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* build fixes

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* github workflow fixes

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix typo

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* add webpack-env types to core

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix github workflows

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* refactor/fix integration tests

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* lint fix

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* yarn run dev

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* eslint settings for vscode

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* move templates to right package

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* open-lens build fixes

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* integration test fix

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix nx task dependencies

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* use bash shell for unit tests in test workflow

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix test:unit for windows

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix win-ca webpack error in open-lens

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix win-ca webpack error in open-lens

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* fix build:app on windows

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* remove ELECTRON_BUILDER_EXTRA_ARGS

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* sync src/ from master

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* remove Makefile from core

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen authored and GitHub committed 2023-01-24 10:46:26 -08:00
1 parent 0066ca4ea2
commit 2657df2293
3321 files changed
+28699 -11447

No files matched your search

@@ -0,0 +1,74 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import "@testing-library/jest-dom/extend-expect";
import { fireEvent } from "@testing-library/react";
import { SidebarCluster } from "../sidebar-cluster";
import { KubernetesCluster } from "../../../../common/catalog-entities";
import { getDiForUnitTesting } from "../../../getDiForUnitTesting";
import type { DiRender } from "../../test-utils/renderFor";
import { renderFor } from "../../test-utils/renderFor";
import hotbarStoreInjectable from "../../../../common/hotbars/store.injectable";
import type { HotbarStore } from "../../../../common/hotbars/store";
const clusterEntity = new KubernetesCluster({
metadata: {
uid: "test-uid",
name: "test-cluster",
source: "local",
labels: {},
},
spec: {
kubeconfigPath: "",
kubeconfigContext: "",
},
status: {
phase: "connected",
},
});
describe("<SidebarCluster/>", () => {
let render: DiRender;
beforeEach(() => {
const di = getDiForUnitTesting({ doGeneralOverrides: true });
di.override(hotbarStoreInjectable, () => ({
isAddedToActive: () => {},
}) as unknown as HotbarStore);
render = renderFor(di);
});
it("renders w/o errors", () => {
const { container } = render(<SidebarCluster clusterEntity={clusterEntity}/>);
expect(container).toBeInstanceOf(HTMLElement);
});
it("renders cluster avatar and name", () => {
const { getByText, getAllByText } = render(<SidebarCluster clusterEntity={clusterEntity}/>);
expect(getByText("tc")).toBeInTheDocument();
const v = getAllByText("test-cluster");
expect(v.length).toBeGreaterThan(0);
for (const e of v) {
expect(e).toBeInTheDocument();
}
});
it("renders cluster menu", () => {
const { getByTestId, getByText } = render(<SidebarCluster clusterEntity={clusterEntity}/>);
const link = getByTestId("sidebar-cluster-dropdown");
fireEvent.click(link);
expect(getByText("Add to Hotbar")).toBeInTheDocument();
});
});
@@ -0,0 +1,36 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
.closeButton {
width: 35px;
height: 35px;
display: grid;
place-items: center;
cursor: pointer;
border: 2px solid var(--textColorDimmed);
border-radius: 50%;
&:hover {
background-color: #72767d25;
}
&:active {
transform: translateY(1px);
}
}
.icon {
color: var(--textColorAccent);
opacity: 0.6;
}
.esc {
text-align: center;
margin-top: var(--margin);
font-weight: bold;
user-select: none;
color: var(--textColorDimmed);
pointer-events: none;
}
@@ -0,0 +1,30 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import styles from "./close-button.module.scss";
import type { HTMLAttributes } from "react";
import React from "react";
import { Icon } from "../icon";
export interface CloseButtonProps extends HTMLAttributes<HTMLDivElement> {
}
export function CloseButton(props: CloseButtonProps) {
return (
<div {...props}>
<div
className={styles.closeButton}
role="button"
aria-label="Close"
>
<Icon material="close" className={styles.icon}/>
</div>
<div className={styles.esc} aria-hidden="true">
ESC
</div>
</div>
);
}
@@ -0,0 +1,22 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { IconProps } from "../icon";
import type React from "react";
import type { PageTarget } from "../../routes/page-registration";
import type { IComputedValue } from "mobx";
export interface ClusterPageMenuRegistration {
id?: string;
parentId?: string;
target?: PageTarget;
title: React.ReactNode;
components: ClusterPageMenuComponents;
visible?: IComputedValue<boolean>;
}
export interface ClusterPageMenuComponents {
Icon: React.ComponentType<IconProps>;
}
@@ -0,0 +1,98 @@
/**
* 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 React from "react";
import { extensionRegistratorInjectionToken } from "../../../extensions/extension-loader/extension-registrator-injection-token";
import type { SidebarItemRegistration } from "./sidebar-items.injectable";
import { sidebarItemsInjectionToken } from "./sidebar-items.injectable";
import { computed } from "mobx";
import routesInjectable from "../../routes/routes.injectable";
import { matches, noop } from "lodash/fp";
import routeIsActiveInjectable from "../../routes/route-is-active.injectable";
import { navigateToRouteInjectionToken } from "../../../common/front-end-routing/navigate-to-route-injection-token";
import { getExtensionRoutePath } from "../../routes/for-extension";
import type { LensRendererExtension } from "../../../extensions/lens-renderer-extension";
import extensionShouldBeEnabledForClusterFrameInjectable from "../../extension-loader/extension-should-be-enabled-for-cluster-frame.injectable";
const extensionSidebarItemRegistratorInjectable = getInjectable({
id: "extension-sidebar-item-registrator",
instantiate: (di) => (ext) => {
const extension = ext as LensRendererExtension;
const navigateToRoute = di.inject(navigateToRouteInjectionToken);
const routes = di.inject(routesInjectable);
const extensionShouldBeEnabledForClusterFrame = di.inject(extensionShouldBeEnabledForClusterFrameInjectable, extension);
const sidebarItemsForExtensionInjectable = getInjectable({
id: `sidebar-items-for-extension-${extension.sanitizedExtensionId}`,
injectionToken: sidebarItemsInjectionToken,
instantiate: (di) => {
return computed(() => {
const extensionRoutes = routes.get().filter(matches({ extension }));
return extension.clusterPageMenus.map((registration) => {
const targetRoutePath = getExtensionRoutePath(
extension,
registration.target?.pageId,
);
const targetRoute = extensionRoutes.find(
matches({ path: targetRoutePath }),
);
const isVisible = computed(() => {
if (!extensionShouldBeEnabledForClusterFrame.value.get()) {
return false;
}
if (!registration.visible) {
return true;
}
return registration.visible.get();
});
const res: SidebarItemRegistration = {
id: `${extension.sanitizedExtensionId}-${registration.id}`,
orderNumber: 9999,
parentId: registration.parentId
? `${extension.sanitizedExtensionId}-${registration.parentId}`
: null,
isVisible,
title: registration.title,
getIcon: registration.components.Icon
? () => <registration.components.Icon />
: undefined,
...(targetRoute
? {
onClick: () => navigateToRoute(targetRoute),
isActive: di.inject(
routeIsActiveInjectable,
targetRoute,
),
}
: { onClick: noop }),
};
return res;
});
});
},
});
return [
sidebarItemsForExtensionInjectable,
];
},
injectionToken: extensionRegistratorInjectionToken,
});
export default extensionSidebarItemRegistratorInjectable;
@@ -0,0 +1,33 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
.mainLayout {
display: grid;
grid-template-areas:
"sidebar contents"
"sidebar footer";
grid-template-rows: [contents] 1fr [footer] auto;
grid-template-columns: [sidebar] var(--sidebar-width) [contents] 1fr;
width: 100%;
z-index: 1;
height: 100%;
}
.sidebar {
grid-area: sidebar;
display: flex;
position: relative;
background: var(--sidebarBackground);
}
.contents {
overflow: hidden;
}
.footer {
position: relative;
grid-area: footer;
min-width: 0; /* restrict size when overflow content (e.g. <Dock> tabs scrolling) */
}
@@ -0,0 +1,78 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import styles from "./main-layout.module.scss";
import React from "react";
import { observer } from "mobx-react";
import type { StorageLayer } from "../../utils";
import { cssNames } from "../../utils";
import { ErrorBoundary } from "../error-boundary";
import { ResizeDirection, ResizeGrowthDirection, ResizeSide, ResizingAnchor } from "../resizing-anchor";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { SidebarStorageState } from "./sidebar-storage/sidebar-storage.injectable";
import sidebarStorageInjectable, { defaultSidebarWidth } from "./sidebar-storage/sidebar-storage.injectable";
export interface MainLayoutProps {
sidebar: React.ReactNode;
className?: string;
footer?: React.ReactNode;
children?: React.ReactNode | React.ReactNode[];
}
/**
* Main layout is commonly used as a wrapper for "global pages"
*
* @link https://api-docs.k8slens.dev/master/extensions/capabilities/common-capabilities/#global-pages
*/
interface Dependencies {
sidebarStorage: StorageLayer<SidebarStorageState>;
}
@observer
class NonInjectedMainLayout extends React.Component<MainLayoutProps & Dependencies> {
onSidebarResize = (width: number) => {
this.props.sidebarStorage.merge({ width });
};
render() {
const { className, footer, children, sidebar } = this.props;
const { width: sidebarWidth } = this.props.sidebarStorage.get();
const style = { "--sidebar-width": `${sidebarWidth}px` } as React.CSSProperties;
return (
<div className={cssNames(styles.mainLayout, className)} style={style}>
<div className={styles.sidebar}>
{sidebar}
<ResizingAnchor
direction={ResizeDirection.HORIZONTAL}
placement={ResizeSide.TRAILING}
growthDirection={ResizeGrowthDirection.LEFT_TO_RIGHT}
getCurrentExtent={() => sidebarWidth}
onDrag={this.onSidebarResize}
onDoubleClick={() => this.onSidebarResize(defaultSidebarWidth)}
minExtent={150}
maxExtent={400}
/>
</div>
<div className={styles.contents}>
<ErrorBoundary>{children}</ErrorBoundary>
</div>
<div className={styles.footer}>{footer}</div>
</div>
);
}
}
export const MainLayout = withInjectables<Dependencies, MainLayoutProps>(NonInjectedMainLayout, {
getProps: (di, props) => ({
...props,
sidebarStorage: di.inject(sidebarStorageInjectable),
}),
});
@@ -0,0 +1,13 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { SettingLayout } from "./setting-layout";
/**
* PageLayout is deprecated. See MainLayout & SettingLayout
*
* @deprecated
*/
export class PageLayout extends SettingLayout {}
@@ -0,0 +1,177 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
.SettingLayout {
--nav-column-width: 30vw;
width: 100%;
height: 100%;
display: grid;
color: var(--settingsColor);
position: fixed;
z-index: 13!important;
left: 0;
top: 0;
right: 0;
bottom: 0;
height: unset;
background-color: var(--settingsBackground);
&.showNavigation {
grid-template-columns: var(--nav-column-width) 1fr;
> .contentRegion {
justify-content: flex-start;
}
}
> .sidebarRegion {
display: flex;
justify-content: flex-end;
overflow-y: auto;
background-color: var(--secondaryBackground);
.sidebar {
width: 218px;
padding: 60px 10px 60px 20px;
h2 {
font-size: 15px;
overflow-wrap: anywhere;
color: var(--textColorAccent);
font-weight: 600;
padding-right: 20px;
word-break: break-word;
}
hr {
margin-top: 10px;
margin-bottom: 10px;
margin-left: 10px;
margin-right: 20px;
height: 1px;
border-top: thin solid var(--hrColor);
}
.Tabs {
.header {
padding: 6px 10px;
font-size: 13px;
font-weight: 800;
text-transform: uppercase;
color: var(--textColorPrimary);
&:first-child {
padding-top: 0;
}
}
.Tab {
padding: 6px 10px;
margin-bottom: 2px;
border-radius: 4px;
font-size: 15px;
line-height: 20px;
cursor: pointer;
white-space: normal;
&::after {
content: none;
}
&:hover {
background-color: var(--navSelectedBackground);
color: var(--navHoverColor);
}
&.active {
background-color: var(--navSelectedBackground);
}
> .label {
width: 100%;
font-weight: 500;
}
}
}
}
}
> .contentRegion {
display: flex;
overflow: auto;
justify-content: center;
> .content {
width: 100%;
max-width: 740px;
min-width: 460px;
padding: 60px 40px 0;
height: max-content;
margin-bottom: 60px;
}
> .toolsRegion {
width: 45px;
}
}
section {
display: flex;
flex-direction: column;
h1, h2 {
color: var(--textColorAccent);
}
h1 {
color: var(--textColorAccent);
font-size: 22px;
font-weight: bold;
margin-bottom: 10px;
}
h2 {
font-size: 18px;
line-height: 20px;
font-weight: 600;
margin-bottom: 30px;
}
a {
color: var(--colorInfo);
}
.hint {
margin-top: 8px;
font-size: 14px;
line-height: 20px;
}
.SubTitle {
margin-top: 0;
margin-bottom: 8px;
padding-bottom: 0;
font-size: 13px;
line-height: 1;
}
hr {
margin-top: 40px;
margin-bottom: 40px;
height: 1px;
border-top: thin solid var(--hrColor);
&.small {
margin-top: 20px;
margin-bottom: 20px;
}
}
.Checkbox .label {
color: var(--textColorTertiary);
}
}
}
@@ -0,0 +1,110 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import "./setting-layout.scss";
import React from "react";
import { observer } from "mobx-react";
import type { IClassName } from "../../utils";
import { cssNames } from "../../utils";
import { CloseButton } from "./close-button";
import { getLegacyGlobalDiForExtensionApi } from "../../../extensions/as-legacy-globals-for-extension-api/legacy-global-di-for-extension-api";
import navigateToCatalogInjectable from "../../../common/front-end-routing/routes/catalog/navigate-to-catalog.injectable";
import observableHistoryInjectable from "../../navigation/observable-history.injectable";
export interface SettingLayoutProps extends React.DOMAttributes<any> {
className?: IClassName;
contentClass?: IClassName;
provideBackButtonNavigation?: boolean;
contentGaps?: boolean;
navigation?: React.ReactNode;
back?: (evt: React.MouseEvent | KeyboardEvent) => void;
closeButtonProps?: { "data-testid"?: string };
}
const defaultProps: Partial<SettingLayoutProps> = {
closeButtonProps: {},
provideBackButtonNavigation: true,
contentGaps: true,
back: () => {
const di = getLegacyGlobalDiForExtensionApi();
const navigateToCatalog = di.inject(navigateToCatalogInjectable);
const observableHistory = di.inject(observableHistoryInjectable);
if (observableHistory.length <= 1) {
navigateToCatalog();
} else {
observableHistory.goBack();
}
},
};
/**
* Layout for settings like pages with navigation
*/
@observer
export class SettingLayout extends React.Component<SettingLayoutProps> {
static defaultProps = defaultProps as object;
async componentDidMount() {
const { hash } = window.location;
if (hash) {
document.querySelector(hash)?.scrollIntoView();
}
window.addEventListener("keydown", this.onEscapeKey);
}
componentWillUnmount() {
window.removeEventListener("keydown", this.onEscapeKey);
}
onEscapeKey = (evt: KeyboardEvent) => {
if (!this.props.provideBackButtonNavigation) {
return;
}
if (evt.code === "Escape") {
evt.stopPropagation();
this.props.back?.(evt);
}
};
render() {
const {
contentClass, provideBackButtonNavigation,
contentGaps, navigation, children, back, closeButtonProps, ...elemProps
} = this.props;
const className = cssNames("SettingLayout", { showNavigation: navigation }, this.props.className);
return (
<div {...elemProps} className={className}>
{ navigation && (
<nav className="sidebarRegion">
<div className="sidebar">
{navigation}
</div>
</nav>
)}
<div className="contentRegion" id="ScrollSpyRoot">
<div className={cssNames("content", contentClass, contentGaps && "flex column gaps")}>
{children}
</div>
<div className="toolsRegion">
{
this.props.provideBackButtonNavigation && (
<div className="fixed top-[60px]">
<CloseButton onClick={back} {...closeButtonProps}/>
</div>
)
}
</div>
</div>
</div>
);
}
}
@@ -0,0 +1,50 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { withInjectables } from "@ogre-tools/injectable-react";
import type { IComputedValue } from "mobx";
import { observer } from "mobx-react";
import React from "react";
import siblingTabsInjectable from "../../routes/sibling-tabs.injectable";
import { TabLayout } from "./tab-layout-2";
import type { HierarchicalSidebarItem } from "./sidebar-items.injectable";
interface SiblingTabLayoutProps {
children: React.ReactNode;
scrollable?: boolean;
}
interface Dependencies {
tabs: IComputedValue<HierarchicalSidebarItem[]>;
}
const NonInjectedSiblingsInTabLayout = observer(
({ tabs, children, ...other }: Dependencies & SiblingTabLayoutProps) => {
const dereferencedTabs = tabs.get();
if (dereferencedTabs.length) {
return (
<TabLayout
tabs={dereferencedTabs}
{...other}
>
{children}
</TabLayout>
);
}
return <>{children}</>;
},
);
export const SiblingsInTabLayout = withInjectables<Dependencies, SiblingTabLayoutProps>(
NonInjectedSiblingsInTabLayout,
{
getProps: (di, props) => ({
tabs: di.inject(siblingTabsInjectable),
...props,
}),
},
);
@@ -0,0 +1,94 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
.SidebarCluster {
display: flex;
align-items: center;
padding: calc(var(--padding) * 1.2);
cursor: pointer;
&:hover {
background: var(--sidebarLogoBackground);
}
&:focus-visible {
.dropdown {
box-shadow: 0 0 0 2px var(--focus-color);
}
}
.dropdown {
border-radius: 3px;
margin-left: var(--margin);
}
}
.clusterName {
font-weight: bold;
overflow: hidden;
word-break: break-word;
color: var(--textColorAccent);
display: -webkit-box;
/* Simulate text-overflow:ellipsis styles but for multiple text lines */
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
.menu {
width: 200px;
margin-top: -10px;
}
.avatar {
font-weight: 500;
margin-right: calc(var(--margin) * 1.5);
}
.loadingAvatar {
position: relative;
pointer-events: none;
&:after {
content: "";
position: absolute;
left: 0;
top: 0;
width: 0;
height: 100%;
background: transparentize(white, .85);
animation: waiting 1.5s infinite linear;
}
}
.loadingClusterName {
position: relative;
pointer-events: none;
width: 80%;
height: 16px;
&:after {
content: "";
position: absolute;
left: 0;
top: 0;
width: 0;
height: 100%;
background: transparentize(white, .85);
animation: waiting 1.5s infinite linear;
}
}
@keyframes waiting {
0% {
left: 0;
width: 0;
}
50% {
left: 25%;
width: 75%;
}
75% {
left: 100%;
width: 0;
}
}
@@ -0,0 +1,155 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import styles from "./sidebar-cluster.module.scss";
import { observable } from "mobx";
import React, { useState } from "react";
import { broadcastMessage } from "../../../common/ipc";
import type { CatalogEntity, CatalogEntityContextMenu } from "../../api/catalog-entity";
import { IpcRendererNavigationEvents } from "../../../common/ipc/navigation-events";
import { Avatar } from "../avatar";
import { Icon } from "../icon";
import { Menu, MenuItem } from "../menu";
import { Tooltip } from "../tooltip";
import { withInjectables } from "@ogre-tools/injectable-react";
import hotbarStoreInjectable from "../../../common/hotbars/store.injectable";
import type { HotbarStore } from "../../../common/hotbars/store";
import { observer } from "mobx-react";
import type { VisitEntityContextMenu } from "../../../common/catalog/visit-entity-context-menu.injectable";
import visitEntityContextMenuInjectable from "../../../common/catalog/visit-entity-context-menu.injectable";
import type { Navigate } from "../../navigation/navigate.injectable";
import type { NormalizeCatalogEntityContextMenu } from "../../catalog/normalize-menu-item.injectable";
import navigateInjectable from "../../navigation/navigate.injectable";
import normalizeCatalogEntityContextMenuInjectable from "../../catalog/normalize-menu-item.injectable";
export interface SidebarClusterProps {
clusterEntity: CatalogEntity | null | undefined;
}
interface Dependencies {
navigate: Navigate;
normalizeMenuItem: NormalizeCatalogEntityContextMenu;
hotbarStore: HotbarStore;
visitEntityContextMenu: VisitEntityContextMenu;
}
const NonInjectedSidebarCluster = observer(({
clusterEntity,
hotbarStore,
visitEntityContextMenu: onContextMenuOpen,
navigate,
normalizeMenuItem,
}: Dependencies & SidebarClusterProps) => {
const [menuItems] = useState(observable.array<CatalogEntityContextMenu>());
const [opened, setOpened] = useState(false );
if (!clusterEntity) {
// render a Loading version of the SidebarCluster
return (
<div className={styles.SidebarCluster}>
<Avatar
title="??"
background="var(--halfGray)"
size={40}
className={styles.loadingAvatar}
/>
<div className={styles.loadingClusterName} />
</div>
);
}
const onMenuOpen = () => {
const isAddedToActive = hotbarStore.isAddedToActive(clusterEntity);
const title = isAddedToActive
? "Remove from Hotbar"
: "Add to Hotbar";
const onClick = isAddedToActive
? () => hotbarStore.removeFromHotbar(clusterEntity.getId())
: () => hotbarStore.addToHotbar(clusterEntity);
menuItems.replace([{ title, onClick }]);
onContextMenuOpen(clusterEntity, {
menuItems,
navigate: (url, forceMainFrame = true) => {
if (forceMainFrame) {
broadcastMessage(IpcRendererNavigationEvents.NAVIGATE_IN_APP, url);
} else {
navigate(url);
}
},
});
toggle();
};
const onKeyDown = (evt: React.KeyboardEvent<HTMLDivElement>) => {
if (evt.code == "Space") {
toggle();
}
};
const toggle = () => {
setOpened(!opened);
};
const id = `cluster-${clusterEntity.getId()}`;
const tooltipId = `tooltip-${id}`;
return (
<div
id={id}
className={styles.SidebarCluster}
tabIndex={0}
onKeyDown={onKeyDown}
role="menubar"
data-testid="sidebar-cluster-dropdown"
>
<Avatar
title={clusterEntity.getName()}
colorHash={`${clusterEntity.getName()}-${clusterEntity.metadata.source}`}
size={40}
src={clusterEntity.spec.icon?.src}
className={styles.avatar}
/>
<div className={styles.clusterName} id={tooltipId}>
{clusterEntity.getName()}
</div>
<Tooltip targetId={tooltipId}>
{clusterEntity.getName()}
</Tooltip>
<Icon material="arrow_drop_down" className={styles.dropdown} />
<Menu
usePortal
htmlFor={id}
isOpen={opened}
open={onMenuOpen}
closeOnClickItem
closeOnClickOutside
close={toggle}
className={styles.menu}
>
{
menuItems
.map(normalizeMenuItem)
.map((menuItem) => (
<MenuItem key={menuItem.title} onClick={menuItem.onClick}>
{menuItem.title}
</MenuItem>
))
}
</Menu>
</div>
);
});
export const SidebarCluster = withInjectables<Dependencies, SidebarClusterProps>(NonInjectedSidebarCluster, {
getProps: (di, props) => ({
...props,
hotbarStore: di.inject(hotbarStoreInjectable),
visitEntityContextMenu: di.inject(visitEntityContextMenuInjectable),
navigate: di.inject(navigateInjectable),
normalizeMenuItem: di.inject(normalizeCatalogEntityContextMenuInjectable),
}),
});
@@ -0,0 +1,125 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import styles from "./sidebar-items.module.scss";
import React from "react";
import { computed, makeObservable } from "mobx";
import type { StorageLayer } from "../../utils";
import { cssNames } from "../../utils";
import { observer } from "mobx-react";
import { NavLink } from "react-router-dom";
import { Icon } from "../icon";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { SidebarStorageState } from "./sidebar-storage/sidebar-storage.injectable";
import sidebarStorageInjectable from "./sidebar-storage/sidebar-storage.injectable";
import type { HierarchicalSidebarItem } from "./sidebar-items.injectable";
interface Dependencies {
sidebarStorage: StorageLayer<SidebarStorageState>;
}
export interface SidebarItemProps {
item: HierarchicalSidebarItem;
}
@observer
class NonInjectedSidebarItem extends React.Component<
SidebarItemProps & Dependencies
> {
static displayName = "SidebarItem";
constructor(props: SidebarItemProps & Dependencies) {
super(props);
makeObservable(this);
}
get id(): string {
return this.registration.id;
}
@computed get expanded(): boolean {
return Boolean(this.props.sidebarStorage.get().expanded[this.id]);
}
@computed get isExpandable(): boolean {
return this.props.item.children.length > 0;
}
@computed get isActive(): boolean {
return this.props.item.isActive.get();
}
get registration() {
return this.props.item.registration;
}
toggleExpand = () => {
this.props.sidebarStorage.merge((draft) => {
draft.expanded[this.id] = !draft.expanded[this.id];
});
};
renderSubMenu() {
const { isExpandable, expanded } = this;
if (!isExpandable || !expanded) {
return null;
}
return (
<ul className={cssNames(styles.subMenu, { [styles.active]: this.isActive })}>
{this.props.item.children.map(item => <SidebarItem key={item.registration.id} item={item} />)}
</ul>
);
}
render() {
return (
<div
className={styles.SidebarItem}
data-testid={`sidebar-item-${this.id}`}
data-is-active-test={this.isActive}
data-parent-id-test={this.registration.parentId}
>
<NavLink
to={""}
isActive={() => this.isActive}
className={styles.navItem}
onClick={(event) => {
event.preventDefault();
event.stopPropagation();
if (this.isExpandable) {
this.toggleExpand();
} else {
this.registration.onClick();
}
}}
data-testid={`sidebar-item-link-for-${this.id}`}
>
{this.registration.getIcon?.()}
<span>{this.registration.title}</span>
{this.isExpandable && (
<Icon
className={styles.expandIcon}
material={
this.expanded ? "keyboard_arrow_up" : "keyboard_arrow_down"
}
/>
)}
</NavLink>
{this.renderSubMenu()}
</div>
);
}
}
export const SidebarItem = withInjectables<Dependencies, SidebarItemProps>(NonInjectedSidebarItem, {
getProps: (di, props) => ({
...props,
sidebarStorage: di.inject(sidebarStorageInjectable),
}),
});
@@ -0,0 +1,105 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable, getInjectionToken } from "@ogre-tools/injectable";
import type { IComputedValue } from "mobx";
import { computed } from "mobx";
import { pipeline } from "@ogre-tools/fp";
import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx";
import {
filter,
flatMap,
identity,
invokeMap,
isEmpty,
map,
orderBy,
some,
} from "lodash/fp";
export interface SidebarItemRegistration {
id: string;
parentId: string | null;
title: React.ReactNode;
onClick: () => void;
getIcon?: () => React.ReactNode;
isActive?: IComputedValue<boolean>;
isVisible?: IComputedValue<boolean>;
orderNumber: number;
}
export const sidebarItemsInjectionToken = getInjectionToken<
IComputedValue<SidebarItemRegistration[]>
>({ id: "sidebar-items-injection-token" });
export interface HierarchicalSidebarItem {
registration: SidebarItemRegistration;
children: HierarchicalSidebarItem[];
isActive: IComputedValue<boolean>;
}
const sidebarItemsInjectable = getInjectable({
id: "sidebar-items",
instantiate: (di) => {
const computedInjectMany = di.inject(computedInjectManyInjectable);
const sidebarItemRegistrations = computedInjectMany(sidebarItemsInjectionToken);
return computed((): HierarchicalSidebarItem[] => {
const registrations = pipeline(
sidebarItemRegistrations.get(),
flatMap(dereference),
);
const getSidebarItemsHierarchy = (registrations: SidebarItemRegistration[]) => {
const _getSidebarItemsHierarchy = (parentId: string | null): HierarchicalSidebarItem[] =>
pipeline(
registrations,
filter((item) => item.parentId === parentId),
map((registration) => {
const children = _getSidebarItemsHierarchy(registration.id);
return {
registration,
children,
isActive: computed(() => {
if (isEmpty(children)) {
return registration.isActive ? registration.isActive.get() : false;
}
return pipeline(
children,
invokeMap("isActive.get"),
some(identity),
);
}),
};
}),
filter(item => item.registration.isVisible?.get() ?? true),
(items) =>
orderBy(
["registration.orderNumber"],
["asc"],
items,
),
);
return _getSidebarItemsHierarchy(null);
};
return getSidebarItemsHierarchy(registrations);
});
},
});
const dereference = (items: IComputedValue<SidebarItemRegistration[]>) =>
items.get();
export default sidebarItemsInjectable;
@@ -0,0 +1,63 @@
.SidebarItem {
display: flex;
flex-direction: column;
flex-shrink: 0;
width: 100%;
user-select: none;
> .navItem {
display: flex;
gap: 8px;
text-decoration: none;
padding: 6px 8px 6px 10px;
cursor: pointer;
&:global(.active), &:hover {
background: var(--blue);
color: var(--sidebarActiveColor);
}
span {
word-break: break-word;
}
}
.expandIcon {
margin-left: auto;
}
.subMenu {
border-left: 4px solid transparent;
&.active {
border-color: var(--blue);
}
.SidebarItem {
color: var(--textColorPrimary);
padding-left: 25px;
.navItem {
padding-top: 4px;
padding-bottom: 4px;
&:global(.active), &:hover {
color: var(--sidebarSubmenuActiveColor);
background: none;
}
}
}
}
// 3rd menu level
.subMenu .subMenu {
&.active {
border-color: transparent;
}
.SidebarItem {
padding-left: calc(var(--padding) / 2);
}
}
}
@@ -0,0 +1,30 @@
/**
* 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 createStorageInjectable from "../../../utils/create-storage/create-storage.injectable";
export interface SidebarStorageState {
width: number;
expanded: {
[itemId: string]: boolean;
};
}
export const defaultSidebarWidth = 200;
const sidebarStorageInjectable = getInjectable({
id: "sidebar-storage",
instantiate: (di) => {
const createStorage = di.inject(createStorageInjectable);
return createStorage("sidebar", {
width: defaultSidebarWidth,
expanded: {},
});
},
});
export default sidebarStorageInjectable;
@@ -0,0 +1,25 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
.sidebarNav {
@apply flex overflow-auto flex-col;
width: var(--sidebar-width);
padding-bottom: calc(var(--padding) * 3);
/* Shadow above scrolling content from https://gist.github.com/distinctgrey/7548778 */
background:
linear-gradient(var(--sidebarBackground) 30%, rgba(255,255,255,0)),
linear-gradient(rgba(255,255,255,0), var(--sidebarBackground) 70%) 0 100%,
radial-gradient(farthest-side at 50% 0, rgba(0,0,0,.2), rgba(0,0,0,0)),
radial-gradient(farthest-side at 50% 100%, rgba(0,0,0,.2), rgba(0,0,0,0)) 0 100%;
background-repeat: no-repeat;
background-size: 100% 40px, 100% 40px, 100% 12px, 100% 12px;
background-attachment: local, local, scroll, scroll;
}
.sidebarNav :global(.Icon) {
box-sizing: content-box;
border-radius: 50%;
}
@@ -0,0 +1,54 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import styles from "./sidebar.module.scss";
import React from "react";
import { observer } from "mobx-react";
import { cssNames } from "../../utils";
import { SidebarItem } from "./sidebar-item";
import type { CatalogEntityRegistry } from "../../api/catalog/entity/registry";
import { SidebarCluster } from "./sidebar-cluster";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { HierarchicalSidebarItem } from "./sidebar-items.injectable";
import sidebarItemsInjectable from "./sidebar-items.injectable";
import type { IComputedValue } from "mobx";
import catalogEntityRegistryInjectable from "../../api/catalog/entity/registry.injectable";
interface Dependencies {
sidebarItems: IComputedValue<HierarchicalSidebarItem[]>;
entityRegistry: CatalogEntityRegistry;
}
const NonInjectedSidebar = observer(({
sidebarItems,
entityRegistry,
}: Dependencies) => (
<div className={cssNames("flex flex-col")} data-testid="cluster-sidebar">
<SidebarCluster clusterEntity={entityRegistry.activeEntity} />
<div className={`${styles.sidebarNav} sidebar-active-status`}>
{
sidebarItems.get()
.map(hierarchicalSidebarItem => (
<SidebarItem
item={hierarchicalSidebarItem}
key={hierarchicalSidebarItem.registration.id}
/>
))
}
</div>
</div>
));
export const Sidebar = withInjectables<Dependencies>(NonInjectedSidebar, {
getProps: (di, props) => ({
...props,
sidebarItems: di.inject(sidebarItemsInjectable),
entityRegistry: di.inject(catalogEntityRegistryInjectable),
}),
});
Sidebar.displayName = "Sidebar";
@@ -0,0 +1,18 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
.SubHeader {
color: var(--textColorAccent);
font-weight: $font-weight-bold;
text-align: center;
&:not(.compact) {
padding: $padding * 2;
}
&.withLine {
border-bottom: 1px solid var(--borderColor);
}
}
@@ -0,0 +1,34 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import "./sub-header.scss";
import React from "react";
import type { SingleOrMany } from "../../utils";
import { cssNames } from "../../utils";
export interface SubHeaderProps {
className?: string;
withLine?: boolean; // add bottom line
compact?: boolean; // no extra padding around content
children: SingleOrMany<React.ReactNode>;
}
export class SubHeader extends React.Component<SubHeaderProps> {
render() {
const { withLine, compact, children } = this.props;
let { className } = this.props;
className = cssNames("SubHeader", {
withLine,
compact,
}, className);
return (
<div className={className}>
{children}
</div>
);
}
}
@@ -0,0 +1,19 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
.SubTitle {
--sub-title-spacing: #{$margin * 2};
text-transform: uppercase;
font-weight: bold;
&:not(.compact) {
padding-bottom: $margin;
}
&:not(:first-child) {
margin-top: var(--sub-title-spacing);
}
}
@@ -0,0 +1,34 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import "./sub-title.scss";
import React from "react";
import type { SingleOrMany } from "../../utils";
import { cssNames } from "../../utils";
export interface SubTitleProps {
className?: string;
title: React.ReactNode;
compact?: boolean; // no bottom padding
id?: string;
children?: SingleOrMany<React.ReactNode>;
}
export class SubTitle extends React.Component<SubTitleProps> {
render() {
const { className, compact, title, children, id } = this.props;
const classNames = cssNames("SubTitle", className, {
compact,
});
return (
<div className={classNames} id={id}>
{title}
{" "}
{children}
</div>
);
}
}
@@ -0,0 +1,63 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import "./tab-layout.scss";
import React from "react";
import { observer } from "mobx-react";
import { cssNames } from "../../utils";
import { Tab, Tabs } from "../tabs";
import { ErrorBoundary } from "../error-boundary";
import type { HierarchicalSidebarItem } from "./sidebar-items.injectable";
export interface TabLayoutProps {
tabs?: HierarchicalSidebarItem[];
children?: React.ReactNode;
scrollable?: boolean;
}
export const TabLayout = observer(
({
tabs = [],
scrollable,
children,
}: TabLayoutProps) => {
const hasTabs = tabs.length > 0;
return (
<div
className={cssNames("TabLayout")}
data-testid="tab-layout"
>
{hasTabs && (
<Tabs center>
{tabs.map(({ registration, isActive }) => {
const active = isActive.get();
return (
<Tab
onClick={registration.onClick}
key={registration.id}
label={registration.title}
active={active}
data-is-active-test={active}
data-testid={`tab-link-for-${registration.id}`}
value={undefined}
/>
);
})}
</Tabs>
)}
<main className={cssNames({ scrollable })}>
<ErrorBoundary>
{children}
</ErrorBoundary>
</main>
</div>
);
},
);
@@ -0,0 +1,31 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
.TabLayout {
display: flex;
flex-direction: column;
height: 100%;
> .Tabs {
background: var(--layoutTabsBackground);
flex-shrink: 0;
height: 34px;
}
main {
$spacing: $margin * 2;
flex-grow: 1;
overflow-y: hidden;
overflow-x: auto;
margin: $spacing;
&.scrollable {
overflow-y: scroll;
margin-right: 0;
}
}
}
@@ -0,0 +1,100 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import "./tab-layout.scss";
import type { ReactNode } from "react";
import React from "react";
import { matchPath, Redirect, Route, Switch } from "react-router";
import { observer } from "mobx-react";
import type { IClassName } from "../../utils";
import { cssNames } from "../../utils";
import { Tab, Tabs } from "../tabs";
import { ErrorBoundary } from "../error-boundary";
import type { ObservableHistory } from "mobx-observable-history";
import { withInjectables } from "@ogre-tools/injectable-react";
import observableHistoryInjectable from "../../navigation/observable-history.injectable";
import type { Navigate } from "../../navigation/navigate.injectable";
import navigateInjectable from "../../navigation/navigate.injectable";
export interface TabLayoutProps {
className?: IClassName;
contentClass?: IClassName;
tabs?: TabLayoutRoute[];
children?: ReactNode;
scrollable?: boolean;
}
export interface TabLayoutRoute {
routePath: string;
title: React.ReactNode;
component: React.ComponentType<any>;
url?: string; // page-url, if not provided `routePath` is used (doesn't work when path has some :placeholder(s))
exact?: boolean; // route-path matching rule
default?: boolean; // initial tab to open with provided `url, by default tabs[0] is used
}
interface Dependencies {
observableHistory: ObservableHistory<unknown>;
navigate: Navigate;
}
const NonInjectedTabLayout = observer((props: TabLayoutProps & Dependencies) => {
const {
className,
contentClass,
tabs = [],
scrollable,
children,
observableHistory,
navigate,
} = props;
const currentLocation = observableHistory.location.pathname;
const hasTabs = tabs.length > 0;
const startTabUrl = hasTabs ? (tabs.find(tab => tab.default) || tabs[0])?.url : null;
return (
<div className={cssNames("TabLayout", className)}>
{hasTabs && (
<Tabs<string> center onChange={(url) => navigate(url)}>
{tabs.map(({ title, routePath, url = routePath, exact }) => (
<Tab
key={url}
label={title}
value={url}
active={!!matchPath(currentLocation, { path: routePath, exact })}
/>
))}
</Tabs>
)}
<main className={cssNames(contentClass, { scrollable })}>
<ErrorBoundary>
{hasTabs && (
<Switch>
{tabs.map(({ routePath, exact, component }) => (
<Route
key={routePath}
exact={exact}
path={routePath}
component={component}
/>
))}
{startTabUrl && <Redirect to={startTabUrl}/>}
</Switch>
)}
{children}
</ErrorBoundary>
</main>
</div>
);
});
export const TabLayout = withInjectables<Dependencies, TabLayoutProps>(NonInjectedTabLayout, {
getProps: (di, props) => ({
...props,
observableHistory: di.inject(observableHistoryInjectable),
navigate: di.inject(navigateInjectable),
}),
});
@@ -0,0 +1,33 @@
/**
* 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 { action } from "mobx";
import { beforeFrameStartsSecondInjectionToken } from "../../../before-frame-starts/tokens";
import ipcRendererInjectable from "../../../utils/channel/ipc-renderer.injectable";
import topBarStateInjectable from "./state.injectable";
// TODO: replace with a SyncBox
const startTopbarStateSyncInjectable = getInjectable({
id: "start-topbar-state-sync",
instantiate: (di) => ({
id: "start-topbar-state-sync",
run: () => {
const state = di.inject(topBarStateInjectable);
const ipcRenderer = di.inject(ipcRendererInjectable);
ipcRenderer.on("history:can-go-back", action((event, canGoBack: boolean) => {
state.prevEnabled = canGoBack;
}));
ipcRenderer.on("history:can-go-forward", action((event, canGoForward: boolean) => {
state.nextEnabled = canGoForward;
}));
},
}),
injectionToken: beforeFrameStartsSecondInjectionToken,
causesSideEffects: true,
});
export default startTopbarStateSyncInjectable;
@@ -0,0 +1,16 @@
/**
* 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 { observable } from "mobx";
const topBarStateInjectable = getInjectable({
id: "top-bar-state",
instantiate: () => observable.object({
prevEnabled: false,
nextEnabled: false,
}),
});
export default topBarStateInjectable;
@@ -0,0 +1,8 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import toggleMaximizeWindowInjectable from "./toggle-maximize-window.injectable";
import { getGlobalOverrideForFunction } from "../../../../../common/test-utils/get-global-override-for-function";
export default getGlobalOverrideForFunction(toggleMaximizeWindowInjectable);
@@ -0,0 +1,15 @@
/**
* 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 { WindowAction } from "../../../../../common/ipc/window";
import { requestWindowAction } from "../../../../ipc";
const toggleMaximizeWindowInjectable = getInjectable({
id: "toggle-maximize-window",
instantiate: () => () => requestWindowAction(WindowAction.TOGGLE_MAXIMIZE),
causesSideEffects: true,
});
export default toggleMaximizeWindowInjectable;
@@ -0,0 +1,31 @@
/**
* 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 isLinuxInjectable from "../../../../../../common/vars/is-linux.injectable";
import isWindowsInjectable from "../../../../../../common/vars/is-windows.injectable";
import { computed } from "mobx";
import { ContextMenu } from "./context-menu";
import { topBarItemOnLeftSideInjectionToken } from "../top-bar-item-injection-token";
const contextMenuTopBarItemInjectable = getInjectable({
id: "context-menu-top-bar-item",
instantiate: (di) => {
const isWindows = di.inject(isWindowsInjectable);
const isLinux = di.inject(isLinuxInjectable);
return {
id: "context-menu",
isShown: computed(() => isWindows || isLinux),
orderNumber: 10,
Component: ContextMenu,
};
},
injectionToken: topBarItemOnLeftSideInjectionToken,
});
export default contextMenuTopBarItemInjectable;
@@ -0,0 +1,39 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { withInjectables } from "@ogre-tools/injectable-react";
import React from "react";
import styles from "../../top-bar.module.scss";
import openAppContextMenuInjectable from "./open-app-context-menu/open-app-context-menu.injectable";
interface Dependencies {
openAppContextMenu: () => void;
}
const NonInjectedContextMenu = ({ openAppContextMenu }: Dependencies) => (
<div className={styles.winMenu}>
<div onClick={openAppContextMenu} data-testid="window-menu">
<svg
width="12"
height="12"
viewBox="0 0 12 12"
shapeRendering="crispEdges"
>
<path fill="currentColor" d="M0,8.5h12v1H0V8.5z" />
<path fill="currentColor" d="M0,5.5h12v1H0V5.5z" />
<path fill="currentColor" d="M0,2.5h12v1H0V2.5z" />
</svg>
</div>
</div>
);
export const ContextMenu = withInjectables<Dependencies>(
NonInjectedContextMenu,
{
getProps: (di) => ({
openAppContextMenu: di.inject(openAppContextMenuInjectable),
}),
},
);
@@ -0,0 +1,8 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import openAppContextMenuInjectable from "./open-app-context-menu.injectable";
import { getGlobalOverrideForFunction } from "../../../../../../../common/test-utils/get-global-override-for-function";
export default getGlobalOverrideForFunction(openAppContextMenuInjectable);
@@ -0,0 +1,14 @@
/**
* 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 { emitOpenAppMenuAsContextMenu } from "../../../../../../ipc";
const openAppContextMenuInjectable = getInjectable({
id: "open-app-context-menu",
instantiate: () => emitOpenAppMenuAsContextMenu,
causesSideEffects: true,
});
export default openAppContextMenuInjectable;
@@ -0,0 +1,8 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import goBackInjectable from "./go-back.injectable";
import { getGlobalOverrideForFunction } from "../../../../../../../common/test-utils/get-global-override-for-function";
export default getGlobalOverrideForFunction(goBackInjectable);
@@ -0,0 +1,15 @@
/**
* 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 { WindowAction } from "../../../../../../../common/ipc/window";
import { requestWindowAction } from "../../../../../../ipc";
const goBackInjectable = getInjectable({
id: "go-back",
instantiate: () => () => requestWindowAction(WindowAction.GO_BACK),
causesSideEffects: true,
});
export default goBackInjectable;
@@ -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";
import { computed } from "mobx";
import { topBarItemOnLeftSideInjectionToken } from "../top-bar-item-injection-token";
import { NavigationToBack } from "./navigation-to-back";
const navigationToBackTopBarItemInjectable = getInjectable({
id: "navigation-to-back-top-bar-item",
instantiate: () => ({
id: "navigation-to-back",
isShown: computed(() => true),
orderNumber: 30,
Component: NavigationToBack,
}),
injectionToken: topBarItemOnLeftSideInjectionToken,
});
export default navigationToBackTopBarItemInjectable;
@@ -0,0 +1,39 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { IComputedValue } from "mobx";
import { Icon } from "../../../../icon";
import topBarPrevEnabledInjectable from "./prev-enabled.injectable";
import goBackInjectable from "./go-back/go-back.injectable";
import { observer } from "mobx-react";
interface Dependencies {
prevEnabled: IComputedValue<boolean>;
goBack: () => void;
}
const NonInjectedNavigationToBack = observer(({
prevEnabled,
goBack,
}: Dependencies) => (
<Icon
data-testid="history-back"
material="arrow_back"
onClick={goBack}
disabled={!prevEnabled.get()}
/>
));
export const NavigationToBack = withInjectables<Dependencies>(
NonInjectedNavigationToBack,
{
getProps: (di) => ({
prevEnabled: di.inject(topBarPrevEnabledInjectable),
goBack: di.inject(goBackInjectable),
}),
},
);
@@ -0,0 +1,18 @@
/**
* 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 { computed } from "mobx";
import topBarStateInjectable from "../../state.injectable";
const topBarPrevEnabledInjectable = getInjectable({
id: "top-bar-prev-enabled",
instantiate: (di) => {
const state = di.inject(topBarStateInjectable);
return computed(() => state.prevEnabled);
},
});
export default topBarPrevEnabledInjectable;
@@ -0,0 +1,8 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import goForwardInjectable from "./go-forward.injectable";
import { getGlobalOverrideForFunction } from "../../../../../../../common/test-utils/get-global-override-for-function";
export default getGlobalOverrideForFunction(goForwardInjectable);
@@ -0,0 +1,15 @@
/**
* 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 { WindowAction } from "../../../../../../../common/ipc/window";
import { requestWindowAction } from "../../../../../../ipc";
const goForwardInjectable = getInjectable({
id: "go-forward",
instantiate: () => () => requestWindowAction(WindowAction.GO_FORWARD),
causesSideEffects: true,
});
export default goForwardInjectable;
@@ -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";
import { computed } from "mobx";
import { NavigationToForward } from "./navigation-to-forward";
import { topBarItemOnLeftSideInjectionToken } from "../top-bar-item-injection-token";
const navigationToForwardTopBarItemInjectable = getInjectable({
id: "navigation-to-forward-top-bar-item",
instantiate: () => ({
id: "navigation-to-forward",
isShown: computed(() => true),
orderNumber: 40,
Component: NavigationToForward,
}),
injectionToken: topBarItemOnLeftSideInjectionToken,
});
export default navigationToForwardTopBarItemInjectable;
@@ -0,0 +1,39 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { IComputedValue } from "mobx";
import { Icon } from "../../../../icon";
import topBarNextEnabledInjectable from "./next-enabled.injectable";
import goForwardInjectable from "./go-forward/go-forward.injectable";
import { observer } from "mobx-react";
interface Dependencies {
nextEnabled: IComputedValue<boolean>;
goForward: () => void;
}
const NonInjectedNavigationToForward = observer(({
nextEnabled,
goForward,
}: Dependencies) => (
<Icon
data-testid="history-forward"
material="arrow_forward"
onClick={goForward}
disabled={!nextEnabled.get()}
/>
));
export const NavigationToForward = withInjectables<Dependencies>(
NonInjectedNavigationToForward,
{
getProps: (di) => ({
nextEnabled: di.inject(topBarNextEnabledInjectable),
goForward: di.inject(goForwardInjectable),
}),
},
);
@@ -0,0 +1,18 @@
/**
* 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 { computed } from "mobx";
import topBarStateInjectable from "../../state.injectable";
const topBarNextEnabledInjectable = getInjectable({
id: "top-bar-next-enabled",
instantiate: (di) => {
const state = di.inject(topBarStateInjectable);
return computed(() => state.nextEnabled);
},
});
export default topBarNextEnabledInjectable;
@@ -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";
import { computed } from "mobx";
import { NavigationToHome } from "./navigation-to-home";
import { topBarItemOnLeftSideInjectionToken } from "../top-bar-item-injection-token";
const navigationToHomeTopBarItemInjectable = getInjectable({
id: "navigation-to-home-top-bar-item",
instantiate: () => ({
id: "navigation-to-home",
isShown: computed(() => true),
orderNumber: 20,
Component: NavigationToHome,
}),
injectionToken: topBarItemOnLeftSideInjectionToken,
});
export default navigationToHomeTopBarItemInjectable;
@@ -0,0 +1,44 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import { withInjectables } from "@ogre-tools/injectable-react";
import type { IComputedValue } from "mobx";
import { Icon } from "../../../../icon";
import routeIsActiveInjectable from "../../../../../routes/route-is-active.injectable";
import { observer } from "mobx-react";
import welcomeRouteInjectable from "../../../../../../common/front-end-routing/routes/welcome/welcome-route.injectable";
import navigateToWelcomeInjectable from "../../../../../../common/front-end-routing/routes/welcome/navigate-to-welcome.injectable";
interface Dependencies {
disabled: IComputedValue<boolean>;
goHome: () => void;
}
const NonInjectedNavigationToHome = observer(({
disabled,
goHome,
}: Dependencies) => (
<Icon
data-testid="home-button"
material="home"
onClick={goHome}
disabled={disabled.get()}
/>
));
export const NavigationToHome = withInjectables<Dependencies>(
NonInjectedNavigationToHome,
{
getProps: (di) => ({
disabled: di.inject(
routeIsActiveInjectable,
di.inject(welcomeRouteInjectable),
),
goHome: di.inject(navigateToWelcomeInjectable),
}),
},
);
@@ -0,0 +1,22 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type React from "react";
import { getInjectionToken } from "@ogre-tools/injectable";
import type { IComputedValue } from "mobx";
export interface TopBarItem {
id: string;
isShown: IComputedValue<boolean>;
orderNumber: number;
Component: React.ComponentType;
}
export const topBarItemOnRightSideInjectionToken = getInjectionToken<TopBarItem>({
id: "top-bar-item-on-right-side-injection-token",
});
export const topBarItemOnLeftSideInjectionToken = getInjectionToken<TopBarItem>({
id: "top-bar-item-on-left-side-injection-token",
});
@@ -0,0 +1,30 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { pipeline } from "@ogre-tools/fp";
import { getInjectable } from "@ogre-tools/injectable";
import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx";
import { filter, sortBy } from "lodash/fp";
import { computed } from "mobx";
import { topBarItemOnLeftSideInjectionToken } from "./top-bar-item-injection-token";
const topBarItemsOnLeftSideInjectable = getInjectable({
id: "top-bar-items-on-left-side",
instantiate: (di) => {
const computedInjectMany = di.inject(computedInjectManyInjectable);
const items = computedInjectMany(topBarItemOnLeftSideInjectionToken);
return computed(() =>
pipeline(
items.get(),
filter((item) => item.isShown.get()),
sortBy(item => item.orderNumber),
),
);
},
});
export default topBarItemsOnLeftSideInjectable;
@@ -0,0 +1,30 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { pipeline } from "@ogre-tools/fp";
import { getInjectable } from "@ogre-tools/injectable";
import { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx";
import { filter, sortBy } from "lodash/fp";
import { computed } from "mobx";
import { topBarItemOnRightSideInjectionToken } from "./top-bar-item-injection-token";
const topBarItemsOnRightSideInjectable = getInjectable({
id: "top-bar-items-on-right-side",
instantiate: (di) => {
const computedInjectMany = di.inject(computedInjectManyInjectable);
const items = computedInjectMany(topBarItemOnRightSideInjectionToken);
return computed(() =>
pipeline(
items.get(),
filter((item) => item.isShown.get()),
sortBy(item => item.orderNumber),
),
);
},
});
export default topBarItemsOnRightSideInjectable;
@@ -0,0 +1,8 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import closeWindowInjectable from "./close-window.injectable";
import { getGlobalOverrideForFunction } from "../../../../../../../common/test-utils/get-global-override-for-function";
export default getGlobalOverrideForFunction(closeWindowInjectable);
@@ -0,0 +1,15 @@
/**
* 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 { WindowAction } from "../../../../../../../common/ipc/window";
import { requestWindowAction } from "../../../../../../ipc";
const closeWindowInjectable = getInjectable({
id: "close-window",
instantiate: () => () => requestWindowAction(WindowAction.CLOSE),
causesSideEffects: true,
});
export default closeWindowInjectable;
@@ -0,0 +1,8 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import maximizeWindowInjectable from "./maximize-window.injectable";
import { getGlobalOverrideForFunction } from "../../../../../../../common/test-utils/get-global-override-for-function";
export default getGlobalOverrideForFunction(maximizeWindowInjectable);
@@ -0,0 +1,15 @@
/**
* 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 { WindowAction } from "../../../../../../../common/ipc/window";
import { requestWindowAction } from "../../../../../../ipc";
const maximizeWindowInjectable = getInjectable({
id: "maximize-window",
instantiate: () => () => requestWindowAction(WindowAction.MINIMIZE),
causesSideEffects: true,
});
export default maximizeWindowInjectable;
@@ -0,0 +1,30 @@
/**
* 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 { computed } from "mobx";
import { topBarItemOnRightSideInjectionToken } from "../top-bar-item-injection-token";
import { WindowControls } from "./window-controls";
import isWindowsInjectable from "../../../../../../common/vars/is-windows.injectable";
import isLinuxInjectable from "../../../../../../common/vars/is-linux.injectable";
const windowControlsTopBarItemInjectable = getInjectable({
id: "window-controls-top-bar-item",
instantiate: (di) => {
const isWindows = di.inject(isWindowsInjectable);
const isLinux = di.inject(isLinuxInjectable);
return ({
id: "window-controls",
orderNumber: 900,
isShown: computed(() => (isWindows || isLinux)),
Component: WindowControls,
});
},
injectionToken: topBarItemOnRightSideInjectionToken,
});
export default windowControlsTopBarItemInjectable;
@@ -0,0 +1,90 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import { withInjectables } from "@ogre-tools/injectable-react";
import { cssNames } from "../../../../../utils";
import styles from "../../top-bar.module.scss";
import isLinuxInjectable from "../../../../../../common/vars/is-linux.injectable";
import toggleMaximizeWindowInjectable from "../../toggle-maximize-window/toggle-maximize-window.injectable";
import closeWindowInjectable from "./close-window/close-window.injectable";
import maximizeWindowInjectable from "./maximize-window/maximize-window.injectable";
interface Dependencies {
isLinux: boolean;
toggleMaximizeWindow: () => void;
closeWindow: () => void;
minimizeWindow: () => void;
}
const NonInjectedWindowControls = ({
isLinux,
toggleMaximizeWindow,
closeWindow,
minimizeWindow,
}: Dependencies) => (
<div
className={cssNames(styles.windowButtons, {
[styles.linuxButtons]: isLinux,
})}
>
<div
className={styles.minimize}
data-testid="window-minimize"
onClick={minimizeWindow}
>
<svg shapeRendering="crispEdges" viewBox="0 0 12 12">
<rect
fill="currentColor"
width="10"
height="1"
x="1"
y="9" />
</svg>
</div>
<div
className={styles.maximize}
data-testid="window-maximize"
onClick={toggleMaximizeWindow}
>
<svg shapeRendering="crispEdges" viewBox="0 0 12 12">
<rect
width="9"
height="9"
x="1.5"
y="1.5"
fill="none"
stroke="currentColor"
/>
</svg>
</div>
<div
className={styles.close}
data-testid="window-close"
onClick={closeWindow}
>
<svg shapeRendering="crispEdges" viewBox="0 0 12 12">
<polygon
fill="currentColor"
points="11 1.576 6.583 6 11 10.424 10.424 11 6 6.583 1.576 11 1 10.424 5.417 6 1 1.576 1.576 1 6 5.417 10.424 1"
/>
</svg>
</div>
</div>
);
export const WindowControls = withInjectables<Dependencies>(
NonInjectedWindowControls,
{
getProps: (di) => ({
isLinux: di.inject(isLinuxInjectable),
toggleMaximizeWindow: di.inject(toggleMaximizeWindowInjectable),
closeWindow: di.inject(closeWindowInjectable),
minimizeWindow: di.inject(maximizeWindowInjectable),
}),
},
);
@@ -0,0 +1,11 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
interface TopBarComponents {
Item: React.ComponentType;
}
export interface TopBarRegistration {
components: TopBarComponents;
}
@@ -0,0 +1,112 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
.topBar {
background-color: var(--layoutBackground);
z-index: 2;
grid-area: topbar;
height: var(--main-layout-header);
/* Use topbar as draggable region */
user-select: none;
-webkit-app-region: drag;
}
.topBar .items {
justify-content: space-between;
align-items: center;
display: flex;
height: 100%;
padding-left: calc(var(--padding) * 2);
padding-right: calc(var(--padding) * 2);
}
.preventedDragging {
-webkit-app-region: no-drag;
}
.separator {
flex-grow: 1;
}
:global(.is-mac) .topBar {
padding-left: var(--hotbar-width);
}
.winMenu {
width: var(--hotbar-width);
> div {
@apply flex items-center justify-center;
width: 40px;
height: 40px;
&:hover {
background-color: var(--borderFaintColor);
}
&:active {
background-color: var(--borderColor);
}
}
}
.windowButtons {
display: flex;
margin-right: calc(var(--padding) * -2);
> div {
@apply flex items-center justify-center;
width: 40px;
height: 40px;
svg {
width: 12px;
height: 12px;
}
}
&.linuxButtons {
> div {
width: 18px;
height: 18px;
border-radius: 50%;
margin-right: var(--padding);
color: var(--textColorAccent);
svg {
width: 8px;
height: 8px;
}
}
.close {
color: white;
background-color: #e63e02; /* Standard close button bg color on ubuntu */
}
.close:hover {
background-color: #ff5a23;
}
}
}
.minimize,
.maximize {
&:hover {
background-color: var(--borderFaintColor);
}
&:active {
background-color: var(--borderColor);
}
}
.close {
&:hover {
color: white;
background-color: #ef4b4e;
}
}
@@ -0,0 +1,187 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import React from "react";
import { fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import { TopBar } from "./top-bar";
import { getDiForUnitTesting } from "../../../getDiForUnitTesting";
import type { DiContainer } from "@ogre-tools/injectable";
import type { DiRender } from "../../test-utils/renderFor";
import { renderFor } from "../../test-utils/renderFor";
import { computed, observable } from "mobx";
import rendererExtensionsInjectable from "../../../../extensions/renderer-extensions.injectable";
import closeWindowInjectable from "./top-bar-items/window-controls/close-window/close-window.injectable";
import goBackInjectable from "./top-bar-items/navigation-to-back/go-back/go-back.injectable";
import maximizeWindowInjectable from "./top-bar-items/window-controls/maximize-window/maximize-window.injectable";
import openAppContextMenuInjectable from "./top-bar-items/context-menu/open-app-context-menu/open-app-context-menu.injectable";
import toggleMaximizeWindowInjectable from "./toggle-maximize-window/toggle-maximize-window.injectable";
import topBarStateInjectable from "./state.injectable";
import platformInjectable from "../../../../common/vars/platform.injectable";
import goForwardInjectable from "./top-bar-items/navigation-to-forward/go-forward/go-forward.injectable";
import currentlyInClusterFrameInjectable from "../../../routes/currently-in-cluster-frame.injectable";
describe("<TopBar/>", () => {
let di: DiContainer;
let render: DiRender;
let goBack: jest.MockedFunction<() => void>;
let goForward: jest.MockedFunction<() => void>;
let openAppContextMenu: jest.MockedFunction<() => void>;
let closeWindow: jest.MockedFunction<() => void>;
let maximizeWindow: jest.MockedFunction<() => void>;
let toggleMaximizeWindow: jest.MockedFunction<() => void>;
beforeEach(() => {
di = getDiForUnitTesting({ doGeneralOverrides: true });
di.override(rendererExtensionsInjectable, () => computed(() => []));
di.override(openAppContextMenuInjectable, () => openAppContextMenu = jest.fn());
di.override(goBackInjectable, () => goBack = jest.fn());
di.override(goForwardInjectable, () => goForward = jest.fn());
di.override(closeWindowInjectable, () => closeWindow = jest.fn());
di.override(maximizeWindowInjectable, () => maximizeWindow = jest.fn());
di.override(toggleMaximizeWindowInjectable, () => toggleMaximizeWindow = jest.fn());
di.override(currentlyInClusterFrameInjectable, () => false);
render = renderFor(di);
});
describe("with both previous and next history enabled", () => {
beforeEach(() => {
di.override(topBarStateInjectable, () => observable.object({
prevEnabled: true,
nextEnabled: true,
}));
});
it("renders w/o errors", () => {
const { container } = render(<TopBar/>);
expect(container).toBeInstanceOf(HTMLElement);
});
it("renders home button", async () => {
const { findByTestId } = render(<TopBar/>);
expect(await findByTestId("home-button")).toBeInTheDocument();
});
it("renders history arrows", async () => {
const { findByTestId } = render(<TopBar/>);
expect(await findByTestId("history-back")).toBeInTheDocument();
expect(await findByTestId("history-forward")).toBeInTheDocument();
});
it("enables arrow by ipc event", async () => {
const { findByTestId } = render(<TopBar/>);
expect(await findByTestId("history-back")).not.toHaveClass("disabled");
expect(await findByTestId("history-forward")).not.toHaveClass("disabled");
});
it("triggers browser history back and forward", async () => {
const { findByTestId } = render(<TopBar/>);
const prevButton = await findByTestId("history-back");
const nextButton = await findByTestId("history-forward");
fireEvent.click(prevButton);
expect(goBack).toBeCalled();
fireEvent.click(nextButton);
expect(goForward).toBeCalled();
});
describe("on macos", () => {
beforeEach(() => {
di.override(platformInjectable, () => "darwin");
});
it("doesn't show windows title", () => {
const { queryByTestId } = render(<TopBar/>);
expect(queryByTestId("window-menu")).not.toBeInTheDocument();
expect(queryByTestId("window-minimize")).not.toBeInTheDocument();
expect(queryByTestId("window-maximize")).not.toBeInTheDocument();
expect(queryByTestId("window-close")).not.toBeInTheDocument();
});
});
describe("on linux", () => {
beforeEach(() => {
di.override(platformInjectable, () => "linux");
});
it("does show windows title buttons", () => {
const { queryByTestId } = render(<TopBar/>);
expect(queryByTestId("window-menu")).toBeInTheDocument();
expect(queryByTestId("window-minimize")).toBeInTheDocument();
expect(queryByTestId("window-maximize")).toBeInTheDocument();
expect(queryByTestId("window-close")).toBeInTheDocument();
});
it("triggers ipc events on click", () => {
const { getByTestId } = render(<TopBar />);
const menu = getByTestId("window-menu");
const minimize = getByTestId("window-minimize");
const maximize = getByTestId("window-maximize");
const close = getByTestId("window-close");
fireEvent.click(menu);
expect(openAppContextMenu).toBeCalled();
fireEvent.click(minimize);
expect(maximizeWindow).toBeCalled();
fireEvent.click(maximize);
expect(toggleMaximizeWindow).toBeCalled();
fireEvent.click(close);
expect(closeWindow).toBeCalled();
});
});
describe("on windows", () => {
beforeEach(() => {
di.override(platformInjectable, () => "win32");
});
it("does show windows title buttons", () => {
const { queryByTestId } = render(<TopBar/>);
expect(queryByTestId("window-menu")).toBeInTheDocument();
expect(queryByTestId("window-minimize")).toBeInTheDocument();
expect(queryByTestId("window-maximize")).toBeInTheDocument();
expect(queryByTestId("window-close")).toBeInTheDocument();
});
it("triggers ipc events on click", () => {
const { getByTestId } = render(<TopBar />);
const menu = getByTestId("window-menu");
const minimize = getByTestId("window-minimize");
const maximize = getByTestId("window-maximize");
const close = getByTestId("window-close");
fireEvent.click(menu);
expect(openAppContextMenu).toBeCalled();
fireEvent.click(minimize);
expect(maximizeWindow).toBeCalled();
fireEvent.click(maximize);
expect(toggleMaximizeWindow).toBeCalled();
fireEvent.click(close);
expect(closeWindow).toBeCalled();
});
});
});
});
@@ -0,0 +1,79 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import styles from "./top-bar.module.scss";
import React, { useEffect } from "react";
import { observer } from "mobx-react";
import type { IComputedValue } from "mobx";
import { withInjectables } from "@ogre-tools/injectable-react";
import toggleMaximizeWindowInjectable from "./toggle-maximize-window/toggle-maximize-window.injectable";
import watchHistoryStateInjectable from "../../../remote-helpers/watch-history-state.injectable";
import topBarItemsOnRightSideInjectable from "./top-bar-items/top-bar-items-on-right-side.injectable";
import type { TopBarItem } from "./top-bar-items/top-bar-item-injection-token";
import { Map } from "../../map/map";
import Gutter from "../../gutter/gutter";
import topBarItemsOnLeftSideInjectable from "./top-bar-items/top-bar-items-on-left-side.injectable";
interface Dependencies {
itemsOnLeft: IComputedValue<TopBarItem[]>;
itemsOnRight: IComputedValue<TopBarItem[]>;
toggleMaximizeWindow: () => void;
watchHistoryState: () => () => void;
}
const NonInjectedTopBar = observer(
({
itemsOnLeft,
itemsOnRight,
toggleMaximizeWindow,
watchHistoryState,
}: Dependencies) => {
useEffect(() => watchHistoryState(), []);
return (
<div className={styles.topBar} onDoubleClick={toggleMaximizeWindow}>
<div className={styles.items}>
<Map
items={itemsOnLeft.get()}
getSeparator={() => <Gutter size="sm" />}
>
{toItemWhichWorksWithWindowDraggingAndDoubleClicking}
</Map>
<div className={styles.separator} />
<Map
items={itemsOnRight.get()}
getSeparator={() => <Gutter size="sm" />}
>
{toItemWhichWorksWithWindowDraggingAndDoubleClicking}
</Map>
</div>
</div>
);
},
);
export const TopBar = withInjectables<Dependencies>(NonInjectedTopBar, {
getProps: (di) => ({
itemsOnLeft: di.inject(topBarItemsOnLeftSideInjectable),
itemsOnRight: di.inject(topBarItemsOnRightSideInjectable),
toggleMaximizeWindow: di.inject(toggleMaximizeWindowInjectable),
watchHistoryState: di.inject(watchHistoryStateInjectable),
}),
});
const toItemWhichWorksWithWindowDraggingAndDoubleClicking = (
item: TopBarItem,
) => (
<div
className={styles.preventedDragging}
onDoubleClick={(event) => {
return event.stopPropagation();
}}
>
<item.Component />
</div>
);
@@ -0,0 +1,68 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
.WizardLayout {
$spacing: $padding * 2;
position: relative;
padding: $padding * 2;
width: 100%;
height: 100%;
display: grid;
grid-template-columns: 1fr 40%;
> * {
--flex-gap: #{$spacing};
overflow: auto;
padding: $spacing;
}
> .head-col {
position: sticky;
justify-content: space-between;
background-color: var(--clusterMenuBackground);
}
> .content-col {
margin-right: $spacing;
background-color: var(--contentColor);
border-radius: $radius;
> div {
flex: 1;
}
> .error {
border-radius: $radius;
padding: $padding;
background-color: pink;
}
}
> .info-col {
background-color: var(--contentColor);
}
p {
line-height: 140%;
}
a {
color: var(--colorInfo);
}
&.centered {
.content-col {
margin: 0;
> div {
margin: 0 auto;
width: 60%;
min-width: 570px;
max-width: 1000px;
}
}
}
}
@@ -0,0 +1,50 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import "./wizard-layout.scss";
import React from "react";
import { observer } from "mobx-react";
import type { IClassName } from "../../utils";
import { cssNames } from "../../utils";
export interface WizardLayoutProps extends React.DOMAttributes<any> {
className?: IClassName;
header?: React.ReactNode;
headerClass?: IClassName;
contentClass?: IClassName;
infoPanelClass?: IClassName;
infoPanel?: React.ReactNode;
centered?: boolean; // Centering content horizontally
}
@observer
export class WizardLayout extends React.Component<WizardLayoutProps> {
render() {
const {
className, contentClass, infoPanelClass, infoPanel, header, headerClass, centered,
children, ...props
} = this.props;
return (
<div {...props} className={cssNames("WizardLayout", { centered }, className)}>
{header && (
<div className={cssNames("head-col flex gaps align-center", headerClass)}>
{header}
</div>
)}
<div className={cssNames("content-col flex column gaps", contentClass)}>
<div className="flex column gaps">
{children}
</div>
</div>
{infoPanel && (
<div className={cssNames("info-col flex column gaps", infoPanelClass)}>
{infoPanel}
</div>
)}
</div>
);
}
}