diff --git a/packages/core/src/features/namespace-filtering/renderer/storage.injectable.ts b/packages/core/src/features/namespace-filtering/renderer/storage.injectable.ts new file mode 100644 index 0000000000..d69111a35e --- /dev/null +++ b/packages/core/src/features/namespace-filtering/renderer/storage.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 assert from "assert"; +import hostedClusterInjectable from "../../../renderer/cluster-frame-context/hosted-cluster.injectable"; +import createStorageInjectable from "../../../renderer/utils/create-storage/create-storage.injectable"; + +const selectedNamespacesStorageInjectable = getInjectable({ + id: "selected-namespaces-storage", + instantiate: (di) => { + const createStorage = di.inject(createStorageInjectable); + const cluster = di.inject(hostedClusterInjectable); + + assert(cluster, "selectedNamespacesStorage is only available in certain environments"); + + const defaultSelectedNamespaces = cluster.allowedNamespaces.includes("default") + ? ["default"] + : cluster.allowedNamespaces.slice(0, 1); + + return createStorage("selected_namespaces", defaultSelectedNamespaces); + }, +}); + +export default selectedNamespacesStorageInjectable; diff --git a/packages/core/src/renderer/components/+namespaces/store.injectable.ts b/packages/core/src/renderer/components/+namespaces/store.injectable.ts index 1ee9dc024f..68540d0ce5 100644 --- a/packages/core/src/renderer/components/+namespaces/store.injectable.ts +++ b/packages/core/src/renderer/components/+namespaces/store.injectable.ts @@ -5,13 +5,13 @@ import { getInjectable } from "@ogre-tools/injectable"; import { NamespaceStore } from "./store"; import { kubeObjectStoreInjectionToken } from "../../../common/k8s-api/api-manager/kube-object-store-token"; -import createStorageInjectable from "../../utils/create-storage/create-storage.injectable"; import namespaceApiInjectable from "../../../common/k8s-api/endpoints/namespace.api.injectable"; import assert from "assert"; import storesAndApisCanBeCreatedInjectable from "../../stores-apis-can-be-created.injectable"; import clusterFrameContextForClusterScopedResourcesInjectable from "../../cluster-frame-context/for-cluster-scoped-resources.injectable"; import clusterConfiguredAccessibleNamespacesInjectable from "../../cluster/accessible-namespaces.injectable"; import loggerInjectable from "../../../common/logger.injectable"; +import selectedNamespacesStorageInjectable from "../../../features/namespace-filtering/renderer/storage.injectable"; const namespaceStoreInjectable = getInjectable({ id: "namespace-store", @@ -19,12 +19,11 @@ const namespaceStoreInjectable = getInjectable({ instantiate: (di) => { assert(di.inject(storesAndApisCanBeCreatedInjectable), "namespaceStore is only available in certain environments"); - const createStorage = di.inject(createStorageInjectable); const api = di.inject(namespaceApiInjectable); return new NamespaceStore({ context: di.inject(clusterFrameContextForClusterScopedResourcesInjectable), - storage: createStorage("selected_namespaces", undefined), + storage: di.inject(selectedNamespacesStorageInjectable), clusterConfiguredAccessibleNamespaces: di.inject(clusterConfiguredAccessibleNamespacesInjectable), logger: di.inject(loggerInjectable), }, api); diff --git a/packages/core/src/renderer/components/+namespaces/store.ts b/packages/core/src/renderer/components/+namespaces/store.ts index 3435975fd6..e1e26a0a92 100644 --- a/packages/core/src/renderer/components/+namespaces/store.ts +++ b/packages/core/src/renderer/components/+namespaces/store.ts @@ -19,7 +19,7 @@ export interface NamespaceTree { } interface Dependencies extends KubeObjectStoreDependencies { - readonly storage: StorageLayer; + readonly storage: StorageLayer; readonly clusterConfiguredAccessibleNamespaces: IComputedValue; } @@ -28,21 +28,6 @@ export class NamespaceStore extends KubeObjectStore { super(dependencies, api); makeObservable(this); autoBind(this); - - // initialize allowed namespaces - const { allowedNamespaces } = this; - const selectedNamespaces = this.dependencies.storage.get(); // raw namespaces, undefined on first load - - // return previously saved namespaces from local-storage (if any) - if (Array.isArray(selectedNamespaces)) { - this.selectNamespaces(selectedNamespaces.filter(namespace => allowedNamespaces.includes(namespace))); - } else if (allowedNamespaces.includes("default")) { - this.selectNamespaces(["default"]); - } else if (allowedNamespaces.length) { - this.selectNamespaces([allowedNamespaces[0]]); - } else { - this.selectNamespaces([]); - } } public onContextChange(callback: (namespaces: string[]) => void, opts: { fireImmediately?: boolean } = {}): IReactionDisposer {