mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Refine UserStore from/to usage (#3292)
This commit is contained in:
parent
600bd659f6
commit
dd819bb534
@ -5,14 +5,15 @@
|
|||||||
|
|
||||||
import { app, ipcMain } from "electron";
|
import { app, ipcMain } from "electron";
|
||||||
import semver, { SemVer } from "semver";
|
import semver, { SemVer } from "semver";
|
||||||
import { action, computed, makeObservable, observable, reaction } from "mobx";
|
import { action, computed, observable, reaction, makeObservable, isObservableArray, isObservableSet, isObservableMap } from "mobx";
|
||||||
import { BaseStore } from "../base-store";
|
import { BaseStore } from "../base-store";
|
||||||
import migrations, { fileNameMigration } from "../../migrations/user-store";
|
import migrations, { fileNameMigration } from "../../migrations/user-store";
|
||||||
import { getAppVersion } from "../utils/app-version";
|
import { getAppVersion } from "../utils/app-version";
|
||||||
import { kubeConfigDefaultPath } from "../kube-helpers";
|
import { kubeConfigDefaultPath } from "../kube-helpers";
|
||||||
import { appEventBus } from "../app-event-bus/event-bus";
|
import { appEventBus } from "../app-event-bus/event-bus";
|
||||||
import { getOrInsertSet, toggle, toJS } from "../../renderer/utils";
|
import { getOrInsertSet, toggle, toJS, entries, fromEntries } from "../../renderer/utils";
|
||||||
import { DESCRIPTORS, EditorConfiguration, ExtensionRegistry, KubeconfigSyncValue, UserPreferencesModel, TerminalConfig } from "./preferences-helpers";
|
import { DESCRIPTORS } from "./preferences-helpers";
|
||||||
|
import type { EditorConfiguration, ExtensionRegistry, KubeconfigSyncValue, UserPreferencesModel, TerminalConfig } from "./preferences-helpers";
|
||||||
import logger from "../../main/logger";
|
import logger from "../../main/logger";
|
||||||
|
|
||||||
export interface UserStoreModel {
|
export interface UserStoreModel {
|
||||||
@ -164,55 +165,31 @@ export class UserStore extends BaseStore<UserStoreModel> /* implements UserStore
|
|||||||
this.lastSeenAppVersion = lastSeenAppVersion;
|
this.lastSeenAppVersion = lastSeenAppVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.httpsProxy = DESCRIPTORS.httpsProxy.fromStore(preferences?.httpsProxy);
|
for (const [key, { fromStore }] of entries(DESCRIPTORS)) {
|
||||||
this.shell = DESCRIPTORS.shell.fromStore(preferences?.shell);
|
const curVal = this[key];
|
||||||
this.colorTheme = DESCRIPTORS.colorTheme.fromStore(preferences?.colorTheme);
|
const newVal = fromStore((preferences)?.[key] as never) as never;
|
||||||
this.terminalTheme = DESCRIPTORS.terminalTheme.fromStore(preferences?.terminalTheme);
|
|
||||||
this.localeTimezone = DESCRIPTORS.localeTimezone.fromStore(preferences?.localeTimezone);
|
if (
|
||||||
this.allowUntrustedCAs = DESCRIPTORS.allowUntrustedCAs.fromStore(preferences?.allowUntrustedCAs);
|
isObservableArray(curVal)
|
||||||
this.allowTelemetry = DESCRIPTORS.allowTelemetry.fromStore(preferences?.allowTelemetry);
|
|| isObservableSet(curVal)
|
||||||
this.allowErrorReporting = DESCRIPTORS.allowErrorReporting.fromStore(preferences?.allowErrorReporting);
|
|| isObservableMap(curVal)
|
||||||
this.downloadMirror = DESCRIPTORS.downloadMirror.fromStore(preferences?.downloadMirror);
|
) {
|
||||||
this.downloadKubectlBinaries = DESCRIPTORS.downloadKubectlBinaries.fromStore(preferences?.downloadKubectlBinaries);
|
curVal.replace(newVal);
|
||||||
this.downloadBinariesPath = DESCRIPTORS.downloadBinariesPath.fromStore(preferences?.downloadBinariesPath);
|
} else {
|
||||||
this.kubectlBinariesPath = DESCRIPTORS.kubectlBinariesPath.fromStore(preferences?.kubectlBinariesPath);
|
this[key] = newVal;
|
||||||
this.openAtLogin = DESCRIPTORS.openAtLogin.fromStore(preferences?.openAtLogin);
|
}
|
||||||
this.hiddenTableColumns.replace(DESCRIPTORS.hiddenTableColumns.fromStore(preferences?.hiddenTableColumns));
|
}
|
||||||
this.syncKubeconfigEntries.replace(DESCRIPTORS.syncKubeconfigEntries.fromStore(preferences?.syncKubeconfigEntries));
|
|
||||||
this.editorConfiguration = DESCRIPTORS.editorConfiguration.fromStore(preferences?.editorConfiguration);
|
|
||||||
this.terminalCopyOnSelect = DESCRIPTORS.terminalCopyOnSelect.fromStore(preferences?.terminalCopyOnSelect);
|
|
||||||
this.terminalConfig = DESCRIPTORS.terminalConfig.fromStore(preferences?.terminalConfig);
|
|
||||||
this.updateChannel = DESCRIPTORS.updateChannel.fromStore(preferences?.updateChannel);
|
|
||||||
this.extensionRegistryUrl = DESCRIPTORS.extensionRegistryUrl.fromStore(preferences?.extensionRegistryUrl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON(): UserStoreModel {
|
toJSON(): UserStoreModel {
|
||||||
const model: UserStoreModel = {
|
const preferences = fromEntries(
|
||||||
lastSeenAppVersion: this.lastSeenAppVersion,
|
entries(DESCRIPTORS)
|
||||||
preferences: {
|
.map(([key, { toStore }]) => [key, toStore(this[key] as never)]),
|
||||||
httpsProxy: DESCRIPTORS.httpsProxy.toStore(this.httpsProxy),
|
) as UserPreferencesModel;
|
||||||
shell: DESCRIPTORS.shell.toStore(this.shell),
|
|
||||||
colorTheme: DESCRIPTORS.colorTheme.toStore(this.colorTheme),
|
|
||||||
terminalTheme: DESCRIPTORS.terminalTheme.toStore(this.terminalTheme),
|
|
||||||
localeTimezone: DESCRIPTORS.localeTimezone.toStore(this.localeTimezone),
|
|
||||||
allowUntrustedCAs: DESCRIPTORS.allowUntrustedCAs.toStore(this.allowUntrustedCAs),
|
|
||||||
allowTelemetry: DESCRIPTORS.allowTelemetry.toStore(this.allowTelemetry),
|
|
||||||
allowErrorReporting: DESCRIPTORS.allowErrorReporting.toStore(this.allowErrorReporting),
|
|
||||||
downloadMirror: DESCRIPTORS.downloadMirror.toStore(this.downloadMirror),
|
|
||||||
downloadKubectlBinaries: DESCRIPTORS.downloadKubectlBinaries.toStore(this.downloadKubectlBinaries),
|
|
||||||
downloadBinariesPath: DESCRIPTORS.downloadBinariesPath.toStore(this.downloadBinariesPath),
|
|
||||||
kubectlBinariesPath: DESCRIPTORS.kubectlBinariesPath.toStore(this.kubectlBinariesPath),
|
|
||||||
openAtLogin: DESCRIPTORS.openAtLogin.toStore(this.openAtLogin),
|
|
||||||
hiddenTableColumns: DESCRIPTORS.hiddenTableColumns.toStore(this.hiddenTableColumns),
|
|
||||||
syncKubeconfigEntries: DESCRIPTORS.syncKubeconfigEntries.toStore(this.syncKubeconfigEntries),
|
|
||||||
editorConfiguration: DESCRIPTORS.editorConfiguration.toStore(this.editorConfiguration),
|
|
||||||
terminalCopyOnSelect: DESCRIPTORS.terminalCopyOnSelect.toStore(this.terminalCopyOnSelect),
|
|
||||||
terminalConfig: DESCRIPTORS.terminalConfig.toStore(this.terminalConfig),
|
|
||||||
updateChannel: DESCRIPTORS.updateChannel.toStore(this.updateChannel),
|
|
||||||
extensionRegistryUrl: DESCRIPTORS.extensionRegistryUrl.toStore(this.extensionRegistryUrl),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return toJS(model);
|
return toJS({
|
||||||
|
lastSeenAppVersion: this.lastSeenAppVersion,
|
||||||
|
preferences,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,10 @@
|
|||||||
* A better typed version of `Object.fromEntries` where the keys are known to
|
* A better typed version of `Object.fromEntries` where the keys are known to
|
||||||
* be a specific subset
|
* be a specific subset
|
||||||
*/
|
*/
|
||||||
export function fromEntries<T, Key extends string>(entries: Iterable<readonly [Key, T]>): { [k in Key]: T } {
|
export function fromEntries<T, Key extends string>(entries: Iterable<readonly [Key, T]>): Record<Key, T> {
|
||||||
return Object.fromEntries(entries) as { [k in Key]: T };
|
return Object.fromEntries(entries) as { [k in Key]: T };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function entries<T extends Record<string, any>>(obj: T): [keyof T, T[keyof T]][] {
|
||||||
|
return Object.entries(obj);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user