From 8709bc2a2e8dc232727efdbba39656ecdd8d2800 Mon Sep 17 00:00:00 2001 From: Roman Date: Wed, 3 Mar 2021 19:15:05 +0200 Subject: [PATCH] fixes, reverted logic for selecting default namespaces Signed-off-by: Roman --- .../components/+namespaces/namespace.store.ts | 28 +++++++++++++------ src/renderer/local-storage.ts | 2 +- src/renderer/utils/storageHelper.ts | 16 +++++------ 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/renderer/components/+namespaces/namespace.store.ts b/src/renderer/components/+namespaces/namespace.store.ts index 43b9988ded..69abbf931c 100644 --- a/src/renderer/components/+namespaces/namespace.store.ts +++ b/src/renderer/components/+namespaces/namespace.store.ts @@ -6,14 +6,14 @@ import { Namespace, namespacesApi } from "../../api/endpoints/namespaces.api"; import { createPageParam } from "../../navigation"; import { apiManager } from "../../api/api-manager"; -const selectedNamespaces = createStorage("selected_namespaces", ["default"]); +const selectedNamespaces = createStorage("selected_namespaces"); export const namespaceUrlParam = createPageParam({ name: "namespaces", isSystem: true, multiValues: true, get defaultValue() { - return selectedNamespaces.get(); + return selectedNamespaces.get() ?? []; // initial namespaces coming from URL or local-storage (default) } }); @@ -42,8 +42,8 @@ export class NamespaceStore extends KubeObjectStore { } private async init() { - await selectedNamespaces.whenReady; await this.contextReady; + await selectedNamespaces.whenReady; this.setContext(this.initialNamespaces); this.autoLoadAllowedNamespaces(); @@ -59,8 +59,8 @@ export class NamespaceStore extends KubeObjectStore { private autoUpdateUrlAndLocalStorage(): IReactionDisposer { return this.onContextChange(namespaces => { - selectedNamespaces.set(namespaces); - namespaceUrlParam.set(namespaces, { replaceHistory: true }); + selectedNamespaces.set(namespaces); // save to local-storage + namespaceUrlParam.set(namespaces, { replaceHistory: true }); // update url }, { fireImmediately: true, }); @@ -74,10 +74,22 @@ export class NamespaceStore extends KubeObjectStore { } private get initialNamespaces(): string[] { - const namespaces = this.allowedNamespaces; + const namespaces = new Set(this.allowedNamespaces); + const prevSelectedNamespaces = selectedNamespaces.get(); - // return all namespaces (empty list) if "default" namespace doesn't exist - return selectedNamespaces.get().filter(namespace => namespaces.includes(namespace)); + // return previously saved namespaces from local-storage (if any) + if (prevSelectedNamespaces) { + return prevSelectedNamespaces.filter(namespace => namespaces.has(namespace)); + } + + // otherwise select "default" or first allowed namespace + if (namespaces.has("default")) { + return ["default"]; + } else if (namespaces.size) { + return [Array.from(namespaces)[0]]; + } + + return []; } @computed get allowedNamespaces(): string[] { diff --git a/src/renderer/local-storage.ts b/src/renderer/local-storage.ts index bff3acf7e0..05aa5cebab 100644 --- a/src/renderer/local-storage.ts +++ b/src/renderer/local-storage.ts @@ -11,7 +11,7 @@ export class LensLocalStorage { private folderPath = path.resolve((app || remote.app).getPath("userData"), "lens-local-storage"); private filePath: string; - @observable state = observable.map(); + @observable state = observable.map([], { deep: false }); @observable isLoaded = false; whenReady = when(() => this.isLoaded); diff --git a/src/renderer/utils/storageHelper.ts b/src/renderer/utils/storageHelper.ts index 5222d16ec1..1d7a137f79 100755 --- a/src/renderer/utils/storageHelper.ts +++ b/src/renderer/utils/storageHelper.ts @@ -4,7 +4,7 @@ import type { CreateObservableOptions } from "mobx/lib/api/observable"; import { action, comparer, observable, toJS, when } from "mobx"; import produce, { Draft, setAutoFreeze } from "immer"; -import { isEmpty, isEqual, isFunction } from "lodash"; +import { isEqual, isFunction, isObject } from "lodash"; setAutoFreeze(false); // allow to merge observables @@ -67,7 +67,7 @@ export class StorageHelper { try { const value = await this.load(); - const notEmpty = this.hasValue(value); + const notEmpty = value != null; const notDefault = !this.isDefaultValue(value); if (notEmpty && notDefault) { @@ -79,11 +79,7 @@ export class StorageHelper { } } - hasValue(value: T) { - return !isEmpty(value); - } - - isDefaultValue(value: T) { + isDefaultValue(value: T): boolean { return isEqual(value, this.defaultValue); } @@ -136,7 +132,11 @@ export class StorageHelper { } merge(value: Partial | ((draft: Draft) => Partial | void)) { - const updater = isFunction(value) ? value : (state: Draft) => Object.assign(state, value); + const updater = isFunction(value) ? value : (state: Draft) => { + if (isObject(state)) return Object.assign(state, value); + + return value; + }; const currentValue = this.toJS(); const nextValue = produce(currentValue, updater) as T;