[web] Fix for unexpected calls to the API #1688

Lyrics feature doesn't mess anymore when the user is playing tracks from Spotify
This commit is contained in:
Alain Nussbaumer
2023-11-24 13:58:30 +01:00
parent f19e9fb48b
commit 91c1e5b174
4 changed files with 31 additions and 42 deletions

View File

@@ -305,13 +305,14 @@ export default {
}, },
update_lyrics() { update_lyrics() {
let track = this.$store.state.queue.items.filter( const track = this.$store.getters.now_playing
(e) => e.id == this.$store.state.player.item_id if (track && track.track_id) {
) webapi.library_track(track.track_id).then(({ data }) => {
if (track.length >= 1)
webapi.library_track(track[0].track_id).then(({ data }) => {
this.$store.commit(types.UPDATE_LYRICS, data) this.$store.commit(types.UPDATE_LYRICS, data)
}) })
} else {
this.$store.commit(types.UPDATE_LYRICS)
}
}, },
update_settings() { update_settings() {

View File

@@ -36,8 +36,7 @@ export default {
methods: { methods: {
toggle_lyrics() { toggle_lyrics() {
this.$store.state.lyrics.lyrics_pane = this.$store.state.lyrics.pane = !this.$store.state.lyrics.pane
!this.$store.state.lyrics.lyrics_pane
} }
} }
} }

View File

@@ -34,10 +34,8 @@ export default createStore({
item_progress_ms: 0 item_progress_ms: 0
}, },
lyrics: { lyrics: {
found: false, pane: false,
lyrics_id: -1, content: []
lyrics_pane: false,
lyrics: []
}, },
queue: { queue: {
version: 0, version: 0,
@@ -76,17 +74,9 @@ export default createStore({
}, },
getters: { getters: {
lyrics: (state) => { lyrics: (state) => state.lyrics.content,
return state.lyrics.lyrics
},
lyrics_found: (state) => { lyrics_pane: (state) => state.lyrics.pane,
return state.lyrics.found
},
lyrics_pane: (state) => {
return state.lyrics.lyrics_pane
},
now_playing: (state) => { now_playing: (state) => {
const item = state.queue.items.find(function (item) { const item = state.queue.items.find(function (item) {
@@ -206,27 +196,28 @@ export default createStore({
[types.UPDATE_QUEUE](state, queue) { [types.UPDATE_QUEUE](state, queue) {
state.queue = queue state.queue = queue
}, },
[types.UPDATE_LYRICS](state, lyrics) { [types.UPDATE_LYRICS](state, track) {
// Parse from .LRC or text format to synchronized lyrics // Parse from .LRC or text format to synchronized lyrics
function parse(lyrics) { function parse(lyrics) {
let lyricsObj = [] if (lyrics) {
let tempArr = lyrics.split('\n') const lyricsObj = []
const regex = /(\[(\d+):(\d+)(?:\.\d+)?\] ?)?(.*)/ const regex = /(\[(\d+):(\d+)(?:\.\d+)?\] ?)?(.*)/
lyrics.split('\n').forEach((item) => {
tempArr.forEach((item) => { const matches = regex.exec(item)
let matches = regex.exec(item)
if (matches !== null && matches[4].length) { if (matches !== null && matches[4].length) {
let obj = [matches[4]] const obj = [matches[4]]
if (matches[2] != null && matches[3] != null) if (matches[2] != null && matches[3] != null)
obj.push(parseInt(matches[2], 10) * 60 + parseInt(matches[3], 10)) obj.push(
parseInt(matches[2], 10) * 60 + parseInt(matches[3], 10)
)
lyricsObj.push(obj) lyricsObj.push(obj)
} }
}) })
return lyricsObj return lyricsObj
} }
state.lyrics.lyrics = 'lyrics' in lyrics ? parse(lyrics.lyrics) : '' return {}
if (!state.lyrics.found) }
state.lyrics.found = state.lyrics.lyrics.length > 0 state.lyrics.content = track ? parse(track.lyrics) : ''
}, },
[types.UPDATE_LASTFM](state, lastfm) { [types.UPDATE_LASTFM](state, lastfm) {
state.lastfm = lastfm state.lastfm = lastfm

View File

@@ -424,9 +424,7 @@ export default {
}, },
library_track(trackId) { library_track(trackId) {
if (trackId) { // Temporary fix
return axios.get(`./api/library/tracks/${trackId}`) return axios.get(`./api/library/tracks/${trackId}`)
}
}, },
library_track_playlists(trackId) { library_track_playlists(trackId) {