YesPlayMusic/packages/web/states/persistedUiStates.ts

36 lines
799 B
TypeScript
Raw Normal View History

2022-08-03 23:48:39 +08:00
import { merge } from 'lodash-es'
2022-07-11 11:06:41 +08:00
import { proxy, subscribe } from 'valtio'
interface PersistedUiStates {
loginPhoneCountryCode: string
loginType: 'phone' | 'email' | 'qrCode'
2022-08-03 23:48:39 +08:00
minimizePlayer: boolean
2022-07-11 11:06:41 +08:00
}
const initPersistedUiStates: PersistedUiStates = {
loginPhoneCountryCode: '+86',
loginType: 'qrCode',
2022-08-03 23:48:39 +08:00
minimizePlayer: false,
2022-07-11 11:06:41 +08:00
}
2022-08-03 23:48:39 +08:00
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)
)
2022-07-11 11:06:41 +08:00
subscribe(persistedUiStates, () => {
2022-08-03 23:48:39 +08:00
localStorage.setItem(STORAGE_KEY, JSON.stringify(persistedUiStates))
2022-07-11 11:06:41 +08:00
})
export default persistedUiStates