From 8b8a361b247e78b63d0311580c94a15f64ddac98 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 3 Mar 2023 16:36:27 -0500 Subject: [PATCH] Move selected_namespaces storage to own injectable with initialization Signed-off-by: Sebastian Malton --- .../namespace-storage.injectable.ts | 23 +++++++++++++++++++ .../renderer/components/+namespaces/store.ts | 15 ------------ 2 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 packages/core/src/renderer/components/+namespaces/namespace-storage.injectable.ts diff --git a/packages/core/src/renderer/components/+namespaces/namespace-storage.injectable.ts b/packages/core/src/renderer/components/+namespaces/namespace-storage.injectable.ts new file mode 100644 index 0000000000..846b522f08 --- /dev/null +++ b/packages/core/src/renderer/components/+namespaces/namespace-storage.injectable.ts @@ -0,0 +1,23 @@ +/** + * 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 clusterFrameContextForNamespacedResourcesInjectable from "../../cluster-frame-context/for-namespaced-resources.injectable"; +import { isDefined } from "../../utils"; +import createStorageInjectable from "../../utils/create-storage/create-storage.injectable"; + +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") + ? ["default"] + : [context.allNamespaces[0]].filter(isDefined); + + return createStorage("selected_namespaces", defaultSelectedNamespaces); + }, +}); + +export default selectedNamespaceStorageInjectable; diff --git a/packages/core/src/renderer/components/+namespaces/store.ts b/packages/core/src/renderer/components/+namespaces/store.ts index 3e3608fb81..dac4445f31 100644 --- a/packages/core/src/renderer/components/+namespaces/store.ts +++ b/packages/core/src/renderer/components/+namespaces/store.ts @@ -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 {