Skip to content

🎧 Your Personal Streaming Service

Add current playing track id to the Redux store

Deluan87cc397bc31c
diff --git a/ui/src/audioplayer/Player.js b/ui/src/audioplayer/Player.js
index 474dd55ee..d2f2c0657 100644
--- a/ui/src/audioplayer/Player.js
+++ b/ui/src/audioplayer/Player.js
@@ -4,7 +4,7 @@ import { useAuthState, useDataProvider, useTranslate } from 'react-admin'
import ReactJkMusicPlayer from 'react-jinke-music-player'
import 'react-jinke-music-player/assets/index.css'
import subsonic from '../subsonic'
-import { scrobbled, syncQueue } from './queue'
+import { scrobble, syncQueue } from './queue'
import themes from '../themes'
const Player = () => {
@@ -76,7 +76,7 @@ const Player = () => {
const { authenticated } = useAuthState()
const OnAudioListsChange = (currentPlayIndex, audioLists) => {
- dispatch(syncQueue(audioLists))
+ dispatch(syncQueue(currentPlayIndex, audioLists))
}
const OnAudioProgress = (info) => {
@@ -86,13 +86,14 @@ const Player = () => {
}
const item = queue.queue.find((item) => item.trackId === info.trackId)
if (item && !item.scrobbled) {
- dispatch(scrobbled(info.trackId))
+ dispatch(scrobble(info.trackId, true))
subsonic.scrobble(info.trackId, true)
}
}
const OnAudioPlay = (info) => {
if (info.duration) {
+ dispatch(scrobble(info.trackId, false))
subsonic.scrobble(info.trackId, false)
dataProvider.getOne('keepalive', { id: info.trackId })
}
diff --git a/ui/src/audioplayer/queue.js b/ui/src/audioplayer/queue.js
index c44cd6877..416953d69 100644
--- a/ui/src/audioplayer/queue.js
+++ b/ui/src/audioplayer/queue.js
@@ -28,18 +28,20 @@ const setTrack = (data) => ({
const playAlbum = (id, data) => ({
type: PLAYER_PLAY_ALBUM,
- data,
id,
+ data,
})
-const syncQueue = (data) => ({
+const syncQueue = (id, data) => ({
type: PLAYER_SYNC_QUEUE,
+ id,
data,
})
-const scrobbled = (id) => ({
+const scrobble = (id, submit) => ({
type: PLAYER_SCROBBLE,
- data: id,
+ id,
+ submit,
})
const playQueueReducer = (
@@ -59,17 +61,31 @@ const playQueueReducer = (
queue: [mapToAudioLists(data)],
clear: true,
playing: true,
+ current: data.id,
}
case PLAYER_SYNC_QUEUE:
- return { ...previousState, queue: data, clear: false }
+ const currentTrack = data.find((item) => item.id === data.id) || {}
+ return {
+ ...previousState,
+ queue: data,
+ clear: false,
+ current: currentTrack.id,
+ }
case PLAYER_SCROBBLE:
const newQueue = previousState.queue.map((item) => {
return {
...item,
- scrobbled: item.scrobbled || item.trackId === data,
+ scrobbled:
+ item.scrobbled || (item.trackId === payload.id && payload.submit),
}
})
- return { ...previousState, queue: newQueue, clear: false, playing: true }
+ return {
+ ...previousState,
+ queue: newQueue,
+ clear: false,
+ playing: true,
+ current: payload.id,
+ }
case PLAYER_PLAY_ALBUM:
queue = []
let match = false
@@ -81,10 +97,16 @@ const playQueueReducer = (
queue.push(mapToAudioLists(data[id]))
}
})
- return { ...previousState, queue, clear: true, playing: true }
+ return {
+ ...previousState,
+ queue,
+ clear: true,
+ playing: true,
+ current: payload.id,
+ }
default:
return previousState
}
}
-export { addTrack, setTrack, playAlbum, syncQueue, scrobbled, playQueueReducer }
+export { addTrack, setTrack, playAlbum, syncQueue, scrobble, playQueueReducer }
diff --git a/ui/src/subsonic/index.js b/ui/src/subsonic/index.js
index 6404cb307..b5dfc5aac 100644
--- a/ui/src/subsonic/index.js
+++ b/ui/src/subsonic/index.js
@@ -23,8 +23,7 @@ const url = (command, id, options) => {
return baseUrl(url)
}
-const scrobble = (id, submit) => {
- return fetchUtils.fetchJson(url('scrobble', id, { submission: submit }))
-}
+const scrobble = (id, submit) =>
+ fetchUtils.fetchJson(url('scrobble', id, { submission: submit }))
export default { url, scrobble }