1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

fixes, reverted logic for selecting default namespaces

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-03-03 19:15:05 +02:00
parent 76f9f3981d
commit 8709bc2a2e
3 changed files with 29 additions and 17 deletions

View File

@ -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<string[]>("selected_namespaces");
export const namespaceUrlParam = createPageParam<string[]>({
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<Namespace> {
}
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<Namespace> {
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<Namespace> {
}
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[] {

View File

@ -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<string, any>();
@observable state = observable.map<string, any>([], { deep: false });
@observable isLoaded = false;
whenReady = when(() => this.isLoaded);

View File

@ -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<T = any> {
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<T = any> {
}
}
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<T = any> {
}
merge(value: Partial<T> | ((draft: Draft<T>) => Partial<T> | void)) {
const updater = isFunction(value) ? value : (state: Draft<T>) => Object.assign(state, value);
const updater = isFunction(value) ? value : (state: Draft<T>) => {
if (isObject(state)) return Object.assign(state, value);
return value;
};
const currentValue = this.toJS();
const nextValue = produce(currentValue, updater) as T;