diff --git a/src/main/features/core/player/index.ts b/src/main/features/core/player/index.ts index fc1e75106..dec78262e 100644 --- a/src/main/features/core/player/index.ts +++ b/src/main/features/core/player/index.ts @@ -371,8 +371,14 @@ ipcMain.on('player-set-queue', async (_event, current?: string, next?: string, p // Replaces the queue in position 1 to the given data ipcMain.on('player-set-queue-next', async (_event, url?: string) => { try { + const size = await getMpvInstance()?.getPlaylistSize(); + + if (size && size > 1) { + await getMpvInstance()?.playlistRemove(1); + } + if (url) { - await getMpvInstance()?.load(url, 'append'); + getMpvInstance()?.load(url, 'append'); } } catch (err: any | NodeMpvError) { mpvLog({ action: `Failed to set play queue` }, err); diff --git a/src/renderer/store/player.store.ts b/src/renderer/store/player.store.ts index 3e3af190e..d6dc04b1c 100644 --- a/src/renderer/store/player.store.ts +++ b/src/renderer/store/player.store.ts @@ -2383,12 +2383,7 @@ export const subscribeCurrentTrack = ( ); }; -export const subscribeNextSong = ( - onChange: ( - properties: { index: number; song: QueueSong | undefined }, - prev: { index: number; song: QueueSong | undefined }, - ) => void, -) => { +export const subscribeNextSongInsertion = (onChange: (song: QueueSong | undefined) => void) => { return usePlayerStoreBase.subscribe( (state) => { const queue = state.getQueue(); @@ -2417,21 +2412,25 @@ export const subscribeNextSong = ( nextSong = calculateNextSong(queueIndex, queue.items, repeat); } - // Calculate the index for the next song - let nextIndex: number | undefined; - if (nextSong) { - nextIndex = queue.items.findIndex((item) => item._uniqueId === nextSong?._uniqueId); - } - - return { index: nextIndex ?? -1, song: nextSong }; + return { index: queueIndex, song: nextSong }; }, - (nextSong, prevNextSong) => { - onChange(nextSong, prevNextSong); + (current, prev) => { + // Only trigger if: + // 1. We have a previous value (not the first call) + // 2. Index hasn't changed (not a natural advance) + // 3. Next song has changed (song was inserted) + if ( + prev && + current.index === prev.index && + current.song?._uniqueId !== prev.song?._uniqueId + ) { + // Index stayed the same but next song changed = insertion at next position + onChange(current.song); + } }, { - equalityFn: (a, b) => { - return a.song?._uniqueId === b.song?._uniqueId; - }, + // Always allow the subscription to fire so we can check conditions in the callback + equalityFn: () => false, }, ); };