fix routing

This commit is contained in:
joshuaboud 2022-05-24 15:53:04 -03:00
parent a716954e1f
commit 8270d02f9d
No known key found for this signature in database
GPG Key ID: 17EFB59E2A8BF50E
3 changed files with 26 additions and 31 deletions

View File

@ -3,6 +3,7 @@ import App from './App.vue';
import { errorString, FIFO } from '@45drives/cockpit-helpers'; import { errorString, FIFO } from '@45drives/cockpit-helpers';
import '@45drives/cockpit-css/src/index.css'; import '@45drives/cockpit-css/src/index.css';
import { useSpawn, errorStringHTML } from '@45drives/cockpit-helpers'; import { useSpawn, errorStringHTML } from '@45drives/cockpit-helpers';
import { lastPathStorageKey } from './keys';
import router from './router'; import router from './router';
@ -33,7 +34,14 @@ const errorHandler = (error, title = "System Error") => {
throw error; throw error;
} }
let lastValidRoutePath = null;
router.beforeEach(async (to, from) => { 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.name === 'browse') {
if (!to.params.path) if (!to.params.path)
return "/browse/"; // force / for opening root return "/browse/"; // force / for opening root
@ -48,15 +56,13 @@ router.beforeEach(async (to, from) => {
} }
} catch (error) { } catch (error) {
if (from.name === undefined) 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"); errorHandler(errorStringHTML(error), "Failed to open path");
return false; return false;
} }
} }
if (cockpit.location.href !== to.fullPath.replace(/\/$/, '') && to.name !== 'errorRedirect') { lastValidRoutePath = to.fullPath; // protect double-update from next line
cockpit.location.replace(to.fullPath); cockpit.location.go(to.fullPath); // needed to update URL in address bar
return false; // avoid double render from router change and cockpit path change
}
return true; return true;
}) })

View File

@ -18,9 +18,13 @@ const routes = [
props: route => ({ title: route.query.title, message: route.query.message }), props: route => ({ title: route.query.title, message: route.query.message }),
}, },
{ {
path: '/:pathMatch(.*)', path: '/:pathMatch(.+)',
name: 'notFound', name: 'notFound',
redirect: route => ({ name: 'errorRedirect', query: { title: 'Not found', message: `${route.href} is not a valid location.` }}), redirect: route => ({ name: 'errorRedirect', query: { title: 'Not found', message: `${route.href} is not a valid location.` }}),
},
{
path: '/',
name: 'root',
} }
]; ];

View File

@ -167,6 +167,8 @@ export default {
router.push(`/edit${path}`); router.push(`/edit${path}`);
} }
const getSelected = () => directoryViewRef.value?.getSelected?.() ?? [];
watch(searchFilterStr, () => { watch(searchFilterStr, () => {
searchFilterRegExp.value = new RegExp( searchFilterRegExp.value = new RegExp(
`^${searchFilterStr.value `^${searchFilterStr.value
@ -177,33 +179,15 @@ export default {
); );
}, { immediate: true }); }, { immediate: true });
watch(() => route.params.path, async (current, last) => { watch(() => route.params.path, async (newPath, lastPath) => {
if (!last) { if (!lastPath) {
console.log("First watch execute", last); console.log("First watch execute", lastPath);
} }
if (route.name !== 'browse' || current === last) if (route.name !== 'browse' || newPath === lastPath)
return; return;
try { localStorage.setItem(lastPathStorageKey, newPath);
const tmpPath = route.params.path; if (pathHistory.current() !== newPath) {
// let realPath = (await useSpawn(['realpath', '--canonicalize-existing', tmpPath], { superuser: 'try' }).promise()).stdout.trim(); pathHistory.push(newPath); // updates actual view
// 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();
} }
}, { immediate: true }); }, { immediate: true });
@ -221,6 +205,7 @@ export default {
forward, forward,
up, up,
openEditor, openEditor,
getSelected,
} }
}, },
components: { components: {