/** * Copyright (c) 2021 OpenLens Authors * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import styles from "./sidebar.module.css"; import type { TabLayoutRoute } from "./tab-layout"; import React from "react"; import { disposeOnUnmount, observer } from "mobx-react"; import { cssNames } from "../../utils"; import { Icon } from "../icon"; import { Workloads } from "../+workloads"; import { UserManagement } from "../+user-management"; import { Storage } from "../+storage"; import { Network } from "../+network"; import { crdStore } from "../+custom-resources/crd.store"; import { CustomResources } from "../+custom-resources/custom-resources"; import { isActiveRoute } from "../../navigation"; import { isAllowedResource } from "../../../common/utils/allowed-resource"; import { Spinner } from "../spinner"; import { ClusterPageMenuRegistration, ClusterPageMenuRegistry, ClusterPageRegistry, getExtensionPageUrl } from "../../../extensions/registries"; import { SidebarItem } from "./sidebar-item"; import { Apps } from "../+apps"; import * as routes from "../../../common/routes"; import { Config } from "../+config"; import { catalogEntityRegistry } from "../../api/catalog-entity-registry"; import { SidebarCluster } from "./sidebar-cluster"; import { kubeWatchApi } from "../../../common/k8s-api/kube-watch-api"; interface Props { className?: string; } @observer export class Sidebar extends React.Component { static displayName = "Sidebar"; componentDidMount() { disposeOnUnmount(this, [ kubeWatchApi.subscribeStores([crdStore]), ]); } renderCustomResources() { if (crdStore.isLoading) { return (
); } return Object.entries(crdStore.groups).map(([group, crds]) => { const id = `crd-group:${group}`; const crdGroupsPageUrl = routes.crdURL({ query: { groups: group }}); return ( {crds.map((crd) => ( ))} ); }); } renderTreeFromTabRoutes(tabRoutes: TabLayoutRoute[] = []): React.ReactNode { if (!tabRoutes.length) { return null; } return tabRoutes.map(({ title, routePath, url = routePath, exact = true }) => { const subMenuItemId = `tab-route-item-${url}`; return ( ); }); } getTabLayoutRoutes(menu: ClusterPageMenuRegistration): TabLayoutRoute[] { if (!menu.id) { return []; } const routes: TabLayoutRoute[] = []; const subMenus = ClusterPageMenuRegistry.getInstance().getSubItems(menu); const clusterPageRegistry = ClusterPageRegistry.getInstance(); for (const subMenu of subMenus) { const page = clusterPageRegistry.getByPageTarget(subMenu.target); if (!page) { continue; } const { extensionId, id: pageId, url, components } = page; if (subMenu.components.Icon) { console.warn( "ClusterPageMenuRegistration has components.Icon defined and a valid parentId. Icon will not be displayed", { id: subMenu.id, parentId: subMenu.parentId, target: subMenu.target, }, ); } routes.push({ routePath: url, url: getExtensionPageUrl({ extensionId, pageId, params: subMenu.target.params }), title: subMenu.title, component: components.Page, }); } return routes; } renderRegisteredMenus() { return ClusterPageMenuRegistry.getInstance().getRootItems().map((menuItem, index) => { const registeredPage = ClusterPageRegistry.getInstance().getByPageTarget(menuItem.target); const tabRoutes = this.getTabLayoutRoutes(menuItem); const id = `registered-item-${index}`; let pageUrl: string; let isActive = false; if (registeredPage) { const { extensionId, id: pageId } = registeredPage; pageUrl = getExtensionPageUrl({ extensionId, pageId, params: menuItem.target.params }); isActive = isActiveRoute(registeredPage.url); } else if (tabRoutes.length > 0) { pageUrl = tabRoutes[0].url; isActive = isActiveRoute(tabRoutes.map((tab) => tab.routePath)); } else { return null; } return ( } > {this.renderTreeFromTabRoutes(tabRoutes)} ); }); } get clusterEntity() { return catalogEntityRegistry.activeEntity; } render() { const { className } = this.props; return (
} /> } /> } > {this.renderTreeFromTabRoutes(Workloads.tabRoutes)} } > {this.renderTreeFromTabRoutes(Config.tabRoutes)} } > {this.renderTreeFromTabRoutes(Network.tabRoutes)} } > {this.renderTreeFromTabRoutes(Storage.tabRoutes)} } /> } /> } > {this.renderTreeFromTabRoutes(Apps.tabRoutes)} } > {this.renderTreeFromTabRoutes(UserManagement.tabRoutes)} } > {this.renderTreeFromTabRoutes(CustomResources.tabRoutes)} {this.renderCustomResources()} {this.renderRegisteredMenus()}
); } }