diff --git a/src/renderer/components/+namespaces/namespace.store.ts b/src/renderer/components/+namespaces/namespace.store.ts index f81fee1ece..4415f416b9 100644 --- a/src/renderer/components/+namespaces/namespace.store.ts +++ b/src/renderer/components/+namespaces/namespace.store.ts @@ -5,7 +5,7 @@ import { Namespace, namespacesApi } from "../../api/endpoints/namespaces.api"; import { createPageParam } from "../../navigation"; import { apiManager } from "../../api/api-manager"; -const selectedNamespaces = createStorage("selected_namespaces"); +const selectedNamespaces = createStorage("selected_namespaces", undefined); export const namespaceUrlParam = createPageParam({ name: "namespaces", diff --git a/src/renderer/components/dock/dock-tab.store.ts b/src/renderer/components/dock/dock-tab.store.ts index c951d221fd..34a9b647bb 100644 --- a/src/renderer/components/dock/dock-tab.store.ts +++ b/src/renderer/components/dock/dock-tab.store.ts @@ -81,6 +81,6 @@ export class DockTabStore { reset() { this.data.clear(); - this.storage?.clear(); + this.storage?.reset(); } } diff --git a/src/renderer/hooks/useStorage.ts b/src/renderer/hooks/useStorage.ts index 6867dcc79a..febda7edb5 100644 --- a/src/renderer/hooks/useStorage.ts +++ b/src/renderer/hooks/useStorage.ts @@ -2,7 +2,7 @@ import { useState } from "react"; import { createStorage } from "../utils"; import { CreateObservableOptions } from "mobx/lib/api/observable"; -export function useStorage(key: string, initialValue?: T, options?: CreateObservableOptions) { +export function useStorage(key: string, initialValue: T, options?: CreateObservableOptions) { const storage = createStorage(key, initialValue, options); const [storageValue, setStorageValue] = useState(storage.get()); const setValue = (value: T) => { diff --git a/src/renderer/utils/__tests__/storageHelper.test.ts b/src/renderer/utils/__tests__/storageHelper.test.ts index 8e13543ccd..bf0f19f114 100644 --- a/src/renderer/utils/__tests__/storageHelper.test.ts +++ b/src/renderer/utils/__tests__/storageHelper.test.ts @@ -142,17 +142,6 @@ describe("renderer/utils/StorageHelper", () => { })); expect(storageHelper.get()).toEqual({ ...storageHelperDefaultValue, message: "updated3" }); }); - - it("clears data in storage", () => { - storageHelper.init(); - - expect(storageHelper.get()).toBeTruthy(); - storageHelper.clear(); - expect(storageHelper.get()).toBeFalsy(); - expect(storageMock[storageKey]).toBeUndefined(); - expect(storageAdapter.removeItem).toHaveBeenCalledWith(storageHelper.key); - }); - }); describe("data in storage-helper is observable (mobx)", () => { diff --git a/src/renderer/utils/createStorage.ts b/src/renderer/utils/createStorage.ts index 623f657461..2caa3736a4 100755 --- a/src/renderer/utils/createStorage.ts +++ b/src/renderer/utils/createStorage.ts @@ -14,7 +14,7 @@ let initialized = false; const loaded = observable.box(false); const storage = observable.map(); -export function createStorage(key: string, defaultValue?: T, observableOptions?: CreateObservableOptions) { +export function createStorage(key: string, defaultValue: T, observableOptions?: CreateObservableOptions) { const clusterId = getHostedClusterId(); const savingFolder = path.resolve((app || remote.app).getPath("userData"), "lens-local-storage"); const jsonFilePath = path.resolve(savingFolder, `${clusterId ?? "app"}.json`); diff --git a/src/renderer/utils/storageHelper.ts b/src/renderer/utils/storageHelper.ts index f6e6a3d5f9..2b2b14464a 100755 --- a/src/renderer/utils/storageHelper.ts +++ b/src/renderer/utils/storageHelper.ts @@ -21,7 +21,7 @@ export interface StorageHelperOptions { autoInit?: boolean; // start preloading data immediately, default: true observable?: CreateObservableOptions; storage: StorageAdapter; - defaultValue?: T; + defaultValue: T; } export class StorageHelper { @@ -133,17 +133,18 @@ export class StorageHelper { } set(value: T) { - this.data.set(value); + if (value == null) { + // This cannot use recursion because defaultValue might be null or undefined + this.data.set(this.defaultValue); + } else { + this.data.set(value); + } } reset() { this.set(this.defaultValue); } - clear() { - this.data.set(null); - } - merge(value: Partial | ((draft: Draft) => Partial | void)) { const nextValue = produce(this.get(), (state: Draft) => { const newValue = isFunction(value) ? value(state) : value;