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:
parent
37e31b2f99
commit
2279ac01ec
@ -48,13 +48,13 @@ describe("Lens cluster pages", () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function getSidebarSelectors(testId: string) {
|
function getMainMenuSelectors(itemId: string) {
|
||||||
const baseSelector = `.Sidebar [data-test-id="${testId}"]`;
|
const baseSelector = `.Sidebar [data-test-id="${itemId}"]`;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
sidebarItemRoot: baseSelector,
|
sidebarItemRoot: baseSelector,
|
||||||
expandIcon: `${baseSelector} .expand-icon`,
|
expandIcon: `${baseSelector} .expand-icon`,
|
||||||
pageLink(href: string){
|
pageLink(href: string) {
|
||||||
return `${baseSelector} a[href^="/${href}"]`;
|
return `${baseSelector} a[href^="/${href}"]`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -74,7 +74,11 @@ describe("Lens cluster pages", () => {
|
|||||||
testId: string;
|
testId: string;
|
||||||
expectedSelector?: string;
|
expectedSelector?: string;
|
||||||
expectedText?: string;
|
expectedText?: string;
|
||||||
subMenu?: Required<Omit<SidebarItem & { href: string }, "testId" | "subMenu">>[];
|
subMenu?: {
|
||||||
|
href: string;
|
||||||
|
expectedSelector: string;
|
||||||
|
expectedText: string;
|
||||||
|
}[];
|
||||||
};
|
};
|
||||||
|
|
||||||
const sidebarMenu: SidebarItem[] = [
|
const sidebarMenu: SidebarItem[] = [
|
||||||
@ -267,7 +271,7 @@ describe("Lens cluster pages", () => {
|
|||||||
}];
|
}];
|
||||||
|
|
||||||
sidebarMenu.forEach(({ testId, expectedSelector, expectedText, subMenu }) => {
|
sidebarMenu.forEach(({ testId, expectedSelector, expectedText, subMenu }) => {
|
||||||
const { sidebarItemRoot, expandIcon, pageLink } = getSidebarSelectors(testId);
|
const { sidebarItemRoot, expandIcon, pageLink } = getMainMenuSelectors(testId);
|
||||||
|
|
||||||
if (subMenu) {
|
if (subMenu) {
|
||||||
it(`expands submenu for pages in "${testId}"`, async () => {
|
it(`expands submenu for pages in "${testId}"`, async () => {
|
||||||
@ -304,7 +308,7 @@ describe("Lens cluster pages", () => {
|
|||||||
it(`shows a logs for a pod`, async () => {
|
it(`shows a logs for a pod`, async () => {
|
||||||
expect(clusterAdded).toBe(true);
|
expect(clusterAdded).toBe(true);
|
||||||
// Go to Pods page
|
// 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.waitUntilTextExists('a[href^="/pods"]', "Pods");
|
||||||
await app.client.click('a[href^="/pods"]');
|
await app.client.click('a[href^="/pods"]');
|
||||||
await app.client.click(".NamespaceSelect");
|
await app.client.click(".NamespaceSelect");
|
||||||
@ -371,7 +375,7 @@ describe("Lens cluster pages", () => {
|
|||||||
|
|
||||||
it(`creates a pod in ${TEST_NAMESPACE} namespace`, async () => {
|
it(`creates a pod in ${TEST_NAMESPACE} namespace`, async () => {
|
||||||
expect(clusterAdded).toBe(true);
|
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.waitUntilTextExists('a[href^="/pods"]', "Pods");
|
||||||
await app.client.click('a[href^="/pods"]');
|
await app.client.click('a[href^="/pods"]');
|
||||||
|
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { createStorage, StorageHelperOptions } from "../local-storage";
|
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 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) => {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import fse from "fs-extra";
|
import fse from "fs-extra";
|
||||||
import { isEmpty } from "lodash";
|
import { isEmpty, noop } from "lodash";
|
||||||
import { app, remote } from "electron";
|
import { app, remote } from "electron";
|
||||||
import { action, observable, reaction, when, } from "mobx";
|
import { action, observable, reaction, when, } from "mobx";
|
||||||
import { StorageHelper, StorageHelperOptions } from "./utils/storageHelper";
|
import { StorageHelper, StorageHelperOptions } from "./utils/storageHelper";
|
||||||
@ -37,7 +37,7 @@ export class LensLocalStorage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
async load() {
|
private async load() {
|
||||||
try {
|
try {
|
||||||
await fse.ensureDir(this.folderPath, { mode: 0o755 });
|
await fse.ensureDir(this.folderPath, { mode: 0o755 });
|
||||||
const state = await fse.readJson(this.filePath);
|
const state = await fse.readJson(this.filePath);
|
||||||
@ -63,14 +63,14 @@ export class LensLocalStorage {
|
|||||||
@action
|
@action
|
||||||
clear() {
|
clear() {
|
||||||
this.state.clear();
|
this.state.clear();
|
||||||
fse.unlink(this.filePath).catch(() => null);
|
fse.unlink(this.filePath).catch(noop);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const lensLocalStorage = new LensLocalStorage();
|
export const lensLocalStorage = new LensLocalStorage();
|
||||||
|
|
||||||
export function createStorage<T>(key: string, defaultValue?: T, options: StorageHelperOptions<T> = {}) {
|
export function createStorage<T>(key: string, defaultValue?: T, options: StorageHelperOptions<T> = {}) {
|
||||||
return new StorageHelper(key, defaultValue, {
|
return new StorageHelper<T>(key, defaultValue, {
|
||||||
...options,
|
...options,
|
||||||
storage: {
|
storage: {
|
||||||
async getItem(key: string) {
|
async getItem(key: string) {
|
||||||
@ -78,9 +78,12 @@ export function createStorage<T>(key: string, defaultValue?: T, options: Storage
|
|||||||
|
|
||||||
return lensLocalStorage.state.get(key);
|
return lensLocalStorage.state.get(key);
|
||||||
},
|
},
|
||||||
async setItem(key: string, value: any) {
|
setItem(key: string, value: any) {
|
||||||
lensLocalStorage.state.set(key, value);
|
lensLocalStorage.state.set(key, value);
|
||||||
},
|
},
|
||||||
|
removeItem(key: string) {
|
||||||
|
lensLocalStorage.state.delete(key);
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,36 +8,36 @@ import { isEmpty, isEqual, isFunction } from "lodash";
|
|||||||
|
|
||||||
setAutoFreeze(false); // allow to merge observables
|
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
|
autoInit?: boolean; // default: true
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface StorageConfiguration<T = any> {
|
export interface StorageConfiguration<T> {
|
||||||
storage?: StorageAdapter<T>;
|
storage?: StorageAdapter<T>;
|
||||||
observable?: CreateObservableOptions;
|
observable?: CreateObservableOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface StorageAdapter<T = any, C = StorageHelper<T>> {
|
export interface StorageAdapter<T> {
|
||||||
getItem(this: C, key: string): T | Promise<T>; // import
|
getItem(key: string): T | Promise<T>; // import
|
||||||
setItem(this: C, key: string, value: T): void; // export
|
setItem(key: string, value: T): void; // export
|
||||||
onChange?(this: C, value: T, oldValue?: T): void;
|
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) {
|
getItem(key: string) {
|
||||||
return JSON.parse(localStorage.getItem(key));
|
return JSON.parse(localStorage.getItem(key));
|
||||||
},
|
},
|
||||||
setItem(key: string, value: any) {
|
setItem(key: string, value: any) {
|
||||||
if (value != null) {
|
|
||||||
localStorage.setItem(key, JSON.stringify(value));
|
localStorage.setItem(key, JSON.stringify(value));
|
||||||
} else {
|
},
|
||||||
|
removeItem(key: string) {
|
||||||
localStorage.removeItem(key);
|
localStorage.removeItem(key);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export class StorageHelper<T = any> {
|
export class StorageHelper<T = any> {
|
||||||
static defaultOptions: StorageHelperOptions = {
|
static defaultOptions: StorageHelperOptions<any> = {
|
||||||
autoInit: true,
|
autoInit: true,
|
||||||
storage: localStorageAdapter,
|
storage: localStorageAdapter,
|
||||||
observable: {
|
observable: {
|
||||||
@ -47,11 +47,11 @@ export class StorageHelper<T = any> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private data = observable.box<T>();
|
private data = observable.box<T>();
|
||||||
@observable.ref storage: StorageAdapter<T, ThisType<this>>;
|
@observable.ref storage: StorageAdapter<T>;
|
||||||
@observable initialized = false;
|
@observable initialized = false;
|
||||||
whenReady = when(() => this.initialized);
|
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.options = { ...StorageHelper.defaultOptions, ...options };
|
||||||
this.configure();
|
this.configure();
|
||||||
this.reset();
|
this.reset();
|
||||||
@ -88,7 +88,7 @@ export class StorageHelper<T = any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
configure({ storage, observable }: StorageConfiguration<T> = this.options): this {
|
private configure({ storage, observable }: StorageConfiguration<T> = this.options): this {
|
||||||
if (storage) this.configureStorage(storage);
|
if (storage) this.configureStorage(storage);
|
||||||
if (observable) this.configureObservable(observable);
|
if (observable) this.configureObservable(observable);
|
||||||
|
|
||||||
@ -96,8 +96,8 @@ export class StorageHelper<T = any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
configureStorage(storage: StorageAdapter<T>) {
|
protected configureStorage(storage: StorageAdapter<T>) {
|
||||||
this.storage = Object.getOwnPropertyNames(storage).reduce((storage, name: keyof StorageAdapter) => {
|
this.storage = Object.getOwnPropertyNames(storage).reduce((storage, name: keyof StorageAdapter<T>) => {
|
||||||
storage[name] = storage[name]?.bind(this); // bind storage-adapter methods to "this"-context
|
storage[name] = storage[name]?.bind(this); // bind storage-adapter methods to "this"-context
|
||||||
|
|
||||||
return storage;
|
return storage;
|
||||||
@ -105,7 +105,7 @@ export class StorageHelper<T = any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@action
|
@action
|
||||||
configureObservable(options: CreateObservableOptions = {}) {
|
protected configureObservable(options: CreateObservableOptions = {}) {
|
||||||
this.data = observable.box<T>(this.data.get(), {
|
this.data = observable.box<T>(this.data.get(), {
|
||||||
...StorageHelper.defaultOptions.observable, // inherit default observability options
|
...StorageHelper.defaultOptions.observable, // inherit default observability options
|
||||||
...options,
|
...options,
|
||||||
@ -152,18 +152,6 @@ export class StorageHelper<T = any> {
|
|||||||
this.set(nextValue);
|
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() {
|
toJS() {
|
||||||
return toJS(this.get(), { recurseEverything: true });
|
return toJS(this.get(), { recurseEverything: true });
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user