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:
parent
468205bae2
commit
8b8a361b24
@ -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;
|
||||
@ -28,21 +28,6 @@ export class NamespaceStore extends KubeObjectStore<Namespace, NamespaceApi> {
|
||||
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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user