Fix shuffled queue set by double click

This commit is contained in:
jeffvli 2023-05-21 20:14:22 -07:00
parent bc2624bffd
commit e937425f4f

View file

@ -107,21 +107,31 @@ export const usePlayerStore = create<PlayerSlice>()(
if (playType === Play.NOW) { if (playType === Play.NOW) {
if (get().shuffle === PlayerShuffle.TRACK) { if (get().shuffle === PlayerShuffle.TRACK) {
const shuffledSongs = shuffle(queueSongs);
const index = initialIndex || 0; const index = initialIndex || 0;
const initialSongUniqueId = queueSongs[index].uniqueId; const initialSong = queueSongs[index];
const initialSongIndex = shuffledSongs.findIndex( const queueCopy = [...queueSongs];
(song) => song.uniqueId === initialSongUniqueId,
// 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) => { set((state) => {
state.queue.shuffled = shuffledSongs.map((song) => song.uniqueId); state.queue.shuffled = shuffledSongIndices;
state.queue.default = queueSongs; state.queue.default = queueSongs;
state.current.time = 0; state.current.time = 0;
state.current.player = 1; state.current.player = 1;
state.current.index = initialSongIndex; state.current.index = 0;
state.current.shuffledIndex = 0; state.current.shuffledIndex = 0;
state.current.song = shuffledSongs[initialSongIndex]; state.current.song = initialSong;
}); });
} else { } else {
const index = initialIndex || 0; const index = initialIndex || 0;
@ -849,7 +859,7 @@ export const usePlayerStore = create<PlayerSlice>()(
}, },
name: 'store_player', name: 'store_player',
partialize: (state) => { partialize: (state) => {
const notPersisted = ['queue', 'current']; const notPersisted = ['queue', 'current', 'entry'];
return Object.fromEntries( return Object.fromEntries(
Object.entries(state).filter(([key]) => !notPersisted.includes(key)), Object.entries(state).filter(([key]) => !notPersisted.includes(key)),
); );