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

62 lines
1.7 KiB
TypeScript
Raw Normal View History

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