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..ec827d70c9 --- /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); + + assert(cluster, "This can only be injected within a cluster frame"); + + const namespaceStore = di.inject(namespaceStoreInjectable); + + 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/for-namespaced-resources.injectable.ts b/packages/core/src/renderer/cluster-frame-context/for-namespaced-resources.injectable.ts index bf8ff3bfc9..23217471d1 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,37 +4,23 @@ */ import { getInjectable } from "@ogre-tools/injectable"; import type { NamespaceScopedClusterContext } 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 selectedNamespaceStorageInjectable from "../components/+namespaces/namespace-storage.injectable"; import { toggle } from "../utils"; +import allNamespacesInjectable from "./all-namespaces.injectable"; const clusterFrameContextForNamespacedResourcesInjectable = getInjectable({ id: "cluster-frame-context-for-namespaced-resources", instantiate: (di): NamespaceScopedClusterContext => { const cluster = di.inject(hostedClusterInjectable); - const namespaceStore = di.inject(namespaceStoreInjectable); const selectedNamespaceStorage = di.inject(selectedNamespaceStorageInjectable); 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 allNamespaces = di.inject(allNamespacesInjectable); const contextNamespaces = computed(() => { const storedState = selectedNamespaceStorage.get(); diff --git a/packages/core/src/renderer/components/+namespaces/namespace-storage.injectable.ts b/packages/core/src/renderer/components/+namespaces/namespace-storage.injectable.ts index 846b522f08..d4b98a82f9 100644 --- a/packages/core/src/renderer/components/+namespaces/namespace-storage.injectable.ts +++ b/packages/core/src/renderer/components/+namespaces/namespace-storage.injectable.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import clusterFrameContextForNamespacedResourcesInjectable from "../../cluster-frame-context/for-namespaced-resources.injectable"; +import allNamespacesInjectable from "../../cluster-frame-context/all-namespaces.injectable"; import { isDefined } from "../../utils"; import createStorageInjectable from "../../utils/create-storage/create-storage.injectable"; @@ -11,10 +11,10 @@ const selectedNamespaceStorageInjectable = getInjectable({ id: "selected-namespace-storage", instantiate: (di) => { const createStorage = di.inject(createStorageInjectable); - const context = di.inject(clusterFrameContextForNamespacedResourcesInjectable); - const defaultSelectedNamespaces = context.allNamespaces.includes("default") + const allNamespaces = di.inject(allNamespacesInjectable); + const defaultSelectedNamespaces = allNamespaces.get().includes("default") ? ["default"] - : [context.allNamespaces[0]].filter(isDefined); + : [allNamespaces.get()[0]].filter(isDefined); return createStorage("selected_namespaces", defaultSelectedNamespaces); },