1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/api/helpers/watch-for-general-entity-navigation.injectable.ts

54 lines
2.0 KiB
TypeScript

/**
* 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 { reaction, when } from "mobx";
import type { GeneralEntity } from "../../../common/catalog-entities";
import generalCategoryInjectable from "../../../common/catalog/categories/general.injectable";
import isActiveRouteInjectable from "../../navigation/is-route-active.injectable";
import observableHistoryInjectable from "../../navigation/observable-history.injectable";
import type { Disposer } from "../../utils";
import { disposer } from "../../utils";
import catalogEntityRegistryInjectable from "../catalog/entity/registry.injectable";
export type WatchForGeneralEntityNavigation = () => Disposer;
const watchForGeneralEntityNavigationInjectable = getInjectable({
id: "watch-for-general-entity-navigation",
instantiate: (di): WatchForGeneralEntityNavigation => {
const observableHistory = di.inject(observableHistoryInjectable);
const isActiveRoute = di.inject(isActiveRouteInjectable);
const entityRegistry = di.inject(catalogEntityRegistryInjectable);
const generalCategory = di.inject(generalCategoryInjectable);
return () => {
const dispose = disposer();
dispose.push(when(
() => entityRegistry.entities.size > 0,
() => {
dispose.push(reaction(
() => observableHistory.location,
() => {
const entities = entityRegistry.getItemsForCategory(generalCategory) as GeneralEntity[];
const activeEntity = entities.find(entity => isActiveRoute(entity.spec.path));
if (activeEntity) {
entityRegistry.activeEntity = activeEntity;
}
},
{
fireImmediately: true,
},
));
},
));
return dispose;
};
},
});
export default watchForGeneralEntityNavigationInjectable;