1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/common/user-store.ts
Roman d7abfe3857 preferences page -- part 2
Signed-off-by: Roman <ixrock@gmail.com>
2020-07-27 09:14:16 -04:00

93 lines
2.4 KiB
TypeScript

import semver from "semver"
import { action, observable, reaction, toJS } from "mobx";
import { BaseStore } from "./base-store";
import migrations from "../migrations/user-store"
import { getAppVersion } from "./utils/app-version";
import { tracker } from "./tracker";
// todo: merge with config.store.ts + theme.store.ts
// fixme: detect new contexts from .kube/config since last open
export enum ThemeType {
LIGHT = "light",
DARK = "dark",
}
export interface Theme {
name: string;
type: ThemeType;
colors?: Record<string, string>;
}
export interface UserStoreModel {
lastSeenAppVersion: string;
seenContexts: string[];
preferences: UserPreferences;
}
export interface UserPreferences {
httpsProxy?: string;
colorTheme?: string | "dark" | "light";
allowUntrustedCAs?: boolean;
allowTelemetry?: boolean;
downloadMirror?: string | "default";
}
export class UserStore extends BaseStore<UserStoreModel> {
private constructor() {
super({
// configName: "lens-user-store", // todo: migrate from default filename
migrations: migrations,
});
// track telemetry availability
reaction(() => this.preferences.allowTelemetry, allowed => {
tracker.event("telemetry", allowed ? "enabled" : "disabled");
});
}
@observable lastSeenAppVersion = "0.0.0"
@observable seenContexts: string[] = [];
@observable newContexts: string[] = [];
@observable preferences: UserPreferences = {
allowTelemetry: true,
allowUntrustedCAs: false,
colorTheme: "dark",
downloadMirror: "default",
httpsProxy: "",
};
get isNewVersion() {
return semver.gt(getAppVersion(), this.lastSeenAppVersion);
}
@action
saveLastSeenAppVersion() {
tracker.event("app", "whats-new-seen")
this.lastSeenAppVersion = getAppVersion();
}
@action
protected fromStore(data: Partial<UserStoreModel> = {}) {
const { lastSeenAppVersion, seenContexts = [], preferences } = data
if (lastSeenAppVersion) {
this.lastSeenAppVersion = lastSeenAppVersion;
}
this.seenContexts = seenContexts;
Object.assign(this.preferences, preferences);
}
toJSON() {
return toJS({
lastSeenAppVersion: this.lastSeenAppVersion,
seenContexts: Array.from(this.seenContexts),
preferences: this.preferences,
}, {
recurseEverything: true,
})
}
}
export const userStore = UserStore.getInstance<UserStore>();