mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
chore: Extract sidebar item injection token into separate package
- Add unit tests to cover custom resources sidebar items Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
1 parent
add1a0a39e
commit
8ac66380ad
113 files changed
+5647
-1679
No files matched your search
+54
-60
@@ -5,8 +5,7 @@
|
||||
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 { sidebarItemInjectionToken } from "@k8slens/cluster-sidebar";
|
||||
import { computed } from "mobx";
|
||||
import routesInjectable from "../../routes/routes.injectable";
|
||||
import { matches, noop } from "lodash/fp";
|
||||
@@ -25,71 +24,66 @@ const extensionSidebarItemRegistratorInjectable = getInjectable({
|
||||
const routes = di.inject(routesInjectable);
|
||||
const extensionShouldBeEnabledForClusterFrame = di.inject(extensionShouldBeEnabledForClusterFrameInjectable, extension);
|
||||
|
||||
const sidebarItemsForExtensionInjectable = getInjectable({
|
||||
id: `sidebar-items-for-extension-${extension.sanitizedExtensionId}`,
|
||||
injectionToken: sidebarItemsInjectionToken,
|
||||
return computed(() => {
|
||||
const extensionRoutes = routes.get().filter(matches({ extension }));
|
||||
|
||||
instantiate: (di) => {
|
||||
return computed(() => {
|
||||
const extensionRoutes = routes.get().filter(matches({ extension }));
|
||||
return extension.clusterPageMenus.map((registration) => {
|
||||
const targetRoutePath = getExtensionRoutePath(
|
||||
extension,
|
||||
registration.target?.pageId,
|
||||
);
|
||||
|
||||
return extension.clusterPageMenus.map((registration) => {
|
||||
const targetRoutePath = getExtensionRoutePath(
|
||||
extension,
|
||||
registration.target?.pageId,
|
||||
);
|
||||
const targetRoute = extensionRoutes.find(
|
||||
matches({ path: targetRoutePath }),
|
||||
);
|
||||
|
||||
const targetRoute = extensionRoutes.find(
|
||||
matches({ path: targetRoutePath }),
|
||||
);
|
||||
const isVisible = computed(() => {
|
||||
if (!extensionShouldBeEnabledForClusterFrame.value.get()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const isVisible = computed(() => {
|
||||
if (!extensionShouldBeEnabledForClusterFrame.value.get()) {
|
||||
return false;
|
||||
}
|
||||
if (!registration.visible) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!registration.visible) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return registration.visible.get();
|
||||
});
|
||||
|
||||
const res: SidebarItemRegistration = {
|
||||
id: `${extension.sanitizedExtensionId}-${registration.id}`,
|
||||
orderNumber: registration.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 registration.visible.get();
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
return [
|
||||
sidebarItemsForExtensionInjectable,
|
||||
];
|
||||
const id = registration.id ?
|
||||
`${extension.sanitizedExtensionId}-${registration.id}`
|
||||
: extension.sanitizedExtensionId;
|
||||
|
||||
return getInjectable({
|
||||
id: `${id}-sidebar-item`,
|
||||
instantiate: () => ({
|
||||
id,
|
||||
orderNumber: registration.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 }),
|
||||
}),
|
||||
injectionToken: sidebarItemInjectionToken,
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
injectionToken: extensionRegistratorInjectionToken,
|
||||
|
||||
@@ -8,7 +8,7 @@ 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";
|
||||
import type { HierarchicalSidebarItem } from "@k8slens/cluster-sidebar";
|
||||
import type { StrictReactNode } from "@k8slens/utilities";
|
||||
|
||||
interface SiblingTabLayoutProps {
|
||||
|
||||
@@ -6,15 +6,13 @@
|
||||
import styles from "./sidebar-items.module.scss";
|
||||
|
||||
import React from "react";
|
||||
import { computed, makeObservable } from "mobx";
|
||||
import { cssNames } from "@k8slens/utilities";
|
||||
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";
|
||||
import type { HierarchicalSidebarItem } from "@k8slens/cluster-sidebar";
|
||||
import type { StorageLayer } from "../../utils/storage-helper";
|
||||
|
||||
interface Dependencies {
|
||||
@@ -25,97 +23,73 @@ export interface SidebarItemProps {
|
||||
item: HierarchicalSidebarItem;
|
||||
}
|
||||
|
||||
@observer
|
||||
class NonInjectedSidebarItem extends React.Component<
|
||||
SidebarItemProps & Dependencies
|
||||
> {
|
||||
static displayName = "SidebarItem";
|
||||
const NonInjectedSidebarItem = observer((props: SidebarItemProps & Dependencies) => {
|
||||
const { item, sidebarStorage } = props;
|
||||
const id = item.id;
|
||||
const expanded = sidebarStorage.get().expanded[id] ?? false;
|
||||
const isExpandable = item.children.length > 0 && item.children.some(item => item.isVisible.get());
|
||||
const isActive = item.isActive.get();
|
||||
|
||||
constructor(props: SidebarItemProps & Dependencies) {
|
||||
super(props);
|
||||
makeObservable(this);
|
||||
}
|
||||
|
||||
get id(): string {
|
||||
return this.item.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 item() {
|
||||
return this.props.item;
|
||||
}
|
||||
|
||||
toggleExpand = () => {
|
||||
this.props.sidebarStorage.merge((draft) => {
|
||||
draft.expanded[this.id] = !draft.expanded[this.id];
|
||||
const toggleExpand = () => {
|
||||
sidebarStorage.merge((draft) => {
|
||||
draft.expanded[id] = !draft.expanded[id];
|
||||
});
|
||||
};
|
||||
|
||||
renderSubMenu() {
|
||||
const { isExpandable, expanded } = this;
|
||||
|
||||
const renderSubMenu = () => {
|
||||
if (!isExpandable || !expanded) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<ul className={cssNames(styles.subMenu, { [styles.active]: this.isActive })}>
|
||||
{this.props.item.children.map(item => <SidebarItem key={item.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.item.parentId}
|
||||
>
|
||||
<NavLink
|
||||
to={""}
|
||||
isActive={() => this.isActive}
|
||||
className={styles.navItem}
|
||||
onClick={(event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
if (this.isExpandable) {
|
||||
this.toggleExpand();
|
||||
} else {
|
||||
this.item.onClick();
|
||||
}
|
||||
}}
|
||||
data-testid={`sidebar-item-link-for-${this.id}`}
|
||||
>
|
||||
{this.item.getIcon?.()}
|
||||
<span>{this.item.title}</span>
|
||||
{this.isExpandable && (
|
||||
<Icon
|
||||
className={styles.expandIcon}
|
||||
material={
|
||||
this.expanded ? "keyboard_arrow_up" : "keyboard_arrow_down"
|
||||
}
|
||||
/>
|
||||
)}
|
||||
</NavLink>
|
||||
{this.renderSubMenu()}
|
||||
<div className={styles.subMenu}>
|
||||
{item.children.map((child) => (
|
||||
<SidebarItem key={child.id} item={child} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
if (!item.isVisible.get()) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className={styles.SidebarItem}
|
||||
data-testid={`sidebar-item-${id}`}
|
||||
data-is-active-test={isActive}
|
||||
data-parent-id-test={item.parentId}
|
||||
>
|
||||
<NavLink
|
||||
to={""}
|
||||
isActive={() => isActive}
|
||||
className={styles.navItem}
|
||||
onClick={(event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
if (isExpandable) {
|
||||
toggleExpand();
|
||||
} else {
|
||||
item.onClick();
|
||||
}
|
||||
}}
|
||||
data-testid={`sidebar-item-link-for-${id}`}
|
||||
>
|
||||
{item.getIcon?.()}
|
||||
<span>{item.title}</span>
|
||||
{isExpandable && (
|
||||
<Icon
|
||||
className={styles.expandIcon}
|
||||
material={expanded ? "keyboard_arrow_up" : "keyboard_arrow_down"}
|
||||
data-testid={`sidebar-item-expand-icon-for-${id}`}
|
||||
/>
|
||||
)}
|
||||
</NavLink>
|
||||
{renderSubMenu()}
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
export const SidebarItem = withInjectables<Dependencies, SidebarItemProps>(NonInjectedSidebarItem, {
|
||||
getProps: (di, props) => ({
|
||||
@@ -123,3 +97,5 @@ export const SidebarItem = withInjectables<Dependencies, SidebarItemProps>(NonIn
|
||||
sidebarStorage: di.inject(sidebarStorageInjectable),
|
||||
}),
|
||||
});
|
||||
|
||||
SidebarItem.displayName = "SidebarItem";
|
||||
@@ -1,80 +0,0 @@
|
||||
/**
|
||||
* 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 { computedInjectManyInjectable } from "@ogre-tools/injectable-extension-for-mobx";
|
||||
import { byOrderNumber } from "../../../common/utils/composable-responsibilities/orderable/orderable";
|
||||
import type { SetRequired } from "type-fest";
|
||||
import type { StrictReactNode } from "@k8slens/utilities";
|
||||
|
||||
export interface SidebarItemRegistration {
|
||||
id: string;
|
||||
parentId: string | null;
|
||||
title: StrictReactNode;
|
||||
onClick: () => void;
|
||||
getIcon?: () => StrictReactNode;
|
||||
isActive?: IComputedValue<boolean>;
|
||||
isVisible?: IComputedValue<boolean>;
|
||||
orderNumber: number;
|
||||
}
|
||||
|
||||
export const sidebarItemsInjectionToken = getInjectionToken<
|
||||
IComputedValue<SidebarItemRegistration[]>
|
||||
>({ id: "sidebar-items-injection-token" });
|
||||
|
||||
export interface HierarchicalSidebarItem extends SetRequired<SidebarItemRegistration, "isActive" | "isVisible"> {
|
||||
children: HierarchicalSidebarItem[];
|
||||
}
|
||||
|
||||
const sidebarItemsInjectable = getInjectable({
|
||||
id: "sidebar-items",
|
||||
|
||||
instantiate: (di) => {
|
||||
const computedInjectMany = di.inject(computedInjectManyInjectable);
|
||||
const sidebarItemRegistrations = computedInjectMany(sidebarItemsInjectionToken);
|
||||
|
||||
return computed((): HierarchicalSidebarItem[] => {
|
||||
const registrations = sidebarItemRegistrations
|
||||
.get()
|
||||
.flatMap(reg => reg.get());
|
||||
|
||||
const getSidebarItemsHierarchy = (registrations: SidebarItemRegistration[]) => {
|
||||
const impl = (parentId: string | null): HierarchicalSidebarItem[] => (
|
||||
registrations
|
||||
.filter((item) => item.parentId === parentId)
|
||||
.map(({
|
||||
isActive = computed(() => false),
|
||||
isVisible = computed(() => true),
|
||||
...registration
|
||||
}) => {
|
||||
const children = impl(registration.id);
|
||||
|
||||
return {
|
||||
...registration,
|
||||
children,
|
||||
isVisible,
|
||||
isActive: computed(() => {
|
||||
if (children.length === 0) {
|
||||
return isActive.get();
|
||||
}
|
||||
|
||||
return children.some(child => child.isActive.get());
|
||||
}),
|
||||
};
|
||||
})
|
||||
.filter(({ isVisible }) => isVisible.get())
|
||||
.sort(byOrderNumber)
|
||||
);
|
||||
|
||||
return impl(null);
|
||||
};
|
||||
|
||||
return getSidebarItemsHierarchy(registrations);
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
export default sidebarItemsInjectable;
|
||||
@@ -12,8 +12,8 @@ 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 { HierarchicalSidebarItem } from "@k8slens/cluster-sidebar";
|
||||
import { sidebarItemsInjectable } from "@k8slens/cluster-sidebar";
|
||||
import type { IComputedValue } from "mobx";
|
||||
import catalogEntityRegistryInjectable from "../../api/catalog/entity/registry.injectable";
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import type { StrictReactNode } from "@k8slens/utilities";
|
||||
import { cssNames } from "@k8slens/utilities";
|
||||
import { Tab, Tabs } from "../tabs";
|
||||
import { ErrorBoundary } from "@k8slens/error-boundary";
|
||||
import type { HierarchicalSidebarItem } from "./sidebar-items.injectable";
|
||||
import type { HierarchicalSidebarItem } from "@k8slens/cluster-sidebar";
|
||||
|
||||
export interface TabLayoutProps {
|
||||
tabs?: HierarchicalSidebarItem[];
|
||||
|
||||
Reference in new issue
Block a user