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

77 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'
import { http } from '@/services'
import { playlistFolderStore, playlistStore, preferenceStore, queueStore, settingStore, themeStore, userStore } from '.'
2022-04-15 14:24:30 +00:00
2022-04-24 08:50:45 +00:00
interface CommonStoreState {
2024-01-04 11:35:36 +00:00
allows_download: boolean
allows_upload: false,
2022-06-10 10:47:46 +00:00
cdn_url: string
current_user: User
current_version: string
latest_version: string
2024-01-04 11:35:36 +00:00
media_path_set: boolean
2022-04-15 14:24:30 +00:00
playlists: Playlist[]
playlist_folders: PlaylistFolder[]
2024-01-04 11:35:36 +00:00
queue_state: QueueState,
2022-04-15 14:24:30 +00:00
settings: Settings
2022-06-10 10:47:46 +00:00
song_count: number,
song_length: number,
2024-01-04 11:35:36 +00:00
uses_i_tunes: boolean
uses_last_fm: boolean
uses_spotify: boolean
uses_you_tube: boolean,
users: User[]
2022-04-15 14:24:30 +00:00
}
2022-04-24 08:50:45 +00:00
export const commonStore = {
state: reactive<CommonStoreState>({
2024-01-04 11:35:36 +00:00
allows_download: false,
allows_upload: false,
2022-06-10 10:47:46 +00:00
cdn_url: '',
current_user: undefined as unknown as User,
current_version: '',
latest_version: '',
2024-01-04 11:35:36 +00:00
media_path_set: false,
2022-04-15 14:24:30 +00:00
playlists: [],
playlist_folders: [],
2022-04-15 14:24:30 +00:00
settings: {} as Settings,
2024-01-04 11:35:36 +00:00
uses_i_tunes: false,
uses_last_fm: false,
uses_spotify: false,
2022-04-15 14:24:30 +00:00
users: [],
2024-01-04 11:35:36 +00:00
uses_you_tube: false,
2022-06-10 10:47:46 +00:00
song_count: 0,
song_length: 0,
queue_state: {
type: 'queue-states',
songs: [],
current_song: null,
playback_position: 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 http.get<CommonStoreState>('data'))
2022-04-15 14:24:30 +00:00
// Always disable YouTube integration on mobile.
2024-01-04 11:35:36 +00:00
this.state.uses_you_tube = this.state.uses_you_tube && !isMobile.phone
// Always disable uploading on mobile
this.state.allows_upload = this.state.allows_upload && !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)
playlistFolderStore.init(this.state.playlist_folders)
2022-04-15 14:24:30 +00:00
settingStore.init(this.state.settings)
queueStore.init(this.state.queue_state)
2022-04-15 14:24:30 +00:00
themeStore.init()
return this.state
}
}