From e937425f4f83da78806af191af364a2c77ab6ba5 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Sun, 21 May 2023 20:14:22 -0700 Subject: [PATCH] Fix shuffled queue set by double click --- src/renderer/store/player.store.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/renderer/store/player.store.ts b/src/renderer/store/player.store.ts index 12da5323..3b2ad96d 100644 --- a/src/renderer/store/player.store.ts +++ b/src/renderer/store/player.store.ts @@ -107,21 +107,31 @@ export const usePlayerStore = create()( if (playType === Play.NOW) { if (get().shuffle === PlayerShuffle.TRACK) { - const shuffledSongs = shuffle(queueSongs); const index = initialIndex || 0; - const initialSongUniqueId = queueSongs[index].uniqueId; - const initialSongIndex = shuffledSongs.findIndex( - (song) => song.uniqueId === initialSongUniqueId, + const initialSong = queueSongs[index]; + const queueCopy = [...queueSongs]; + + // Splice the initial song from the queue + queueCopy.splice(index, 1); + + const shuffledSongIndicesWithoutInitial = shuffle(queueCopy).map( + (song) => song.uniqueId, ); + // Add the initial song to the start of the shuffled queue + const shuffledSongIndices = [ + initialSong.uniqueId, + ...shuffledSongIndicesWithoutInitial, + ]; + set((state) => { - state.queue.shuffled = shuffledSongs.map((song) => song.uniqueId); + state.queue.shuffled = shuffledSongIndices; state.queue.default = queueSongs; state.current.time = 0; state.current.player = 1; - state.current.index = initialSongIndex; + state.current.index = 0; state.current.shuffledIndex = 0; - state.current.song = shuffledSongs[initialSongIndex]; + state.current.song = initialSong; }); } else { const index = initialIndex || 0; @@ -849,7 +859,7 @@ export const usePlayerStore = create()( }, name: 'store_player', partialize: (state) => { - const notPersisted = ['queue', 'current']; + const notPersisted = ['queue', 'current', 'entry']; return Object.fromEntries( Object.entries(state).filter(([key]) => !notPersisted.includes(key)), );