Skip to content

Commit

Permalink
[web] Fix for unexpected calls to the API #1688
Browse files Browse the repository at this point in the history
Lyrics feature doesn't mess anymore when the user is playing tracks from Spotify
  • Loading branch information
hacketiwack committed Nov 24, 2023
1 parent f19e9fb commit 91c1e5b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 42 deletions.
11 changes: 6 additions & 5 deletions web-src/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,14 @@ export default {
},
update_lyrics() {
let track = this.$store.state.queue.items.filter(
(e) => e.id == this.$store.state.player.item_id
)
if (track.length >= 1)
webapi.library_track(track[0].track_id).then(({ data }) => {
const track = this.$store.getters.now_playing
if (track && track.track_id) {
webapi.library_track(track.track_id).then(({ data }) => {
this.$store.commit(types.UPDATE_LYRICS, data)
})
} else {
this.$store.commit(types.UPDATE_LYRICS)
}
},
update_settings() {
Expand Down
3 changes: 1 addition & 2 deletions web-src/src/components/PlayerButtonLyrics.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ export default {
methods: {
toggle_lyrics() {
this.$store.state.lyrics.lyrics_pane =
!this.$store.state.lyrics.lyrics_pane
this.$store.state.lyrics.pane = !this.$store.state.lyrics.pane
}
}
}
Expand Down
55 changes: 23 additions & 32 deletions web-src/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ export default createStore({
item_progress_ms: 0
},
lyrics: {
found: false,
lyrics_id: -1,
lyrics_pane: false,
lyrics: []
pane: false,
content: []
},
queue: {
version: 0,
Expand Down Expand Up @@ -76,17 +74,9 @@ export default createStore({
},

getters: {
lyrics: (state) => {
return state.lyrics.lyrics
},

lyrics_found: (state) => {
return state.lyrics.found
},
lyrics: (state) => state.lyrics.content,

lyrics_pane: (state) => {
return state.lyrics.lyrics_pane
},
lyrics_pane: (state) => state.lyrics.pane,

now_playing: (state) => {
const item = state.queue.items.find(function (item) {
Expand Down Expand Up @@ -206,27 +196,28 @@ export default createStore({
[types.UPDATE_QUEUE](state, queue) {
state.queue = queue
},
[types.UPDATE_LYRICS](state, lyrics) {
[types.UPDATE_LYRICS](state, track) {
// Parse from .LRC or text format to synchronized lyrics
function parse(lyrics) {
let lyricsObj = []
let tempArr = lyrics.split('\n')
const regex = /(\[(\d+):(\d+)(?:\.\d+)?\] ?)?(.*)/

tempArr.forEach((item) => {
let matches = regex.exec(item)
if (matches !== null && matches[4].length) {
let obj = [matches[4]]
if (matches[2] != null && matches[3] != null)
obj.push(parseInt(matches[2], 10) * 60 + parseInt(matches[3], 10))
lyricsObj.push(obj)
}
})
return lyricsObj
if (lyrics) {
const lyricsObj = []
const regex = /(\[(\d+):(\d+)(?:\.\d+)?\] ?)?(.*)/
lyrics.split('\n').forEach((item) => {
const matches = regex.exec(item)
if (matches !== null && matches[4].length) {
const obj = [matches[4]]
if (matches[2] != null && matches[3] != null)
obj.push(
parseInt(matches[2], 10) * 60 + parseInt(matches[3], 10)
)
lyricsObj.push(obj)
}
})
return lyricsObj
}
return {}
}
state.lyrics.lyrics = 'lyrics' in lyrics ? parse(lyrics.lyrics) : ''
if (!state.lyrics.found)
state.lyrics.found = state.lyrics.lyrics.length > 0
state.lyrics.content = track ? parse(track.lyrics) : ''
},
[types.UPDATE_LASTFM](state, lastfm) {
state.lastfm = lastfm
Expand Down
4 changes: 1 addition & 3 deletions web-src/src/webapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,7 @@ export default {
},

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) {
Expand Down

0 comments on commit 91c1e5b

Please sign in to comment.