2022-06-10 10:47:46 +00:00
|
|
|
import { reactive } from 'vue'
|
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-06-10 10:47:46 +00:00
|
|
|
import { songStore } from '@/stores'
|
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
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
async fetch () {
|
|
|
|
this.state.songs = songStore.syncWithVault(await httpService.get<Song[]>('recently-played'))
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-06-10 10:47:46 +00:00
|
|
|
async add (song: Song) {
|
|
|
|
if (!this.state.songs.length) {
|
|
|
|
await this.fetch()
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 16:06:45 +00:00
|
|
|
[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)
|
|
|
|
}
|
|
|
|
}
|