YesPlayMusic/packages/web/states/persistedUiStates.ts
2022-08-03 23:48:39 +08:00

36 lines
799 B
TypeScript

import { merge } from 'lodash-es'
import { proxy, subscribe } from 'valtio'
interface PersistedUiStates {
loginPhoneCountryCode: string
loginType: 'phone' | 'email' | 'qrCode'
minimizePlayer: boolean
}
const initPersistedUiStates: PersistedUiStates = {
loginPhoneCountryCode: '+86',
loginType: 'qrCode',
minimizePlayer: false,
}
const STORAGE_KEY = 'persistedUiStates'
const statesInStorage = localStorage.getItem(STORAGE_KEY)
let sates = {}
if (statesInStorage) {
try {
sates = JSON.parse(statesInStorage)
} catch {
// ignore
}
}
const persistedUiStates = proxy<PersistedUiStates>(
merge(initPersistedUiStates, sates)
)
subscribe(persistedUiStates, () => {
localStorage.setItem(STORAGE_KEY, JSON.stringify(persistedUiStates))
})
export default persistedUiStates