From 62c372d0c7bce2775fe8189a8cb4955a015ecc3f Mon Sep 17 00:00:00 2001 From: Kendall Garner <17521368+kgarner7@users.noreply.github.com> Date: Tue, 27 Aug 2024 21:14:08 -0700 Subject: [PATCH] dont't move to 0 when removing current item from queue --- src/renderer/store/player.store.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/renderer/store/player.store.ts b/src/renderer/store/player.store.ts index 2950d259..cba37496 100644 --- a/src/renderer/store/player.store.ts +++ b/src/renderer/store/player.store.ts @@ -643,14 +643,19 @@ export const usePlayerStore = create()( }, removeFromQueue: (uniqueIds) => { const queue = get().queue.default; - const currentSong = get().current.song; const currentPosition = get().current.index; let queueShift = 0; + let isCurrentSongRemoved = false; + const newQueue = queue.filter((song, index) => { const shouldKeep = !uniqueIds.includes(song.uniqueId); - if (!shouldKeep && index < currentPosition) { - queueShift += 1; + if (!shouldKeep) { + if (index < currentPosition) { + queueShift += 1; + } else if (index === currentPosition) { + isCurrentSongRemoved = true; + } } return shouldKeep; @@ -659,15 +664,16 @@ export const usePlayerStore = create()( (uniqueId) => !uniqueIds.includes(uniqueId), ); - const isCurrentSongRemoved = - currentSong && uniqueIds.includes(currentSong?.uniqueId); - set((state) => { state.queue.default = newQueue; state.queue.shuffled = newShuffledQueue; if (isCurrentSongRemoved) { - state.current.song = newQueue[0]; - state.current.index = 0; + const newPosition = Math.min( + newQueue.length - 1, + currentPosition - queueShift, + ); + state.current.song = newQueue[newPosition]; + state.current.index = newPosition; } else { // if we removed any songs prior to the current one, // shift the index back as necessary