use initial index for shuffling when availabe

This commit is contained in:
Kendall Garner 2025-07-02 20:48:45 -07:00
parent 039d008223
commit 055d9ac5c1
No known key found for this signature in database
GPG key ID: 9355F387FE765C94
2 changed files with 22 additions and 8 deletions

View file

@ -75,7 +75,9 @@ export const useHandlePlayQueueAdd = () => {
if (!server) return toast.error({ message: 'No server selected', type: 'error' }); if (!server) return toast.error({ message: 'No server selected', type: 'error' });
const { byData, byItemType, initialIndex, initialSongId, playType, query } = options; const { byData, byItemType, initialIndex, initialSongId, playType, query } = options;
let songs: null | QueueSong[] = null; 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) { if (byItemType) {
let songList: SongListResponse | undefined; let songList: SongListResponse | undefined;

View file

@ -13,7 +13,7 @@ import { Play, PlayerRepeat, PlayerShuffle, PlayerStatus } from '/@/shared/types
export interface PlayerSlice extends PlayerState { export interface PlayerSlice extends PlayerState {
actions: { actions: {
addToQueue: (args: { addToQueue: (args: {
initialIndex: number; initialIndex?: number;
playType: Play; playType: Play;
songs: QueueSong[]; songs: QueueSong[];
}) => PlayerData; }) => PlayerData;
@ -92,19 +92,30 @@ export const usePlayerStore = createWithEqualityFn<PlayerSlice>()(
uniqueId: nanoid(), uniqueId: nanoid(),
})); }));
// If the queue is empty, next/last should behave the same as now
if (playType === Play.SHUFFLE) { if (playType === Play.SHUFFLE) {
const songs = shuffle(songsToAddToQueue); let shuffled: QueueSong[];
const initialSong = songs[0];
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) { if (get().shuffle === PlayerShuffle.TRACK) {
const shuffledIds = [ const shuffledIds = [
initialSong.uniqueId, initialSong.uniqueId,
...shuffle(songs.slice(1).map((song) => song.uniqueId)), ...shuffle(shuffled.slice(1).map((song) => song.uniqueId)),
]; ];
set((state) => { set((state) => {
state.queue.default = songs; state.queue.default = shuffled;
state.queue.shuffled = shuffledIds; state.queue.shuffled = shuffledIds;
state.current.time = 0; state.current.time = 0;
state.current.player = 1; state.current.player = 1;
@ -114,7 +125,7 @@ export const usePlayerStore = createWithEqualityFn<PlayerSlice>()(
}); });
} else { } else {
set((state) => { set((state) => {
state.queue.default = songs; state.queue.default = shuffled;
state.queue.shuffled = []; state.queue.shuffled = [];
state.current.time = 0; state.current.time = 0;
state.current.player = 1; state.current.player = 1;
@ -131,6 +142,7 @@ export const usePlayerStore = createWithEqualityFn<PlayerSlice>()(
const queue = get().queue.default; const queue = get().queue.default;
const { shuffledIndex } = get().current; const { shuffledIndex } = get().current;
// If the queue is empty, next/last should behave the same as now
if (playType === Play.NOW || queue.length === 0) { if (playType === Play.NOW || queue.length === 0) {
const index = initialIndex || 0; const index = initialIndex || 0;
if (get().shuffle === PlayerShuffle.TRACK) { if (get().shuffle === PlayerShuffle.TRACK) {