YesPlayMusic/packages/web/states/settings.ts

65 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-07-11 11:06:41 +08:00
import { IpcChannels } from '@/shared/IpcChannels'
import { merge } from 'lodash-es'
import { proxy, subscribe } from 'valtio'
2022-10-28 20:29:04 +08:00
import i18n, { getLanguage, supportedLanguages } from '../i18n/i18n'
2022-07-11 11:06:41 +08:00
interface Settings {
accentColor: string
2022-10-28 20:29:04 +08:00
language: typeof supportedLanguages[number]
2022-07-11 11:06:41 +08:00
unm: {
enabled: boolean
sources: Array<
'migu' | 'kuwo' | 'kugou' | 'ytdl' | 'qq' | 'bilibili' | 'joox'
>
searchMode: 'order-first' | 'fast-first'
proxy: null | {
protocol: 'http' | 'https' | 'socks5'
host: string
port: number
username?: string
password?: string
}
cookies: {
qq?: string
joox?: string
}
}
}
const initSettings: Settings = {
accentColor: 'blue',
2022-10-28 20:29:04 +08:00
language: getLanguage(),
2022-07-11 11:06:41 +08:00
unm: {
enabled: true,
sources: ['migu'],
searchMode: 'order-first',
proxy: null,
cookies: {},
},
}
2022-08-03 23:48:39 +08:00
const STORAGE_KEY = 'settings'
2022-10-28 20:29:04 +08:00
2022-08-03 23:48:39 +08:00
let statesInStorage = {}
2022-10-28 20:29:04 +08:00
try {
statesInStorage = JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}')
} catch {
// ignore
2022-08-03 23:48:39 +08:00
}
const settings = proxy<Settings>(merge(initSettings, statesInStorage))
2022-07-11 11:06:41 +08:00
subscribe(settings, () => {
2022-10-28 20:29:04 +08:00
if (
settings.language !== i18n.language &&
supportedLanguages.includes(settings.language)
) {
i18n.changeLanguage(settings.language)
}
2022-07-11 11:06:41 +08:00
window.ipcRenderer?.send(IpcChannels.SyncSettings, settings)
2022-08-03 23:48:39 +08:00
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings))
2022-07-11 11:06:41 +08:00
})
export default settings