2022-03-13 14:40:38 +08:00
|
|
|
import { proxy, subscribe } from 'valtio'
|
|
|
|
import { devtools } from 'valtio/utils'
|
2022-04-09 00:28:37 +08:00
|
|
|
import { Player } from '@/renderer/utils/player'
|
2022-04-29 19:16:34 +08:00
|
|
|
import {merge} from 'lodash-es'
|
2022-03-13 14:40:38 +08:00
|
|
|
|
|
|
|
interface Store {
|
|
|
|
uiStates: {
|
|
|
|
loginPhoneCountryCode: string
|
2022-04-08 01:02:25 +08:00
|
|
|
showLyricPanel: boolean
|
2022-03-13 14:40:38 +08:00
|
|
|
}
|
|
|
|
settings: {
|
|
|
|
showSidebar: boolean
|
2022-04-17 12:46:06 +08:00
|
|
|
accentColor: string
|
2022-03-13 14:40:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: Store = {
|
|
|
|
uiStates: {
|
|
|
|
loginPhoneCountryCode: '+86',
|
2022-04-08 01:02:25 +08:00
|
|
|
showLyricPanel: false,
|
2022-03-13 14:40:38 +08:00
|
|
|
},
|
|
|
|
settings: {
|
|
|
|
showSidebar: true,
|
2022-04-17 12:46:06 +08:00
|
|
|
accentColor: 'blue',
|
2022-03-13 14:40:38 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const stateInLocalStorage = localStorage.getItem('state')
|
|
|
|
export const state = proxy<Store>(
|
2022-04-29 19:16:34 +08:00
|
|
|
merge(initialState, stateInLocalStorage ? JSON.parse(stateInLocalStorage) : {})
|
2022-03-13 14:40:38 +08:00
|
|
|
)
|
|
|
|
subscribe(state, () => {
|
|
|
|
localStorage.setItem('state', JSON.stringify(state))
|
|
|
|
})
|
|
|
|
|
2022-04-08 01:02:25 +08:00
|
|
|
// player
|
2022-04-05 23:02:37 +08:00
|
|
|
const playerInLocalStorage = localStorage.getItem('player')
|
|
|
|
export const player = proxy(new Player())
|
|
|
|
player.init((playerInLocalStorage && JSON.parse(playerInLocalStorage)) || {})
|
|
|
|
subscribe(player, () => {
|
|
|
|
localStorage.setItem('player', JSON.stringify(player))
|
|
|
|
})
|
2022-04-02 16:54:37 +08:00
|
|
|
|
|
|
|
if (import.meta.env.DEV) {
|
2022-04-02 19:29:43 +08:00
|
|
|
;(window as any).player = player
|
2022-04-02 16:54:37 +08:00
|
|
|
}
|
2022-03-13 14:40:38 +08:00
|
|
|
|
|
|
|
// Devtools
|
|
|
|
devtools(state, 'state')
|
|
|
|
devtools(player, 'player')
|