From 8270d02f9d6c883b70a4325b9e822ff8aa50b052 Mon Sep 17 00:00:00 2001 From: joshuaboud Date: Tue, 24 May 2022 15:53:04 -0300 Subject: [PATCH] fix routing --- navigator-vue/src/main.js | 16 ++++++++----- navigator-vue/src/router/index.js | 6 ++++- navigator-vue/src/views/Browser.vue | 35 +++++++++-------------------- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/navigator-vue/src/main.js b/navigator-vue/src/main.js index b7d683f..8a1d842 100644 --- a/navigator-vue/src/main.js +++ b/navigator-vue/src/main.js @@ -3,6 +3,7 @@ import App from './App.vue'; import { errorString, FIFO } from '@45drives/cockpit-helpers'; import '@45drives/cockpit-css/src/index.css'; import { useSpawn, errorStringHTML } from '@45drives/cockpit-helpers'; +import { lastPathStorageKey } from './keys'; import router from './router'; @@ -33,7 +34,14 @@ const errorHandler = (error, title = "System Error") => { throw error; } +let lastValidRoutePath = null; router.beforeEach(async (to, from) => { + if (to.name === 'root') + return `/browse${localStorage.getItem(lastPathStorageKey) ?? '/'}`; + if (to.fullPath === lastValidRoutePath) { + console.log("Route to same path"); + return true; + } if (to.name === 'browse') { if (!to.params.path) return "/browse/"; // force / for opening root @@ -48,15 +56,13 @@ router.beforeEach(async (to, from) => { } } catch (error) { if (from.name === undefined) - return { name: 'errorRedirect', query: { title: "Error opening path", message: errorString(error) } } + return { name: 'errorRedirect', query: { title: "Error opening path", message: errorString(error), ...to.query } } errorHandler(errorStringHTML(error), "Failed to open path"); return false; } } - if (cockpit.location.href !== to.fullPath.replace(/\/$/, '') && to.name !== 'errorRedirect') { - cockpit.location.replace(to.fullPath); - return false; // avoid double render from router change and cockpit path change - } + lastValidRoutePath = to.fullPath; // protect double-update from next line + cockpit.location.go(to.fullPath); // needed to update URL in address bar return true; }) diff --git a/navigator-vue/src/router/index.js b/navigator-vue/src/router/index.js index 407f8ec..acf322b 100644 --- a/navigator-vue/src/router/index.js +++ b/navigator-vue/src/router/index.js @@ -18,9 +18,13 @@ const routes = [ props: route => ({ title: route.query.title, message: route.query.message }), }, { - path: '/:pathMatch(.*)', + path: '/:pathMatch(.+)', name: 'notFound', redirect: route => ({ name: 'errorRedirect', query: { title: 'Not found', message: `${route.href} is not a valid location.` }}), + }, + { + path: '/', + name: 'root', } ]; diff --git a/navigator-vue/src/views/Browser.vue b/navigator-vue/src/views/Browser.vue index dd426c4..4ed2498 100644 --- a/navigator-vue/src/views/Browser.vue +++ b/navigator-vue/src/views/Browser.vue @@ -167,6 +167,8 @@ export default { router.push(`/edit${path}`); } + const getSelected = () => directoryViewRef.value?.getSelected?.() ?? []; + watch(searchFilterStr, () => { searchFilterRegExp.value = new RegExp( `^${searchFilterStr.value @@ -177,33 +179,15 @@ export default { ); }, { immediate: true }); - watch(() => route.params.path, async (current, last) => { - if (!last) { - console.log("First watch execute", last); + watch(() => route.params.path, async (newPath, lastPath) => { + if (!lastPath) { + console.log("First watch execute", lastPath); } - if (route.name !== 'browse' || current === last) + if (route.name !== 'browse' || newPath === lastPath) return; - try { - const tmpPath = route.params.path; - // let realPath = (await useSpawn(['realpath', '--canonicalize-existing', tmpPath], { superuser: 'try' }).promise()).stdout.trim(); - // if (tmpPath !== realPath) - // return cd(realPath); - try { - await useSpawn(['test', '-r', tmpPath, '-a', '-x', tmpPath], { superuser: 'try' }).promise(); - } catch (error) { - console.error(error); - throw new Error(`Permission denied for ${tmpPath}`); - } - localStorage.setItem(lastPathStorageKey, tmpPath); - if (pathHistory.current() !== tmpPath) { - pathHistory.push(tmpPath); // updates actual view - } - } catch (error) { - notifications.value.constructNotification("Failed to open path", errorStringHTML(error), 'error'); - if (pathHistory.backAllowed()) - back(); - else - up(); + localStorage.setItem(lastPathStorageKey, newPath); + if (pathHistory.current() !== newPath) { + pathHistory.push(newPath); // updates actual view } }, { immediate: true }); @@ -221,6 +205,7 @@ export default { forward, up, openEditor, + getSelected, } }, components: {