koel/resources/assets/js/stores/commonStore.ts

59 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
import isMobile from 'ismobilejs'
2022-04-24 08:50:45 +00:00
import { reactive } from 'vue'
import { httpService } from '@/services'
2022-06-10 10:47:46 +00:00
import { playlistStore, preferenceStore, settingStore, themeStore, userStore } from '@/stores'
2022-04-15 14:24:30 +00:00
2022-04-24 08:50:45 +00:00
interface CommonStoreState {
2022-06-10 10:47:46 +00:00
allow_download: boolean
cdn_url: string
current_user: User
current_version: string
latest_version: string
2022-04-15 14:24:30 +00:00
playlists: Playlist[]
settings: Settings
2022-06-10 10:47:46 +00:00
use_i_tunes: boolean
use_last_fm: boolean
2022-07-16 22:42:29 +00:00
use_spotify: boolean
2022-04-15 14:24:30 +00:00
users: User[]
2022-06-10 10:47:46 +00:00
use_you_tube: boolean,
song_count: number,
song_length: number
2022-04-15 14:24:30 +00:00
}
2022-04-24 08:50:45 +00:00
export const commonStore = {
state: reactive<CommonStoreState>({
2022-06-10 10:47:46 +00:00
allow_download: false,
cdn_url: '',
current_user: undefined as unknown as User,
current_version: '',
latest_version: '',
2022-04-15 14:24:30 +00:00
playlists: [],
settings: {} as Settings,
2022-06-10 10:47:46 +00:00
use_i_tunes: false,
use_last_fm: false,
2022-07-16 22:42:29 +00:00
use_spotify: false,
2022-04-15 14:24:30 +00:00
users: [],
2022-06-10 10:47:46 +00:00
use_you_tube: false,
song_count: 0,
song_length: 0
2022-04-20 15:57:53 +00:00
}),
2022-04-15 14:24:30 +00:00
2022-04-24 08:50:45 +00:00
async init () {
Object.assign(this.state, await httpService.get<CommonStoreState>('data'))
2022-04-15 14:24:30 +00:00
// Always disable YouTube integration on mobile.
2022-06-10 10:47:46 +00:00
this.state.use_you_tube = this.state.use_you_tube && !isMobile.phone
2022-04-15 14:24:30 +00:00
// If this is a new user, initialize his preferences to be an empty object.
2022-06-10 10:47:46 +00:00
this.state.current_user.preferences = this.state.current_user.preferences || {}
2022-04-15 14:24:30 +00:00
2022-06-10 10:47:46 +00:00
userStore.init(this.state.current_user)
preferenceStore.init(this.state.current_user)
2022-04-15 14:24:30 +00:00
playlistStore.init(this.state.playlists)
settingStore.init(this.state.settings)
themeStore.init()
return this.state
}
}