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

clean up / responding to comments

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2021-03-03 12:58:39 +02:00
parent 37e31b2f99
commit 2279ac01ec
4 changed files with 39 additions and 44 deletions

View File

@ -48,13 +48,13 @@ describe("Lens cluster pages", () => {
}
};
function getSidebarSelectors(testId: string) {
const baseSelector = `.Sidebar [data-test-id="${testId}"]`;
function getMainMenuSelectors(itemId: string) {
const baseSelector = `.Sidebar [data-test-id="${itemId}"]`;
return {
sidebarItemRoot: baseSelector,
expandIcon: `${baseSelector} .expand-icon`,
pageLink(href: string){
pageLink(href: string) {
return `${baseSelector} a[href^="/${href}"]`;
}
};
@ -74,7 +74,11 @@ describe("Lens cluster pages", () => {
testId: string;
expectedSelector?: string;
expectedText?: string;
subMenu?: Required<Omit<SidebarItem & { href: string }, "testId" | "subMenu">>[];
subMenu?: {
href: string;
expectedSelector: string;
expectedText: string;
}[];
};
const sidebarMenu: SidebarItem[] = [
@ -267,7 +271,7 @@ describe("Lens cluster pages", () => {
}];
sidebarMenu.forEach(({ testId, expectedSelector, expectedText, subMenu }) => {
const { sidebarItemRoot, expandIcon, pageLink } = getSidebarSelectors(testId);
const { sidebarItemRoot, expandIcon, pageLink } = getMainMenuSelectors(testId);
if (subMenu) {
it(`expands submenu for pages in "${testId}"`, async () => {
@ -304,7 +308,7 @@ describe("Lens cluster pages", () => {
it(`shows a logs for a pod`, async () => {
expect(clusterAdded).toBe(true);
// Go to Pods page
await app.client.click(getSidebarSelectors("workloads").expandIcon);
await app.client.click(getMainMenuSelectors("workloads").expandIcon);
await app.client.waitUntilTextExists('a[href^="/pods"]', "Pods");
await app.client.click('a[href^="/pods"]');
await app.client.click(".NamespaceSelect");
@ -371,7 +375,7 @@ describe("Lens cluster pages", () => {
it(`creates a pod in ${TEST_NAMESPACE} namespace`, async () => {
expect(clusterAdded).toBe(true);
await app.client.click(getSidebarSelectors("workloads").expandIcon);
await app.client.click(getMainMenuSelectors("workloads").expandIcon);
await app.client.waitUntilTextExists('a[href^="/pods"]', "Pods");
await app.client.click('a[href^="/pods"]');

View File

@ -1,7 +1,7 @@
import { useState } from "react";
import { createStorage, StorageHelperOptions } from "../local-storage";
export function useStorage<T>(key: string, initialValue?: T, options?: StorageHelperOptions) {
export function useStorage<T>(key: string, initialValue?: T, options?: StorageHelperOptions<any>) {
const storage = createStorage(key, initialValue, options);
const [storageValue, setStorageValue] = useState(storage.get());
const setValue = (value: T) => {

View File

@ -1,6 +1,6 @@
import path from "path";
import fse from "fs-extra";
import { isEmpty } from "lodash";
import { isEmpty, noop } from "lodash";
import { app, remote } from "electron";
import { action, observable, reaction, when, } from "mobx";
import { StorageHelper, StorageHelperOptions } from "./utils/storageHelper";
@ -37,7 +37,7 @@ export class LensLocalStorage {
}
@action
async load() {
private async load() {
try {
await fse.ensureDir(this.folderPath, { mode: 0o755 });
const state = await fse.readJson(this.filePath);
@ -63,14 +63,14 @@ export class LensLocalStorage {
@action
clear() {
this.state.clear();
fse.unlink(this.filePath).catch(() => null);
fse.unlink(this.filePath).catch(noop);
}
}
export const lensLocalStorage = new LensLocalStorage();
export function createStorage<T>(key: string, defaultValue?: T, options: StorageHelperOptions<T> = {}) {
return new StorageHelper(key, defaultValue, {
return new StorageHelper<T>(key, defaultValue, {
...options,
storage: {
async getItem(key: string) {
@ -78,9 +78,12 @@ export function createStorage<T>(key: string, defaultValue?: T, options: Storage
return lensLocalStorage.state.get(key);
},
async setItem(key: string, value: any) {
setItem(key: string, value: any) {
lensLocalStorage.state.set(key, value);
},
removeItem(key: string) {
lensLocalStorage.state.delete(key);
},
},
});
}

View File

@ -8,36 +8,36 @@ import { isEmpty, isEqual, isFunction } from "lodash";
setAutoFreeze(false); // allow to merge observables
export interface StorageHelperOptions<T = any> extends StorageConfiguration<T> {
export interface StorageHelperOptions<T> extends StorageConfiguration<T> {
autoInit?: boolean; // default: true
}
export interface StorageConfiguration<T = any> {
export interface StorageConfiguration<T> {
storage?: StorageAdapter<T>;
observable?: CreateObservableOptions;
}
export interface StorageAdapter<T = any, C = StorageHelper<T>> {
getItem(this: C, key: string): T | Promise<T>; // import
setItem(this: C, key: string, value: T): void; // export
onChange?(this: C, value: T, oldValue?: T): void;
export interface StorageAdapter<T> {
getItem(key: string): T | Promise<T>; // import
setItem(key: string, value: T): void; // export
removeItem?(key: string): void; // if not provided setItem(key,undefined) will be used
onChange?(value: T, oldValue?: T): void;
}
export const localStorageAdapter: StorageAdapter = {
export const localStorageAdapter: StorageAdapter<Record<string, any>> = {
getItem(key: string) {
return JSON.parse(localStorage.getItem(key));
},
setItem(key: string, value: any) {
if (value != null) {
localStorage.setItem(key, JSON.stringify(value));
} else {
localStorage.removeItem(key);
}
localStorage.setItem(key, JSON.stringify(value));
},
removeItem(key: string) {
localStorage.removeItem(key);
}
};
export class StorageHelper<T = any> {
static defaultOptions: StorageHelperOptions = {
static defaultOptions: StorageHelperOptions<any> = {
autoInit: true,
storage: localStorageAdapter,
observable: {
@ -47,11 +47,11 @@ export class StorageHelper<T = any> {
};
private data = observable.box<T>();
@observable.ref storage: StorageAdapter<T, ThisType<this>>;
@observable.ref storage: StorageAdapter<T>;
@observable initialized = false;
whenReady = when(() => this.initialized);
constructor(readonly key: string, readonly defaultValue?: T, readonly options: StorageHelperOptions = {}) {
constructor(readonly key: string, readonly defaultValue?: T, readonly options: StorageHelperOptions<T> = {}) {
this.options = { ...StorageHelper.defaultOptions, ...options };
this.configure();
this.reset();
@ -88,7 +88,7 @@ export class StorageHelper<T = any> {
}
@action
configure({ storage, observable }: StorageConfiguration<T> = this.options): this {
private configure({ storage, observable }: StorageConfiguration<T> = this.options): this {
if (storage) this.configureStorage(storage);
if (observable) this.configureObservable(observable);
@ -96,8 +96,8 @@ export class StorageHelper<T = any> {
}
@action
configureStorage(storage: StorageAdapter<T>) {
this.storage = Object.getOwnPropertyNames(storage).reduce((storage, name: keyof StorageAdapter) => {
protected configureStorage(storage: StorageAdapter<T>) {
this.storage = Object.getOwnPropertyNames(storage).reduce((storage, name: keyof StorageAdapter<T>) => {
storage[name] = storage[name]?.bind(this); // bind storage-adapter methods to "this"-context
return storage;
@ -105,7 +105,7 @@ export class StorageHelper<T = any> {
}
@action
configureObservable(options: CreateObservableOptions = {}) {
protected configureObservable(options: CreateObservableOptions = {}) {
this.data = observable.box<T>(this.data.get(), {
...StorageHelper.defaultOptions.observable, // inherit default observability options
...options,
@ -152,18 +152,6 @@ export class StorageHelper<T = any> {
this.set(nextValue);
}
// TODO: experiment / proxy to target object
proxyTo(object: object) {
return new Proxy(object, {
get: (target: object, prop: PropertyKey, receiver: any) => {
return Reflect.get(target, prop, receiver);
},
set: (target: object, prop: PropertyKey, value: any, receiver: any) => {
return Reflect.set(target, prop, value, receiver);
}
});
}
toJS() {
return toJS(this.get(), { recurseEverything: true });
}