diff --git a/src/common/user-store/user-store.ts b/src/common/user-store/user-store.ts index 9d16b2c419..b72c8f6859 100644 --- a/src/common/user-store/user-store.ts +++ b/src/common/user-store/user-store.ts @@ -5,14 +5,15 @@ import { app, ipcMain } from "electron"; 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 migrations, { fileNameMigration } from "../../migrations/user-store"; import { getAppVersion } from "../utils/app-version"; import { kubeConfigDefaultPath } from "../kube-helpers"; import { appEventBus } from "../app-event-bus/event-bus"; -import { getOrInsertSet, toggle, toJS } from "../../renderer/utils"; -import { DESCRIPTORS, EditorConfiguration, ExtensionRegistry, KubeconfigSyncValue, UserPreferencesModel, TerminalConfig } from "./preferences-helpers"; +import { getOrInsertSet, toggle, toJS, entries, fromEntries } from "../../renderer/utils"; +import { DESCRIPTORS } from "./preferences-helpers"; +import type { EditorConfiguration, ExtensionRegistry, KubeconfigSyncValue, UserPreferencesModel, TerminalConfig } from "./preferences-helpers"; import logger from "../../main/logger"; export interface UserStoreModel { @@ -164,55 +165,31 @@ export class UserStore extends BaseStore /* implements UserStore this.lastSeenAppVersion = lastSeenAppVersion; } - this.httpsProxy = DESCRIPTORS.httpsProxy.fromStore(preferences?.httpsProxy); - this.shell = DESCRIPTORS.shell.fromStore(preferences?.shell); - this.colorTheme = DESCRIPTORS.colorTheme.fromStore(preferences?.colorTheme); - this.terminalTheme = DESCRIPTORS.terminalTheme.fromStore(preferences?.terminalTheme); - this.localeTimezone = DESCRIPTORS.localeTimezone.fromStore(preferences?.localeTimezone); - this.allowUntrustedCAs = DESCRIPTORS.allowUntrustedCAs.fromStore(preferences?.allowUntrustedCAs); - this.allowTelemetry = DESCRIPTORS.allowTelemetry.fromStore(preferences?.allowTelemetry); - this.allowErrorReporting = DESCRIPTORS.allowErrorReporting.fromStore(preferences?.allowErrorReporting); - this.downloadMirror = DESCRIPTORS.downloadMirror.fromStore(preferences?.downloadMirror); - this.downloadKubectlBinaries = DESCRIPTORS.downloadKubectlBinaries.fromStore(preferences?.downloadKubectlBinaries); - this.downloadBinariesPath = DESCRIPTORS.downloadBinariesPath.fromStore(preferences?.downloadBinariesPath); - this.kubectlBinariesPath = DESCRIPTORS.kubectlBinariesPath.fromStore(preferences?.kubectlBinariesPath); - 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); + for (const [key, { fromStore }] of entries(DESCRIPTORS)) { + const curVal = this[key]; + const newVal = fromStore((preferences)?.[key] as never) as never; + + if ( + isObservableArray(curVal) + || isObservableSet(curVal) + || isObservableMap(curVal) + ) { + curVal.replace(newVal); + } else { + this[key] = newVal; + } + } } toJSON(): UserStoreModel { - const model: UserStoreModel = { - lastSeenAppVersion: this.lastSeenAppVersion, - preferences: { - httpsProxy: DESCRIPTORS.httpsProxy.toStore(this.httpsProxy), - 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), - }, - }; + const preferences = fromEntries( + entries(DESCRIPTORS) + .map(([key, { toStore }]) => [key, toStore(this[key] as never)]), + ) as UserPreferencesModel; - return toJS(model); + return toJS({ + lastSeenAppVersion: this.lastSeenAppVersion, + preferences, + }); } } diff --git a/src/common/utils/objects.ts b/src/common/utils/objects.ts index 0f213aa5f5..342d1b00a1 100644 --- a/src/common/utils/objects.ts +++ b/src/common/utils/objects.ts @@ -7,6 +7,10 @@ * A better typed version of `Object.fromEntries` where the keys are known to * be a specific subset */ -export function fromEntries(entries: Iterable): { [k in Key]: T } { +export function fromEntries(entries: Iterable): Record { return Object.fromEntries(entries) as { [k in Key]: T }; } + +export function entries>(obj: T): [keyof T, T[keyof T]][] { + return Object.entries(obj); +}