YesPlayMusic/packages/web/states/settings.ts
2023-03-03 03:12:27 +08:00

53 lines
1.5 KiB
TypeScript

import { IpcChannels } from '@/shared/IpcChannels'
import { merge } from 'lodash-es'
import { proxy, subscribe } from 'valtio'
import i18n, { getInitLanguage, SupportedLanguage, supportedLanguages } from '../i18n/i18n'
interface Settings {
accentColor: string
language: SupportedLanguage
enableFindTrackOnYouTube: boolean
httpProxyForYouTube?: {
host: string
port: number
protocol: 'http' | 'https'
auth?: {
username: string
password: string
}
}
playAnimatedArtworkFromApple: boolean
priorityDisplayOfAlbumArtistDescriptionFromAppleMusic: boolean
displayPlaylistsFromNeteaseMusic: boolean
}
const initSettings: Settings = {
accentColor: 'green',
language: getInitLanguage(),
enableFindTrackOnYouTube: false,
playAnimatedArtworkFromApple: true,
priorityDisplayOfAlbumArtistDescriptionFromAppleMusic: true,
displayPlaylistsFromNeteaseMusic: true,
}
const STORAGE_KEY = 'settings'
let statesInStorage = {}
try {
statesInStorage = JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}')
} catch {
// ignore
}
const settings = proxy<Settings>(merge(initSettings, statesInStorage))
subscribe(settings, () => {
if (settings.language !== i18n.language && supportedLanguages.includes(settings.language)) {
i18n.changeLanguage(settings.language)
}
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings))
window.ipcRenderer?.send(IpcChannels.SyncSettings, JSON.parse(JSON.stringify(settings)))
})
export default settings