From 999976645d966cc6529b5dd230b3910ac20edb90 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Thu, 4 Dec 2025 22:24:55 -0800 Subject: [PATCH] properly recalculate current index when adding to shuffled queue --- src/renderer/store/player.store.ts | 90 ++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 16 deletions(-) diff --git a/src/renderer/store/player.store.ts b/src/renderer/store/player.store.ts index bf5c68bfc..2bbcdc550 100644 --- a/src/renderer/store/player.store.ts +++ b/src/renderer/store/player.store.ts @@ -824,20 +824,66 @@ export const usePlayerStoreBase = createWithEqualityFn()( const insertIndex = Math.max(0, edge === 'top' ? index : index + 1); - // Recalculate the player index if we're inserting items above the current index - if (insertIndex <= state.player.index) { - state.player.index = state.player.index + newUniqueIds.length; - } - const newQueue = [ ...state.queue.default.slice(0, insertIndex), ...newUniqueIds, ...state.queue.default.slice(insertIndex), ]; - recalculatePlayerIndex(state, newQueue); - state.queue.default = newQueue; + + if (state.player.shuffle === PlayerShuffle.TRACK) { + const currentTrack = state.getCurrentSong() as + | QueueSong + | undefined; + const currentTrackUniqueId = currentTrack?._uniqueId; + + if (currentTrackUniqueId) { + // Adjust existing shuffled indexes that are >= insertIndex + const adjustedShuffled = state.queue.shuffled.map((idx) => { + if (idx >= insertIndex) { + return idx + newUniqueIds.length; + } + return idx; + }); + + // New items will be at indexes starting from insertIndex + const newIndexes = Array.from( + { length: newUniqueIds.length }, + (_, i) => insertIndex + i, + ); + + const currentShuffledIndex = state.player.index; + state.queue.shuffled = addIndexesToShuffled( + adjustedShuffled, + currentShuffledIndex, + newIndexes, + ); + + // Recalculate player index to the shuffled position + const queueIndex = newQueue.findIndex( + (id) => id === currentTrackUniqueId, + ); + if (queueIndex !== -1) { + const shuffledPosition = state.queue.shuffled.findIndex( + (idx) => idx === queueIndex, + ); + if (shuffledPosition !== -1) { + state.player.index = shuffledPosition; + } + } + } else { + // No current track, regenerate shuffled indexes + state.queue.shuffled = generateShuffledIndexes(newQueue.length); + } + } else { + // Recalculate the player index if we're inserting items above the current index + if (insertIndex <= state.player.index) { + state.player.index = state.player.index + newUniqueIds.length; + } + + recalculatePlayerIndex(state, newQueue); + } } else { const currentTrack = state.getCurrentSong() as QueueSong | undefined; const currentTrackUniqueId = currentTrack?._uniqueId; @@ -877,18 +923,9 @@ export const usePlayerStoreBase = createWithEqualityFn()( } const combinedQueue = [...state.queue.priority, ...state.queue.default]; - recalculatePlayerIndexByUniqueId( - state, - currentTrackUniqueId, - combinedQueue, - ); if (state.player.shuffle === PlayerShuffle.TRACK) { const currentShuffledIndex = state.player.index; - const combinedQueue = [ - ...state.queue.priority, - ...state.queue.default, - ]; // Find insert position in combined queue let insertPosition: number; @@ -932,6 +969,27 @@ export const usePlayerStoreBase = createWithEqualityFn()( currentShuffledIndex, newIndexes, ); + + // Recalculate player index to the shuffled position + if (currentTrackUniqueId) { + const queueIndex = combinedQueue.findIndex( + (id) => id === currentTrackUniqueId, + ); + if (queueIndex !== -1) { + const shuffledPosition = state.queue.shuffled.findIndex( + (idx) => idx === queueIndex, + ); + if (shuffledPosition !== -1) { + state.player.index = shuffledPosition; + } + } + } + } else { + recalculatePlayerIndexByUniqueId( + state, + currentTrackUniqueId, + combinedQueue, + ); } } });