2022-04-15 14:24:30 +00:00
|
|
|
import { songStore } from '.'
|
2022-04-24 08:50:45 +00:00
|
|
|
import { httpService } from '@/services'
|
2022-04-15 14:24:30 +00:00
|
|
|
import { remove } from 'lodash'
|
2022-04-21 16:06:45 +00:00
|
|
|
import { reactive } from 'vue'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
const EXCERPT_COUNT = 7
|
|
|
|
|
|
|
|
export const recentlyPlayedStore = {
|
2022-04-21 16:06:45 +00:00
|
|
|
excerptState: reactive({
|
2022-04-15 14:24:30 +00:00
|
|
|
songs: [] as Song[]
|
2022-04-21 16:06:45 +00:00
|
|
|
}),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-21 16:06:45 +00:00
|
|
|
state: reactive({
|
2022-04-15 14:24:30 +00:00
|
|
|
songs: [] as Song[]
|
2022-04-21 16:06:45 +00:00
|
|
|
}),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
fetched: false,
|
|
|
|
|
2022-04-21 16:06:45 +00:00
|
|
|
initExcerpt (songIds: string[]) {
|
2022-04-15 14:24:30 +00:00
|
|
|
this.excerptState.songs = songStore.byIds(songIds)
|
|
|
|
},
|
|
|
|
|
2022-04-21 16:06:45 +00:00
|
|
|
async fetchAll () {
|
2022-04-15 14:24:30 +00:00
|
|
|
if (!this.fetched) {
|
2022-04-24 08:50:45 +00:00
|
|
|
this.state.songs = songStore.byIds(await httpService.get<string[]>(`interaction/recently-played`))
|
2022-04-15 14:24:30 +00:00
|
|
|
this.fetched = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.state.songs
|
|
|
|
},
|
|
|
|
|
2022-04-21 16:06:45 +00:00
|
|
|
add (song: Song) {
|
|
|
|
[this.state, this.excerptState].forEach(state => {
|
2022-04-15 14:24:30 +00:00
|
|
|
// make sure there's no duplicate
|
|
|
|
remove(state.songs, s => s.id === song.id)
|
|
|
|
state.songs.unshift(song)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.excerptState.songs.splice(EXCERPT_COUNT)
|
|
|
|
}
|
|
|
|
}
|