2021-04-26 16:41:12 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
2021-08-15 12:51:19 +02:00
|
|
|
import {notifySettingsChanged} from './nativeApp'
|
|
|
|
import {Utils} from './utils'
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-shadow
|
|
|
|
enum UserSettingKey {
|
|
|
|
Language = 'language',
|
|
|
|
Theme = 'theme',
|
2021-09-15 08:48:25 +02:00
|
|
|
LastWorkspaceId = 'lastWorkspaceId',
|
2021-08-15 12:51:19 +02:00
|
|
|
LastBoardId = 'lastBoardId',
|
|
|
|
LastViewId = 'lastViewId',
|
|
|
|
EmojiMartSkin = 'emoji-mart.skin',
|
|
|
|
EmojiMartLast = 'emoji-mart.last',
|
|
|
|
EmojiMartFrequently = 'emoji-mart.frequently',
|
|
|
|
RandomIcons = 'randomIcons'
|
|
|
|
}
|
|
|
|
|
2021-04-28 00:09:26 +02:00
|
|
|
export class UserSettings {
|
2021-08-15 12:51:19 +02:00
|
|
|
static get(key: UserSettingKey): string | null {
|
|
|
|
return localStorage.getItem(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
static set(key: UserSettingKey, value: string | null) {
|
|
|
|
if (!Object.values(UserSettingKey).includes(key)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (value === null) {
|
|
|
|
localStorage.removeItem(key)
|
|
|
|
} else {
|
|
|
|
localStorage.setItem(key, value)
|
|
|
|
}
|
|
|
|
notifySettingsChanged(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
static get language(): string | null {
|
|
|
|
return UserSettings.get(UserSettingKey.Language)
|
|
|
|
}
|
|
|
|
|
|
|
|
static set language(newValue: string | null) {
|
|
|
|
UserSettings.set(UserSettingKey.Language, newValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
static get theme(): string | null {
|
|
|
|
return UserSettings.get(UserSettingKey.Theme)
|
|
|
|
}
|
|
|
|
|
|
|
|
static set theme(newValue: string | null) {
|
|
|
|
UserSettings.set(UserSettingKey.Theme, newValue)
|
|
|
|
}
|
|
|
|
|
2021-09-15 08:48:25 +02:00
|
|
|
static get lastWorkspaceId(): string | null {
|
|
|
|
return UserSettings.get(UserSettingKey.LastWorkspaceId)
|
|
|
|
}
|
|
|
|
|
|
|
|
static set lastWorkspaceId(newValue: string | null) {
|
|
|
|
UserSettings.set(UserSettingKey.LastWorkspaceId, newValue)
|
|
|
|
}
|
|
|
|
|
2021-08-15 12:51:19 +02:00
|
|
|
static get lastBoardId(): string | null {
|
|
|
|
return UserSettings.get(UserSettingKey.LastBoardId)
|
|
|
|
}
|
|
|
|
|
|
|
|
static set lastBoardId(newValue: string | null) {
|
|
|
|
UserSettings.set(UserSettingKey.LastBoardId, newValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
static get lastViewId(): string | null {
|
|
|
|
return UserSettings.get(UserSettingKey.LastViewId)
|
|
|
|
}
|
|
|
|
|
|
|
|
static set lastViewId(newValue: string | null) {
|
|
|
|
UserSettings.set(UserSettingKey.LastViewId, newValue)
|
|
|
|
}
|
|
|
|
|
2021-04-26 16:41:12 +02:00
|
|
|
static get prefillRandomIcons(): boolean {
|
2021-08-15 12:51:19 +02:00
|
|
|
return UserSettings.get(UserSettingKey.RandomIcons) !== 'false'
|
2021-04-26 16:41:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static set prefillRandomIcons(newValue: boolean) {
|
2021-08-15 12:51:19 +02:00
|
|
|
UserSettings.set(UserSettingKey.RandomIcons, JSON.stringify(newValue))
|
|
|
|
}
|
|
|
|
|
|
|
|
static getEmojiMartSetting(key: string): any {
|
|
|
|
const prefixed = `emoji-mart.${key}`
|
|
|
|
Utils.assert((Object as any).values(UserSettingKey).includes(prefixed))
|
|
|
|
const json = UserSettings.get(prefixed as UserSettingKey)
|
|
|
|
return json ? JSON.parse(json) : null
|
2021-04-26 16:41:12 +02:00
|
|
|
}
|
|
|
|
|
2021-08-15 12:51:19 +02:00
|
|
|
static setEmojiMartSetting(key: string, value: any) {
|
|
|
|
const prefixed = `emoji-mart.${key}`
|
|
|
|
Utils.assert((Object as any).values(UserSettingKey).includes(prefixed))
|
|
|
|
UserSettings.set(prefixed as UserSettingKey, JSON.stringify(value))
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 00:09:26 +02:00
|
|
|
|
|
|
|
export function exportUserSettingsBlob(): string {
|
|
|
|
return window.btoa(exportUserSettings())
|
|
|
|
}
|
|
|
|
|
|
|
|
function exportUserSettings(): string {
|
2021-08-15 12:51:19 +02:00
|
|
|
const keys = Object.values(UserSettingKey)
|
2021-04-28 00:09:26 +02:00
|
|
|
const settings = Object.fromEntries(keys.map((key) => [key, localStorage.getItem(key)]))
|
|
|
|
settings.timestamp = `${Date.now()}`
|
|
|
|
return JSON.stringify(settings)
|
|
|
|
}
|
|
|
|
|
2021-08-15 12:51:19 +02:00
|
|
|
export function importUserSettingsBlob(blob: string): string[] {
|
2021-04-28 00:09:26 +02:00
|
|
|
return importUserSettings(window.atob(blob))
|
|
|
|
}
|
|
|
|
|
2021-08-15 12:51:19 +02:00
|
|
|
function importUserSettings(json: string): string[] {
|
2021-04-28 00:09:26 +02:00
|
|
|
const settings = parseUserSettings(json)
|
2021-08-15 12:51:19 +02:00
|
|
|
if (!settings) {
|
|
|
|
return []
|
|
|
|
}
|
2021-04-28 00:09:26 +02:00
|
|
|
const timestamp = settings.timestamp
|
|
|
|
const lastTimestamp = localStorage.getItem('timestamp')
|
|
|
|
if (!timestamp || (lastTimestamp && Number(timestamp) <= Number(lastTimestamp))) {
|
2021-08-15 12:51:19 +02:00
|
|
|
return []
|
2021-04-28 00:09:26 +02:00
|
|
|
}
|
2021-08-15 12:51:19 +02:00
|
|
|
const importedKeys = []
|
2021-04-28 00:09:26 +02:00
|
|
|
for (const [key, value] of Object.entries(settings)) {
|
2021-08-15 12:51:19 +02:00
|
|
|
if (Object.values(UserSettingKey).includes(key as UserSettingKey)) {
|
|
|
|
if (value) {
|
|
|
|
localStorage.setItem(key, value as string)
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem(key)
|
|
|
|
}
|
|
|
|
importedKeys.push(key)
|
2021-05-24 18:59:30 +02:00
|
|
|
}
|
2021-04-28 00:09:26 +02:00
|
|
|
}
|
2021-08-15 12:51:19 +02:00
|
|
|
return importedKeys
|
2021-04-28 00:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseUserSettings(json: string): any {
|
|
|
|
try {
|
|
|
|
return JSON.parse(json)
|
|
|
|
} catch (e) {
|
2021-08-15 12:51:19 +02:00
|
|
|
return undefined
|
2021-04-28 00:09:26 +02:00
|
|
|
}
|
|
|
|
}
|