/** * 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 "./sidebar.scss"; import type { TabLayoutRoute } from "./tab-layout"; import React from "react"; import { 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/rbac"; 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"; interface Props { className?: string; } @observer export class Sidebar extends React.Component { static displayName = "Sidebar"; async componentDidMount() { crdStore.reloadAll(); } 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[] { const routes: TabLayoutRoute[] = []; if (!menu.id) { return routes; } ClusterPageMenuRegistry.getInstance().getSubItems(menu).forEach((subMenu) => { const subPage = ClusterPageRegistry.getInstance().getByPageTarget(subMenu.target); if (subPage) { const { extensionId, id: pageId } = subPage; routes.push({ routePath: subPage.url, url: getExtensionPageUrl({ extensionId, pageId, params: subMenu.target.params }), title: subMenu.title, component: subPage.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)} ); }); } 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()}
); } }