mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
- Add route, clusterRoute, and payloadValidatedClusterRoute helper functions to improve types with backend routes - Turn on the following new lints: - react/jsx-first-prop-new-line - react/jsx-wrap-multilines - react/jsx-one-expression-per-line - react/jsx-max-props-per-line - react/jsx-indent - react/jsx-indent-props Signed-off-by: Sebastian Malton <sebastian@malton.name>
105 lines
2.5 KiB
TypeScript
105 lines
2.5 KiB
TypeScript
/**
|
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
|
*/
|
|
|
|
import { action, observable, reaction } from "mobx";
|
|
import type { StorageHelper } from "../../../utils";
|
|
import { autoBind, toJS } from "../../../utils";
|
|
import type { TabId } from "../dock/store";
|
|
|
|
export interface DockTabStoreOptions {
|
|
autoInit?: boolean; // load data from storage when `storageKey` is provided and bind events, default: true
|
|
storageKey?: string; // save data to persistent storage under the key
|
|
}
|
|
|
|
export type DockTabStorageState<T> = Record<TabId, T>;
|
|
|
|
interface DockTabStoreDependencies {
|
|
createStorage: <T>(storageKey: string, options: DockTabStorageState<T>) => StorageHelper<DockTabStorageState<T>>;
|
|
}
|
|
|
|
export class DockTabStore<T> {
|
|
protected storage?: StorageHelper<DockTabStorageState<T>>;
|
|
private data = observable.map<TabId, T>();
|
|
|
|
constructor(protected dependencies: DockTabStoreDependencies, protected options: DockTabStoreOptions) {
|
|
autoBind(this);
|
|
|
|
this.options = {
|
|
autoInit: true,
|
|
...this.options,
|
|
};
|
|
|
|
if (this.options.autoInit) {
|
|
this.init();
|
|
}
|
|
}
|
|
|
|
protected init() {
|
|
const { storageKey } = this.options;
|
|
|
|
// auto-save to local-storage
|
|
if (storageKey) {
|
|
const storage = this.storage = this.dependencies.createStorage<T>(storageKey, {});
|
|
|
|
storage.whenReady.then(() => {
|
|
this.data.replace(storage.value);
|
|
reaction(() => this.toJSON(), data => storage.set(data));
|
|
});
|
|
}
|
|
}
|
|
|
|
protected finalizeDataForSave(data: T): T {
|
|
return data;
|
|
}
|
|
|
|
protected toJSON(): DockTabStorageState<T> {
|
|
const deepCopy = toJS(this.data);
|
|
|
|
deepCopy.forEach((tabData, key) => {
|
|
deepCopy.set(key, this.finalizeDataForSave(tabData));
|
|
});
|
|
|
|
return Object.fromEntries<T>(deepCopy);
|
|
}
|
|
|
|
protected getAllData() {
|
|
return this.data.toJSON();
|
|
}
|
|
|
|
findTabIdFromData(inspecter: (val: T) => any): TabId | undefined {
|
|
for (const [tabId, data] of this.data) {
|
|
if (inspecter(data)) {
|
|
return tabId;
|
|
}
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
isReady(tabId: TabId): boolean {
|
|
return this.getData(tabId) !== undefined;
|
|
}
|
|
|
|
getData(tabId: TabId) {
|
|
return this.data.get(tabId);
|
|
}
|
|
|
|
setData(tabId: TabId, data: T) {
|
|
this.data.set(tabId, data);
|
|
}
|
|
|
|
clearData(tabId: TabId) {
|
|
this.data.delete(tabId);
|
|
}
|
|
|
|
@action
|
|
reset() {
|
|
for (const tabId of this.data.keys()) {
|
|
this.clearData(tabId);
|
|
}
|
|
this.storage?.reset();
|
|
}
|
|
}
|