From 055d9ac5c1c3c9a8bcd487c1e09396a4945e97e3 Mon Sep 17 00:00:00 2001 From: Kendall Garner <17521368+kgarner7@users.noreply.github.com> Date: Wed, 2 Jul 2025 20:48:45 -0700 Subject: [PATCH] use initial index for shuffling when availabe --- .../player/hooks/use-handle-playqueue-add.ts | 4 ++- src/renderer/store/player.store.ts | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/renderer/features/player/hooks/use-handle-playqueue-add.ts b/src/renderer/features/player/hooks/use-handle-playqueue-add.ts index efa07769..c478b4b2 100644 --- a/src/renderer/features/player/hooks/use-handle-playqueue-add.ts +++ b/src/renderer/features/player/hooks/use-handle-playqueue-add.ts @@ -75,7 +75,9 @@ export const useHandlePlayQueueAdd = () => { if (!server) return toast.error({ message: 'No server selected', type: 'error' }); const { byData, byItemType, initialIndex, initialSongId, playType, query } = options; let songs: null | QueueSong[] = null; - let initialSongIndex = 0; + // Allow this to be undefined for "play shuffled". If undefined, default to 0, + // otherwise, choose the selected item in the queue + let initialSongIndex: number | undefined; if (byItemType) { let songList: SongListResponse | undefined; diff --git a/src/renderer/store/player.store.ts b/src/renderer/store/player.store.ts index 52732462..6f0ae2ae 100644 --- a/src/renderer/store/player.store.ts +++ b/src/renderer/store/player.store.ts @@ -13,7 +13,7 @@ import { Play, PlayerRepeat, PlayerShuffle, PlayerStatus } from '/@/shared/types export interface PlayerSlice extends PlayerState { actions: { addToQueue: (args: { - initialIndex: number; + initialIndex?: number; playType: Play; songs: QueueSong[]; }) => PlayerData; @@ -92,19 +92,30 @@ export const usePlayerStore = createWithEqualityFn()( uniqueId: nanoid(), })); - // If the queue is empty, next/last should behave the same as now if (playType === Play.SHUFFLE) { - const songs = shuffle(songsToAddToQueue); - const initialSong = songs[0]; + let shuffled: QueueSong[]; + + if ( + initialIndex !== undefined && + initialIndex < songsToAddToQueue.length + ) { + const removed = songsToAddToQueue.splice(initialIndex, 1); + const restShuffled = shuffle(songsToAddToQueue); + shuffled = removed.concat(restShuffled); + } else { + shuffled = shuffle(songsToAddToQueue); + } + + const initialSong = shuffled[0]; if (get().shuffle === PlayerShuffle.TRACK) { const shuffledIds = [ initialSong.uniqueId, - ...shuffle(songs.slice(1).map((song) => song.uniqueId)), + ...shuffle(shuffled.slice(1).map((song) => song.uniqueId)), ]; set((state) => { - state.queue.default = songs; + state.queue.default = shuffled; state.queue.shuffled = shuffledIds; state.current.time = 0; state.current.player = 1; @@ -114,7 +125,7 @@ export const usePlayerStore = createWithEqualityFn()( }); } else { set((state) => { - state.queue.default = songs; + state.queue.default = shuffled; state.queue.shuffled = []; state.current.time = 0; state.current.player = 1; @@ -131,6 +142,7 @@ export const usePlayerStore = createWithEqualityFn()( const queue = get().queue.default; const { shuffledIndex } = get().current; + // If the queue is empty, next/last should behave the same as now if (playType === Play.NOW || queue.length === 0) { const index = initialIndex || 0; if (get().shuffle === PlayerShuffle.TRACK) {