dont't move to 0 when removing current item from queue

This commit is contained in:
Kendall Garner 2024-08-27 21:14:08 -07:00
parent 279842b894
commit 62c372d0c7
No known key found for this signature in database
GPG key ID: 18D2767419676C87

View file

@ -643,14 +643,19 @@ export const usePlayerStore = create<PlayerSlice>()(
}, },
removeFromQueue: (uniqueIds) => { removeFromQueue: (uniqueIds) => {
const queue = get().queue.default; const queue = get().queue.default;
const currentSong = get().current.song;
const currentPosition = get().current.index; const currentPosition = get().current.index;
let queueShift = 0; let queueShift = 0;
let isCurrentSongRemoved = false;
const newQueue = queue.filter((song, index) => { const newQueue = queue.filter((song, index) => {
const shouldKeep = !uniqueIds.includes(song.uniqueId); const shouldKeep = !uniqueIds.includes(song.uniqueId);
if (!shouldKeep && index < currentPosition) { if (!shouldKeep) {
queueShift += 1; if (index < currentPosition) {
queueShift += 1;
} else if (index === currentPosition) {
isCurrentSongRemoved = true;
}
} }
return shouldKeep; return shouldKeep;
@ -659,15 +664,16 @@ export const usePlayerStore = create<PlayerSlice>()(
(uniqueId) => !uniqueIds.includes(uniqueId), (uniqueId) => !uniqueIds.includes(uniqueId),
); );
const isCurrentSongRemoved =
currentSong && uniqueIds.includes(currentSong?.uniqueId);
set((state) => { set((state) => {
state.queue.default = newQueue; state.queue.default = newQueue;
state.queue.shuffled = newShuffledQueue; state.queue.shuffled = newShuffledQueue;
if (isCurrentSongRemoved) { if (isCurrentSongRemoved) {
state.current.song = newQueue[0]; const newPosition = Math.min(
state.current.index = 0; newQueue.length - 1,
currentPosition - queueShift,
);
state.current.song = newQueue[newPosition];
state.current.index = newPosition;
} else { } else {
// if we removed any songs prior to the current one, // if we removed any songs prior to the current one,
// shift the index back as necessary // shift the index back as necessary