dont't move to 0 when removing current item from queue
This commit is contained in:
parent
279842b894
commit
62c372d0c7
1 changed files with 14 additions and 8 deletions
|
@ -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) {
|
||||||
|
if (index < currentPosition) {
|
||||||
queueShift += 1;
|
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
|
||||||
|
|
Reference in a new issue