From 5dbb7a2519d6eaed634aeee6c78502941ff527fb Mon Sep 17 00:00:00 2001
From: Phil Hughes <me@iamphill.com>
Date: Fri, 23 Mar 2018 09:03:45 +0000
Subject: [PATCH] fixed opening next tab being a pending tab clears any active
 files when opening pending tab

---
 .../javascripts/ide/stores/actions/file.js    | 14 ++++++---
 .../javascripts/ide/stores/mutations/file.js  | 31 ++++++++++++++-----
 2 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/app/assets/javascripts/ide/stores/actions/file.js b/app/assets/javascripts/ide/stores/actions/file.js
index dbb42154ec..8c41782cfb 100644
--- a/app/assets/javascripts/ide/stores/actions/file.js
+++ b/app/assets/javascripts/ide/stores/actions/file.js
@@ -12,7 +12,7 @@ export const closeFile = ({ commit, state, getters, dispatch }, file) => {
   if (file.pending) {
     commit(types.REMOVE_PENDING_TAB, file);
   } else {
-    const indexOfClosedFile = state.openFiles.findIndex(f => f.path === path);
+    const indexOfClosedFile = getters.tabs.findIndex(f => f.path === path);
     const fileWasActive = file.active;
 
     commit(types.TOGGLE_FILE_OPEN, path);
@@ -21,9 +21,13 @@ export const closeFile = ({ commit, state, getters, dispatch }, file) => {
     if (getters.tabs.length > 0 && fileWasActive) {
       const nextIndexToOpen =
         indexOfClosedFile === 0 ? 0 : indexOfClosedFile - 1;
-      const nextFileToOpen = state.openFiles[nextIndexToOpen];
+      const nextFileToOpen = getters.tabs[nextIndexToOpen];
 
-      router.push(`/project${nextFileToOpen.url}`);
+      if (nextFileToOpen.pending) {
+        dispatch('openPendingTab', nextFileToOpen);
+      } else {
+        router.push(`/project${nextFileToOpen.url}`);
+      }
     } else if (!state.openFiles.length) {
       router.push(`/project/${file.projectId}/tree/${file.branchId}/`);
     }
@@ -151,9 +155,11 @@ export const discardFileChanges = ({ state, commit }, path) => {
   eventHub.$emit(`editor.update.model.content.${file.path}`, file.raw);
 };
 
-export const openPendingTab = ({ commit, state }, file) => {
+export const openPendingTab = ({ commit, dispatch, state }, file) => {
   commit(types.ADD_PENDING_TAB, file);
 
+  dispatch('scrollToTab');
+
   router.push(`/project/${file.projectId}/tree/${state.currentBranchId}/`);
 };
 
diff --git a/app/assets/javascripts/ide/stores/mutations/file.js b/app/assets/javascripts/ide/stores/mutations/file.js
index 06fd55bfa8..65ea1e46b5 100644
--- a/app/assets/javascripts/ide/stores/mutations/file.js
+++ b/app/assets/javascripts/ide/stores/mutations/file.js
@@ -88,14 +88,29 @@ export default {
     });
   },
   [types.ADD_PENDING_TAB](state, file) {
-    Object.assign(state, {
-      pendingTabs: state.pendingTabs.concat({
-        ...file,
-        active: true,
-        pending: true,
-        key: `pending-${file.key}`,
-      }),
-    });
+    const pendingTab = state.pendingTabs.find(f => f.path === file.path);
+
+    if (pendingTab) {
+      Object.assign(state, {
+        pendingTabs: state.pendingTabs.map(tab => ({
+          ...tab,
+          active: !!pendingTab,
+        })),
+      });
+    } else {
+      Object.assign(state, {
+        pendingTabs: state.pendingTabs.concat({
+          ...file,
+          active: true,
+          pending: true,
+          key: `pending-${file.key}`,
+        }),
+        openFiles: state.openFiles.map(f => ({
+          ...f,
+          active: false,
+        })),
+      });
+    }
   },
   [types.REMOVE_PENDING_TAB](state, file) {
     Object.assign(state, {
-- 
2.30.9