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

86 lines
2.2 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
2022-06-10 10:47:46 +00:00
cdn_url: string
current_user: User
current_version: string
latest_version: string
2024-01-08 16:59:05 +00:00
koel_plus: {
active: boolean
short_key: string | null
customer_name: string | null
customer_email: string | null
product_id: string
2024-01-08 16:59:05 +00:00
}
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
2024-01-05 16:42:50 +00:00
uses_you_tube: boolean
2024-01-04 11:35:36 +00:00
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,
2022-06-10 10:47:46 +00:00
cdn_url: '',
current_user: undefined as unknown as User,
current_version: '',
2024-01-08 16:59:05 +00:00
koel_plus: {
active: false,
short_key: null,
customer_name: null,
customer_email: null,
product_id: ''
2024-01-08 16:59:05 +00:00
},
2022-06-10 10:47:46 +00:00
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
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.preferences)
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
}
}