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

Move selected_namespaces storage to own injectable with initialization

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2023-03-03 16:36:27 -05:00
parent 468205bae2
commit 8b8a361b24
2 changed files with 23 additions and 15 deletions

View File

@ -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;

View File

@ -28,21 +28,6 @@ export class NamespaceStore extends KubeObjectStore<Namespace, NamespaceApi> {
super(dependencies, api); super(dependencies, api);
makeObservable(this); makeObservable(this);
autoBind(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 { public onContextChange(callback: (namespaces: string[]) => void, opts: { fireImmediately?: boolean } = {}): IReactionDisposer {