Add current playing track id to the Redux store
This commit is contained in:
@@ -4,7 +4,7 @@ import { useAuthState, useDataProvider, useTranslate } from 'react-admin'
|
|||||||
import ReactJkMusicPlayer from 'react-jinke-music-player'
|
import ReactJkMusicPlayer from 'react-jinke-music-player'
|
||||||
import 'react-jinke-music-player/assets/index.css'
|
import 'react-jinke-music-player/assets/index.css'
|
||||||
import subsonic from '../subsonic'
|
import subsonic from '../subsonic'
|
||||||
import { scrobbled, syncQueue } from './queue'
|
import { scrobble, syncQueue } from './queue'
|
||||||
import themes from '../themes'
|
import themes from '../themes'
|
||||||
|
|
||||||
const Player = () => {
|
const Player = () => {
|
||||||
@@ -76,7 +76,7 @@ const Player = () => {
|
|||||||
const { authenticated } = useAuthState()
|
const { authenticated } = useAuthState()
|
||||||
|
|
||||||
const OnAudioListsChange = (currentPlayIndex, audioLists) => {
|
const OnAudioListsChange = (currentPlayIndex, audioLists) => {
|
||||||
dispatch(syncQueue(audioLists))
|
dispatch(syncQueue(currentPlayIndex, audioLists))
|
||||||
}
|
}
|
||||||
|
|
||||||
const OnAudioProgress = (info) => {
|
const OnAudioProgress = (info) => {
|
||||||
@@ -86,13 +86,14 @@ const Player = () => {
|
|||||||
}
|
}
|
||||||
const item = queue.queue.find((item) => item.trackId === info.trackId)
|
const item = queue.queue.find((item) => item.trackId === info.trackId)
|
||||||
if (item && !item.scrobbled) {
|
if (item && !item.scrobbled) {
|
||||||
dispatch(scrobbled(info.trackId))
|
dispatch(scrobble(info.trackId, true))
|
||||||
subsonic.scrobble(info.trackId, true)
|
subsonic.scrobble(info.trackId, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const OnAudioPlay = (info) => {
|
const OnAudioPlay = (info) => {
|
||||||
if (info.duration) {
|
if (info.duration) {
|
||||||
|
dispatch(scrobble(info.trackId, false))
|
||||||
subsonic.scrobble(info.trackId, false)
|
subsonic.scrobble(info.trackId, false)
|
||||||
dataProvider.getOne('keepalive', { id: info.trackId })
|
dataProvider.getOne('keepalive', { id: info.trackId })
|
||||||
}
|
}
|
||||||
|
|||||||
+34
-12
@@ -28,18 +28,20 @@ const setTrack = (data) => ({
|
|||||||
|
|
||||||
const playAlbum = (id, data) => ({
|
const playAlbum = (id, data) => ({
|
||||||
type: PLAYER_PLAY_ALBUM,
|
type: PLAYER_PLAY_ALBUM,
|
||||||
data,
|
|
||||||
id,
|
id,
|
||||||
})
|
|
||||||
|
|
||||||
const syncQueue = (data) => ({
|
|
||||||
type: PLAYER_SYNC_QUEUE,
|
|
||||||
data,
|
data,
|
||||||
})
|
})
|
||||||
|
|
||||||
const scrobbled = (id) => ({
|
const syncQueue = (id, data) => ({
|
||||||
|
type: PLAYER_SYNC_QUEUE,
|
||||||
|
id,
|
||||||
|
data,
|
||||||
|
})
|
||||||
|
|
||||||
|
const scrobble = (id, submit) => ({
|
||||||
type: PLAYER_SCROBBLE,
|
type: PLAYER_SCROBBLE,
|
||||||
data: id,
|
id,
|
||||||
|
submit,
|
||||||
})
|
})
|
||||||
|
|
||||||
const playQueueReducer = (
|
const playQueueReducer = (
|
||||||
@@ -59,17 +61,31 @@ const playQueueReducer = (
|
|||||||
queue: [mapToAudioLists(data)],
|
queue: [mapToAudioLists(data)],
|
||||||
clear: true,
|
clear: true,
|
||||||
playing: true,
|
playing: true,
|
||||||
|
current: data.id,
|
||||||
}
|
}
|
||||||
case PLAYER_SYNC_QUEUE:
|
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:
|
case PLAYER_SCROBBLE:
|
||||||
const newQueue = previousState.queue.map((item) => {
|
const newQueue = previousState.queue.map((item) => {
|
||||||
return {
|
return {
|
||||||
...item,
|
...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:
|
case PLAYER_PLAY_ALBUM:
|
||||||
queue = []
|
queue = []
|
||||||
let match = false
|
let match = false
|
||||||
@@ -81,10 +97,16 @@ const playQueueReducer = (
|
|||||||
queue.push(mapToAudioLists(data[id]))
|
queue.push(mapToAudioLists(data[id]))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return { ...previousState, queue, clear: true, playing: true }
|
return {
|
||||||
|
...previousState,
|
||||||
|
queue,
|
||||||
|
clear: true,
|
||||||
|
playing: true,
|
||||||
|
current: payload.id,
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return previousState
|
return previousState
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { addTrack, setTrack, playAlbum, syncQueue, scrobbled, playQueueReducer }
|
export { addTrack, setTrack, playAlbum, syncQueue, scrobble, playQueueReducer }
|
||||||
|
|||||||
@@ -23,8 +23,7 @@ const url = (command, id, options) => {
|
|||||||
return baseUrl(url)
|
return baseUrl(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
const scrobble = (id, submit) => {
|
const scrobble = (id, submit) =>
|
||||||
return fetchUtils.fetchJson(url('scrobble', id, { submission: submit }))
|
fetchUtils.fetchJson(url('scrobble', id, { submission: submit }))
|
||||||
}
|
|
||||||
|
|
||||||
export default { url, scrobble }
|
export default { url, scrobble }
|
||||||
|
|||||||
Reference in New Issue
Block a user