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

87 lines
2.1 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'
2022-04-15 14:24:30 +00:00
2022-04-24 08:50:45 +00:00
import { httpService } from '@/services'
2022-04-15 14:24:30 +00:00
import {
userStore,
preferenceStore,
artistStore,
albumStore,
songStore,
playlistStore,
recentlyPlayedStore,
queueStore,
2022-04-24 08:50:45 +00:00
settingStore,
themeStore
2022-04-15 14:24:30 +00:00
} from '.'
2022-04-24 08:50:45 +00:00
interface CommonStoreState {
2022-04-15 14:24:30 +00:00
albums: Album[]
allowDownload: boolean
artists: Artist[]
cdnUrl: string
currentUser: User | undefined
currentVersion: string
favorites: Song[]
interactions: Interaction[]
latestVersion: string
originalMediaPath: string | undefined
playlists: Playlist[]
queued: Song[]
recentlyPlayed: string[]
settings: Settings
songs: Song[]
useiTunes: boolean
useLastfm: boolean
users: User[]
useYouTube: boolean
}
2022-04-24 08:50:45 +00:00
export const commonStore = {
state: reactive<CommonStoreState>({
2022-04-15 14:24:30 +00:00
albums: [],
allowDownload: false,
artists: [],
cdnUrl: '',
2022-04-20 15:57:53 +00:00
currentUser: undefined,
2022-04-15 14:24:30 +00:00
currentVersion: '',
favorites: [],
interactions: [],
latestVersion: '',
originalMediaPath: '',
playlists: [],
queued: [],
recentlyPlayed: [],
settings: {} as Settings,
songs: [],
useiTunes: false,
useLastfm: false,
users: [],
useYouTube: false
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.
this.state.useYouTube = this.state.useYouTube && !isMobile.phone
// If this is a new user, initialize his preferences to be an empty object.
this.state.currentUser!.preferences = this.state.currentUser!.preferences || {}
userStore.init(this.state.users, this.state.currentUser!)
preferenceStore.init(this.state.currentUser)
artistStore.init(this.state.artists)
albumStore.init(this.state.albums)
songStore.init(this.state.songs)
songStore.initInteractions(this.state.interactions)
recentlyPlayedStore.initExcerpt(this.state.recentlyPlayed)
playlistStore.init(this.state.playlists)
queueStore.init()
settingStore.init(this.state.settings)
themeStore.init()
return this.state
}
}