From 0cf83f3000a21cba00ef0b7de8c30d07e397a6d8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcos=20Pe=C3=B1a?= <devilcius@gmail.com>
Date: Thu, 28 Oct 2021 08:17:33 +0000
Subject: [PATCH] Resolve "Removing last track in queue jumps player to new
 last track (instead of continuing current track) (#1485)"

---
 changes/changelog.d/1485.bugfix            |  1 +
 front/src/store/queue.js                   | 10 +++++-----
 front/tests/unit/specs/store/queue.spec.js | 14 ++++++++++++++
 3 files changed, 20 insertions(+), 5 deletions(-)
 create mode 100644 changes/changelog.d/1485.bugfix

diff --git a/changes/changelog.d/1485.bugfix b/changes/changelog.d/1485.bugfix
new file mode 100644
index 000000000..7d0ffb379
--- /dev/null
+++ b/changes/changelog.d/1485.bugfix
@@ -0,0 +1 @@
+Fixed before last track starts playing when last track removed (#1485)
diff --git a/front/src/store/queue.js b/front/src/store/queue.js
index 5acff3efb..e096a6f0a 100644
--- a/front/src/store/queue.js
+++ b/front/src/store/queue.js
@@ -97,18 +97,18 @@ export default {
 
     cleanTrack ({state, dispatch, commit}, index) {
       // are we removing current playin track
-      let current = index === state.currentIndex
+      const current = index === state.currentIndex
       if (current) {
         dispatch('player/stop', null, {root: true})
       }
       commit('splice', {start: index, size: 1})
       if (index < state.currentIndex) {
         commit('currentIndex', state.currentIndex - 1)
-      } else if (index > 0 && index === state.tracks.length) {
+      } else if (index > 0 && index === state.tracks.length && current) {
         // kind of a edge case: if you delete the last track of the queue
-        // we set current index to the previous one to avoid the queue tab from
-        // being stuck because the player disappeared
-        // cf #1092
+        // while it's playing we set current index to the previous one to
+        // avoid the queue tab from being stuck because the player
+        // disappeared cf #1092
         commit('currentIndex', state.tracks.length - 1)
       } else if (current) {
         // we play next track, which now have the same index
diff --git a/front/tests/unit/specs/store/queue.spec.js b/front/tests/unit/specs/store/queue.spec.js
index 7af57d97d..c3ac0be4b 100644
--- a/front/tests/unit/specs/store/queue.spec.js
+++ b/front/tests/unit/specs/store/queue.spec.js
@@ -164,6 +164,20 @@ describe('store/queue', () => {
         ]
       })
     })
+    it('cleanTrack current is last', () => {
+      testAction({
+        action: store.actions.cleanTrack,
+        payload: 5,
+        params: { state: { currentIndex: 5, tracks: [1, 2, 3, 4, 5] } },
+        expectedMutations: [
+          { type: 'splice', payload: { start: 5, size: 1 } },
+          { type: 'currentIndex', payload: 4 }
+        ],
+        expectedActions: [
+          { type: 'player/stop', payload: null, options: { root: true } }
+        ]
+      })
+    })
     it('previous when at beginning', () => {
       testAction({
         action: store.actions.previous,
-- 
GitLab