mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix StorageHelper.get() returning null after clearing (#2421)
This commit is contained in:
parent
59bb4f556d
commit
2f3a1dbc25
@ -5,7 +5,7 @@ import { Namespace, namespacesApi } from "../../api/endpoints/namespaces.api";
|
|||||||
import { createPageParam } from "../../navigation";
|
import { createPageParam } from "../../navigation";
|
||||||
import { apiManager } from "../../api/api-manager";
|
import { apiManager } from "../../api/api-manager";
|
||||||
|
|
||||||
const selectedNamespaces = createStorage<string[]>("selected_namespaces");
|
const selectedNamespaces = createStorage<string[] | undefined>("selected_namespaces", undefined);
|
||||||
|
|
||||||
export const namespaceUrlParam = createPageParam<string[]>({
|
export const namespaceUrlParam = createPageParam<string[]>({
|
||||||
name: "namespaces",
|
name: "namespaces",
|
||||||
|
|||||||
@ -81,6 +81,6 @@ export class DockTabStore<T> {
|
|||||||
|
|
||||||
reset() {
|
reset() {
|
||||||
this.data.clear();
|
this.data.clear();
|
||||||
this.storage?.clear();
|
this.storage?.reset();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import { useState } from "react";
|
|||||||
import { createStorage } from "../utils";
|
import { createStorage } from "../utils";
|
||||||
import { CreateObservableOptions } from "mobx/lib/api/observable";
|
import { CreateObservableOptions } from "mobx/lib/api/observable";
|
||||||
|
|
||||||
export function useStorage<T>(key: string, initialValue?: T, options?: CreateObservableOptions) {
|
export function useStorage<T>(key: string, initialValue: T, options?: CreateObservableOptions) {
|
||||||
const storage = createStorage(key, initialValue, options);
|
const storage = createStorage(key, initialValue, options);
|
||||||
const [storageValue, setStorageValue] = useState(storage.get());
|
const [storageValue, setStorageValue] = useState(storage.get());
|
||||||
const setValue = (value: T) => {
|
const setValue = (value: T) => {
|
||||||
|
|||||||
@ -142,17 +142,6 @@ describe("renderer/utils/StorageHelper", () => {
|
|||||||
}));
|
}));
|
||||||
expect(storageHelper.get()).toEqual({ ...storageHelperDefaultValue, message: "updated3" });
|
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)", () => {
|
describe("data in storage-helper is observable (mobx)", () => {
|
||||||
|
|||||||
@ -14,7 +14,7 @@ let initialized = false;
|
|||||||
const loaded = observable.box(false);
|
const loaded = observable.box(false);
|
||||||
const storage = observable.map<string/* key */, any /* serializable */>();
|
const storage = observable.map<string/* key */, any /* serializable */>();
|
||||||
|
|
||||||
export function createStorage<T>(key: string, defaultValue?: T, observableOptions?: CreateObservableOptions) {
|
export function createStorage<T>(key: string, defaultValue: T, observableOptions?: CreateObservableOptions) {
|
||||||
const clusterId = getHostedClusterId();
|
const clusterId = getHostedClusterId();
|
||||||
const savingFolder = path.resolve((app || remote.app).getPath("userData"), "lens-local-storage");
|
const savingFolder = path.resolve((app || remote.app).getPath("userData"), "lens-local-storage");
|
||||||
const jsonFilePath = path.resolve(savingFolder, `${clusterId ?? "app"}.json`);
|
const jsonFilePath = path.resolve(savingFolder, `${clusterId ?? "app"}.json`);
|
||||||
|
|||||||
@ -21,7 +21,7 @@ export interface StorageHelperOptions<T> {
|
|||||||
autoInit?: boolean; // start preloading data immediately, default: true
|
autoInit?: boolean; // start preloading data immediately, default: true
|
||||||
observable?: CreateObservableOptions;
|
observable?: CreateObservableOptions;
|
||||||
storage: StorageAdapter<T>;
|
storage: StorageAdapter<T>;
|
||||||
defaultValue?: T;
|
defaultValue: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class StorageHelper<T> {
|
export class StorageHelper<T> {
|
||||||
@ -133,17 +133,18 @@ export class StorageHelper<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
set(value: T) {
|
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() {
|
reset() {
|
||||||
this.set(this.defaultValue);
|
this.set(this.defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
|
||||||
this.data.set(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
merge(value: Partial<T> | ((draft: Draft<T>) => Partial<T> | void)) {
|
merge(value: Partial<T> | ((draft: Draft<T>) => Partial<T> | void)) {
|
||||||
const nextValue = produce(this.get(), (state: Draft<T>) => {
|
const nextValue = produce(this.get(), (state: Draft<T>) => {
|
||||||
const newValue = isFunction(value) ? value(state) : value;
|
const newValue = isFunction(value) ? value(state) : value;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user