From 2ae41b7e50ea9cc08a6711eb52a3bd00a570fed6 Mon Sep 17 00:00:00 2001
From: Bram Wiepjes <bramw@protonmail.com>
Date: Wed, 11 Oct 2023 20:27:57 +0000
Subject: [PATCH] Resolve "Navigation is messed up after introducing
 remembering the last view visited feature."

---
 ...igation_bug_with_last_remembered_view.json |  7 +++++++
 .../database/components/table/Table.vue       |  4 ++--
 web-frontend/modules/database/pages/table.vue | 20 +++++++++++++++++--
 web-frontend/test/helpers/testApp.js          |  3 +++
 4 files changed, 30 insertions(+), 4 deletions(-)
 create mode 100644 changelog/entries/unreleased/bug/2006_fix_navigation_bug_with_last_remembered_view.json

diff --git a/changelog/entries/unreleased/bug/2006_fix_navigation_bug_with_last_remembered_view.json b/changelog/entries/unreleased/bug/2006_fix_navigation_bug_with_last_remembered_view.json
new file mode 100644
index 000000000..d0c5e9ec7
--- /dev/null
+++ b/changelog/entries/unreleased/bug/2006_fix_navigation_bug_with_last_remembered_view.json
@@ -0,0 +1,7 @@
+{
+  "type": "bug",
+  "message": "Fix bug where the navigation history was wrong after last view.",
+  "issue_number": 2006,
+  "bullet_points": [],
+  "created_at": "2023-10-05"
+}
diff --git a/web-frontend/modules/database/components/table/Table.vue b/web-frontend/modules/database/components/table/Table.vue
index 5debadbee..2349cdb4e 100644
--- a/web-frontend/modules/database/components/table/Table.vue
+++ b/web-frontend/modules/database/components/table/Table.vue
@@ -34,10 +34,10 @@
                 <EditableViewName ref="rename" :view="view"></EditableViewName>
               </span>
             </template>
-            <span v-else-if="view !== null">
+            <template v-else-if="view !== null">
               <i class="header__filter-icon iconoir-nav-arrow-down"></i>
               {{ $t('table.chooseView') }}
-            </span>
+            </template>
           </a>
           <ViewsContext
             v-if="views !== null"
diff --git a/web-frontend/modules/database/pages/table.vue b/web-frontend/modules/database/pages/table.vue
index f7f76fe07..59693efdd 100644
--- a/web-frontend/modules/database/pages/table.vue
+++ b/web-frontend/modules/database/pages/table.vue
@@ -207,6 +207,22 @@ export default {
     this.$store.dispatch('table/setLoading', false)
   },
   mounted() {
+    // If no view route parameter is provided, then the `asyncData` function
+    // automatically selects the last viewed view, or the first one. This is however
+    // not updated in the URL path, so to make the history navigation work, we must
+    // replace the existing path.
+    if (
+      this.view !== undefined &&
+      parseInt(this.$route.params.viewId) !== this.view.id
+    ) {
+      this.$router.replace({
+        name: 'database-table',
+        params: {
+          viewId: this.view.id,
+        },
+      })
+    }
+
     this.$realtime.subscribe('table', { table_id: this.table.id })
   },
   beforeDestroy() {
@@ -218,7 +234,7 @@ export default {
         return
       }
 
-      this.$nuxt.$router.push({
+      this.$router.push({
         name: 'database-table',
         params: {
           viewId: view.id,
@@ -256,7 +272,7 @@ export default {
           rowId,
         },
       }
-      this.$nuxt.$router.push(location)
+      this.$router.push(location)
     },
     async fetchAdjacentRow(previous, activeSearchTerm = null) {
       const { row, status } = await this.$store.dispatch(
diff --git a/web-frontend/test/helpers/testApp.js b/web-frontend/test/helpers/testApp.js
index a8ce72d05..098a5b059 100644
--- a/web-frontend/test/helpers/testApp.js
+++ b/web-frontend/test/helpers/testApp.js
@@ -121,6 +121,9 @@ export class TestApp {
         resolve({ name, params }) {
           return new URL(`https://${name}`)
         },
+        replace({ name, params }) {
+          return new URL(`https://${name}`)
+        },
       },
       $route: {
         params: {},