diff --git a/src/common/catalog-entities/general.ts b/src/common/catalog-entities/general.ts index 3a2e96a17d..5d13eeabee 100644 --- a/src/common/catalog-entities/general.ts +++ b/src/common/catalog-entities/general.ts @@ -19,9 +19,7 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -import { reaction, when } from "mobx"; -import { catalogEntityRegistry } from "../../renderer/api/catalog-entity-registry"; -import { isActiveRoute, navigate, navigation } from "../../renderer/navigation"; +import { navigate } from "../../renderer/navigation"; import { CatalogCategory, CatalogEntity, CatalogEntityMetadata, CatalogEntitySpec, CatalogEntityStatus } from "../catalog"; import { catalogCategoryRegistry } from "../catalog/catalog-category-registry"; @@ -73,23 +71,6 @@ export class GeneralCategory extends CatalogCategory { kind: "General" } }; - - constructor() { - super(); - - reaction(() => navigation.location, () => this.setEntityOnRouteMatch(), { fireImmediately: true }); - } - - async setEntityOnRouteMatch() { - await when(() => catalogEntityRegistry.entities.size > 0); - - const entities = catalogEntityRegistry.getItemsForCategory(this); - const activeEntity = entities.find(entity => isActiveRoute(entity.spec.path)); - - if (activeEntity) { - catalogEntityRegistry.activeEntity = activeEntity; - } - } } catalogCategoryRegistry.add(new GeneralCategory()); diff --git a/src/common/catalog/catalog-category-registry.ts b/src/common/catalog/catalog-category-registry.ts index 207dc2af3d..bad1570c9c 100644 --- a/src/common/catalog/catalog-category-registry.ts +++ b/src/common/catalog/catalog-category-registry.ts @@ -76,6 +76,10 @@ export class CatalogCategoryRegistry { return this.getForGroupKind(group, data.kind); } + + getByName(name: string) { + return this.items.find(category => category.metadata?.name == name); + } } export const catalogCategoryRegistry = new CatalogCategoryRegistry(); diff --git a/src/main/catalog-sources/helpers/general-active-sync.ts b/src/main/catalog-sources/helpers/general-active-sync.ts new file mode 100644 index 0000000000..e46e27f827 --- /dev/null +++ b/src/main/catalog-sources/helpers/general-active-sync.ts @@ -0,0 +1,36 @@ +/** + * 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 { when } from "mobx"; +import { catalogCategoryRegistry } from "../../../common/catalog"; +import { catalogEntityRegistry } from "../../../renderer/api/catalog-entity-registry"; +import { isActiveRoute } from "../../../renderer/navigation"; + +export async function setEntityOnRouteMatch() { + await when(() => catalogEntityRegistry.entities.size > 0); + + const entities = catalogEntityRegistry.getItemsForCategory(catalogCategoryRegistry.getByName("General")); + const activeEntity = entities.find(entity => isActiveRoute(entity.spec.path)); + + if (activeEntity) { + catalogEntityRegistry.activeEntity = activeEntity; + } +} diff --git a/src/renderer/components/+catalog/catalog.tsx b/src/renderer/components/+catalog/catalog.tsx index 271381aec1..7f6347f5e3 100644 --- a/src/renderer/components/+catalog/catalog.tsx +++ b/src/renderer/components/+catalog/catalog.tsx @@ -44,7 +44,6 @@ import { CatalogMenu } from "./catalog-menu"; import { HotbarIcon } from "../hotbar/hotbar-icon"; import { RenderDelay } from "../render-delay/render-delay"; import { TopBar } from "../layout/topbar"; -import { catalogEntityRegistry } from "../../api/catalog-entity-registry"; export const previousActiveTab = createAppStorage("catalog-previous-active-tab", ""); @@ -105,11 +104,6 @@ export class Catalog extends React.Component { }, {fireImmediately: true}), ]); } - - removeActiveEntity() { - catalogEntityRegistry.activeEntity = null; - } - addToHotbar(item: CatalogEntityItem): void { HotbarStore.getInstance().addToHotbar(item.entity); } diff --git a/src/renderer/components/cluster-manager/cluster-manager.tsx b/src/renderer/components/cluster-manager/cluster-manager.tsx index 4bd14e7805..3b6cf1a434 100644 --- a/src/renderer/components/cluster-manager/cluster-manager.tsx +++ b/src/renderer/components/cluster-manager/cluster-manager.tsx @@ -23,7 +23,7 @@ import "./cluster-manager.scss"; import React from "react"; import { Redirect, Route, Switch } from "react-router"; -import { observer } from "mobx-react"; +import { disposeOnUnmount, observer } from "mobx-react"; import { BottomBar } from "./bottom-bar"; import { Catalog } from "../+catalog"; import { Preferences } from "../+preferences"; @@ -35,9 +35,18 @@ import { HotbarMenu } from "../hotbar/hotbar-menu"; import { EntitySettings } from "../+entity-settings"; import { Welcome } from "../+welcome"; import * as routes from "../../../common/routes"; +import { reaction } from "mobx"; +import { navigation } from "../../navigation"; +import { setEntityOnRouteMatch } from "../../../main/catalog-sources/helpers/general-active-sync"; @observer export class ClusterManager extends React.Component { + componentDidMount() { + disposeOnUnmount(this, [ + reaction(() => navigation.location, () => setEntityOnRouteMatch(), { fireImmediately: true }) + ]); + } + render() { return (