diff --git a/packages/core/src/renderer/cluster-frame-context/all-namespaces.injectable.ts b/packages/core/src/renderer/cluster-frame-context/all-namespaces.injectable.ts new file mode 100644 index 0000000000..47e3dafcda --- /dev/null +++ b/packages/core/src/renderer/cluster-frame-context/all-namespaces.injectable.ts @@ -0,0 +1,37 @@ +/** + * 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 assert from "assert"; +import { computed } from "mobx"; +import namespaceStoreInjectable from "../components/+namespaces/store.injectable"; +import hostedClusterInjectable from "./hosted-cluster.injectable"; + +const allNamespacesInjectable = getInjectable({ + id: "all-namespaces", + instantiate: (di) => { + const cluster = di.inject(hostedClusterInjectable); + const namespaceStore = di.inject(namespaceStoreInjectable); + + assert(cluster, "This can only be injected within a cluster frame"); + + return computed(() => { + // user given list of namespaces + if (cluster.accessibleNamespaces.length) { + return cluster.accessibleNamespaces.slice(); + } + + if (namespaceStore.items.length > 0) { + // namespaces from kubernetes api + return namespaceStore.items.map((namespace) => namespace.getName()); + } + + // fallback to cluster resolved namespaces because we could not load list + return cluster.allowedNamespaces.slice(); + }); + }, +}); + +export default allNamespacesInjectable; diff --git a/packages/core/src/renderer/cluster-frame-context/are-all-namespaces-selected.injectable.ts b/packages/core/src/renderer/cluster-frame-context/are-all-namespaces-selected.injectable.ts new file mode 100644 index 0000000000..23b0be0a1f --- /dev/null +++ b/packages/core/src/renderer/cluster-frame-context/are-all-namespaces-selected.injectable.ts @@ -0,0 +1,32 @@ +/** + * 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 assert from "assert"; +import { computed } from "mobx"; +import allNamespacesInjectable from "./all-namespaces.injectable"; +import hostedClusterInjectable from "./hosted-cluster.injectable"; +import selectedNamespacesForFilteringInjectable from "./selected-namespaces.injectable"; + +const areAllNamespacesSelectedInjectable = getInjectable({ + id: "are-all-namespaces-selected", + instantiate: (di) => { + const cluster = di.inject(hostedClusterInjectable); + + assert(cluster, "This can only be injected within a cluster frame"); + + const allNamespaces = di.inject(allNamespacesInjectable); + const contextNamespaces = di.inject(selectedNamespacesForFilteringInjectable); + + return computed(() => { + const namespaces = new Set(contextNamespaces.get()); + + return allNamespaces.get().length > 1 + && cluster.accessibleNamespaces.length === 0 + && allNamespaces.get().every(ns => namespaces.has(ns)); + }); + }, +}); + +export default areAllNamespacesSelectedInjectable; diff --git a/packages/core/src/renderer/cluster-frame-context/for-namespaced-resources.injectable.ts b/packages/core/src/renderer/cluster-frame-context/for-namespaced-resources.injectable.ts index f4795f8523..19c31f4e72 100644 --- a/packages/core/src/renderer/cluster-frame-context/for-namespaced-resources.injectable.ts +++ b/packages/core/src/renderer/cluster-frame-context/for-namespaced-resources.injectable.ts @@ -4,58 +4,24 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import type { ClusterContext } from "./cluster-frame-context"; -import namespaceStoreInjectable from "../components/+namespaces/store.injectable"; -import hostedClusterInjectable from "./hosted-cluster.injectable"; -import assert from "assert"; -import { computed } from "mobx"; -import selectedNamespacesStorageInjectable from "../../features/namespace-filtering/renderer/storage.injectable"; +import allNamespacesInjectable from "./all-namespaces.injectable"; +import selectedNamespacesForFilteringInjectable from "./selected-namespaces.injectable"; +import areAllNamespacesSelectedInjectable from "./are-all-namespaces-selected.injectable"; +import isNamespaceListSufficientToLoadFromAllNamespacesInjectable from "./is-loading-all.injectable"; +import globalWatchEnabledInjectable from "./global-watch-enabled.injectable"; const clusterFrameContextForNamespacedResourcesInjectable = getInjectable({ id: "cluster-frame-context-for-namespaced-resources", instantiate: (di): ClusterContext => { - const cluster = di.inject(hostedClusterInjectable); - const namespaceStore = di.inject(namespaceStoreInjectable); - const selectedNamespacesStorage = di.inject(selectedNamespacesStorageInjectable); - - assert(cluster, "This can only be injected within a cluster frame"); - - const allNamespaces = computed(() => { - // user given list of namespaces - if (cluster.accessibleNamespaces.length) { - return cluster.accessibleNamespaces.slice(); - } - - if (namespaceStore.items.length > 0) { - // namespaces from kubernetes api - return namespaceStore.items.map((namespace) => namespace.getName()); - } - - // fallback to cluster resolved namespaces because we could not load list - return cluster.allowedNamespaces.slice(); - }); - const contextNamespaces = computed(() => { - const selectedNamespaces = selectedNamespacesStorage.get(); - - return selectedNamespaces.length > 0 - ? selectedNamespaces - : allNamespaces.get(); - }); - const hasSelectedAll = computed(() => { - const namespaces = new Set(contextNamespaces.get()); - - return allNamespaces.get().length > 1 - && cluster.accessibleNamespaces.length === 0 - && allNamespaces.get().every(ns => namespaces.has(ns)); - }); + const allNamespaces = di.inject(allNamespacesInjectable); + const contextNamespaces = di.inject(selectedNamespacesForFilteringInjectable); + const areAllNamespacesSelected = di.inject(areAllNamespacesSelectedInjectable); + const globalWatchEnabled = di.inject(globalWatchEnabledInjectable); return { - isLoadingAll: (namespaces) => ( - allNamespaces.get().length > 1 - && cluster.accessibleNamespaces.length === 0 - && allNamespaces.get().every(ns => namespaces.includes(ns)) - ), - isGlobalWatchEnabled: () => cluster.isGlobalWatchEnabled, + isLoadingAll: di.inject(isNamespaceListSufficientToLoadFromAllNamespacesInjectable), + isGlobalWatchEnabled: () => globalWatchEnabled.get(), get allNamespaces() { return allNamespaces.get(); }, @@ -63,7 +29,7 @@ const clusterFrameContextForNamespacedResourcesInjectable = getInjectable({ return contextNamespaces.get(); }, get hasSelectedAll() { - return hasSelectedAll.get(); + return areAllNamespacesSelected.get(); }, }; }, diff --git a/packages/core/src/renderer/cluster-frame-context/global-watch-enabled.injectable.ts b/packages/core/src/renderer/cluster-frame-context/global-watch-enabled.injectable.ts new file mode 100644 index 0000000000..9ab9845b8f --- /dev/null +++ b/packages/core/src/renderer/cluster-frame-context/global-watch-enabled.injectable.ts @@ -0,0 +1,21 @@ +/** + * 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 assert from "assert"; +import { computed } from "mobx"; +import hostedClusterInjectable from "./hosted-cluster.injectable"; + +const globalWatchEnabledInjectable = getInjectable({ + id: "global-watch-enabled", + instantiate: (di) => { + const cluster = di.inject(hostedClusterInjectable); + + assert(cluster, "This can only be injected within a cluster frame"); + + return computed(() => cluster.isGlobalWatchEnabled); + }, +}); + +export default globalWatchEnabledInjectable; diff --git a/packages/core/src/renderer/cluster-frame-context/is-loading-all.injectable.ts b/packages/core/src/renderer/cluster-frame-context/is-loading-all.injectable.ts new file mode 100644 index 0000000000..2753533d1d --- /dev/null +++ b/packages/core/src/renderer/cluster-frame-context/is-loading-all.injectable.ts @@ -0,0 +1,29 @@ +/** + * 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 assert from "assert"; +import allNamespacesInjectable from "./all-namespaces.injectable"; +import hostedClusterInjectable from "./hosted-cluster.injectable"; + +export type IsNamespaceListSufficientToLoadFromAllNamespaces = (namespace: string[]) => boolean; + +const isNamespaceListSufficientToLoadFromAllNamespacesInjectable = getInjectable({ + id: "is-namespace-list-sufficient-to-load-from-all-namespaces", + instantiate: (di): IsNamespaceListSufficientToLoadFromAllNamespaces => { + const cluster = di.inject(hostedClusterInjectable); + + assert(cluster, "This can only be injected within a cluster frame"); + + const allNamespaces = di.inject(allNamespacesInjectable); + + return (namespaces) => ( + allNamespaces.get().length > 1 + && cluster.accessibleNamespaces.length === 0 + && allNamespaces.get().every(ns => namespaces.includes(ns)) + ); + }, +}); + +export default isNamespaceListSufficientToLoadFromAllNamespacesInjectable; diff --git a/packages/core/src/renderer/cluster-frame-context/selected-namespaces.injectable.ts b/packages/core/src/renderer/cluster-frame-context/selected-namespaces.injectable.ts new file mode 100644 index 0000000000..dd4304e327 --- /dev/null +++ b/packages/core/src/renderer/cluster-frame-context/selected-namespaces.injectable.ts @@ -0,0 +1,26 @@ +/** + * 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 { computed } from "mobx"; +import selectedNamespacesStorageInjectable from "../../features/namespace-filtering/renderer/storage.injectable"; +import allNamespacesInjectable from "./all-namespaces.injectable"; + +const selectedNamespacesForFilteringInjectable = getInjectable({ + id: "selected-namespaces-for-filtering", + instantiate: (di) => { + const selectedNamespacesStorage = di.inject(selectedNamespacesStorageInjectable); + const allNamespaces = di.inject(allNamespacesInjectable); + + return computed(() => { + const selectedNamespaces = selectedNamespacesStorage.get(); + + return selectedNamespaces.length > 0 + ? selectedNamespaces + : allNamespaces.get(); + }); + }, +}); + +export default selectedNamespacesForFilteringInjectable;