mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-10 04:30:25 +02:00
properly recalculate current index when adding to shuffled queue
This commit is contained in:
@@ -824,20 +824,66 @@ export const usePlayerStoreBase = createWithEqualityFn<PlayerState>()(
|
|||||||
|
|
||||||
const insertIndex = Math.max(0, edge === 'top' ? index : index + 1);
|
const insertIndex = Math.max(0, edge === 'top' ? index : index + 1);
|
||||||
|
|
||||||
// Recalculate the player index if we're inserting items above the current index
|
|
||||||
if (insertIndex <= state.player.index) {
|
|
||||||
state.player.index = state.player.index + newUniqueIds.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
const newQueue = [
|
const newQueue = [
|
||||||
...state.queue.default.slice(0, insertIndex),
|
...state.queue.default.slice(0, insertIndex),
|
||||||
...newUniqueIds,
|
...newUniqueIds,
|
||||||
...state.queue.default.slice(insertIndex),
|
...state.queue.default.slice(insertIndex),
|
||||||
];
|
];
|
||||||
|
|
||||||
recalculatePlayerIndex(state, newQueue);
|
|
||||||
|
|
||||||
state.queue.default = newQueue;
|
state.queue.default = newQueue;
|
||||||
|
|
||||||
|
if (state.player.shuffle === PlayerShuffle.TRACK) {
|
||||||
|
const currentTrack = state.getCurrentSong() as
|
||||||
|
| QueueSong
|
||||||
|
| undefined;
|
||||||
|
const currentTrackUniqueId = currentTrack?._uniqueId;
|
||||||
|
|
||||||
|
if (currentTrackUniqueId) {
|
||||||
|
// Adjust existing shuffled indexes that are >= insertIndex
|
||||||
|
const adjustedShuffled = state.queue.shuffled.map((idx) => {
|
||||||
|
if (idx >= insertIndex) {
|
||||||
|
return idx + newUniqueIds.length;
|
||||||
|
}
|
||||||
|
return idx;
|
||||||
|
});
|
||||||
|
|
||||||
|
// New items will be at indexes starting from insertIndex
|
||||||
|
const newIndexes = Array.from(
|
||||||
|
{ length: newUniqueIds.length },
|
||||||
|
(_, i) => insertIndex + i,
|
||||||
|
);
|
||||||
|
|
||||||
|
const currentShuffledIndex = state.player.index;
|
||||||
|
state.queue.shuffled = addIndexesToShuffled(
|
||||||
|
adjustedShuffled,
|
||||||
|
currentShuffledIndex,
|
||||||
|
newIndexes,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Recalculate player index to the shuffled position
|
||||||
|
const queueIndex = newQueue.findIndex(
|
||||||
|
(id) => id === currentTrackUniqueId,
|
||||||
|
);
|
||||||
|
if (queueIndex !== -1) {
|
||||||
|
const shuffledPosition = state.queue.shuffled.findIndex(
|
||||||
|
(idx) => idx === queueIndex,
|
||||||
|
);
|
||||||
|
if (shuffledPosition !== -1) {
|
||||||
|
state.player.index = shuffledPosition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// No current track, regenerate shuffled indexes
|
||||||
|
state.queue.shuffled = generateShuffledIndexes(newQueue.length);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Recalculate the player index if we're inserting items above the current index
|
||||||
|
if (insertIndex <= state.player.index) {
|
||||||
|
state.player.index = state.player.index + newUniqueIds.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
recalculatePlayerIndex(state, newQueue);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const currentTrack = state.getCurrentSong() as QueueSong | undefined;
|
const currentTrack = state.getCurrentSong() as QueueSong | undefined;
|
||||||
const currentTrackUniqueId = currentTrack?._uniqueId;
|
const currentTrackUniqueId = currentTrack?._uniqueId;
|
||||||
@@ -877,18 +923,9 @@ export const usePlayerStoreBase = createWithEqualityFn<PlayerState>()(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const combinedQueue = [...state.queue.priority, ...state.queue.default];
|
const combinedQueue = [...state.queue.priority, ...state.queue.default];
|
||||||
recalculatePlayerIndexByUniqueId(
|
|
||||||
state,
|
|
||||||
currentTrackUniqueId,
|
|
||||||
combinedQueue,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (state.player.shuffle === PlayerShuffle.TRACK) {
|
if (state.player.shuffle === PlayerShuffle.TRACK) {
|
||||||
const currentShuffledIndex = state.player.index;
|
const currentShuffledIndex = state.player.index;
|
||||||
const combinedQueue = [
|
|
||||||
...state.queue.priority,
|
|
||||||
...state.queue.default,
|
|
||||||
];
|
|
||||||
|
|
||||||
// Find insert position in combined queue
|
// Find insert position in combined queue
|
||||||
let insertPosition: number;
|
let insertPosition: number;
|
||||||
@@ -932,6 +969,27 @@ export const usePlayerStoreBase = createWithEqualityFn<PlayerState>()(
|
|||||||
currentShuffledIndex,
|
currentShuffledIndex,
|
||||||
newIndexes,
|
newIndexes,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Recalculate player index to the shuffled position
|
||||||
|
if (currentTrackUniqueId) {
|
||||||
|
const queueIndex = combinedQueue.findIndex(
|
||||||
|
(id) => id === currentTrackUniqueId,
|
||||||
|
);
|
||||||
|
if (queueIndex !== -1) {
|
||||||
|
const shuffledPosition = state.queue.shuffled.findIndex(
|
||||||
|
(idx) => idx === queueIndex,
|
||||||
|
);
|
||||||
|
if (shuffledPosition !== -1) {
|
||||||
|
state.player.index = shuffledPosition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
recalculatePlayerIndexByUniqueId(
|
||||||
|
state,
|
||||||
|
currentTrackUniqueId,
|
||||||
|
combinedQueue,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user