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 '@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;
})

View File

@ -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',
}
];

View File

@ -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: {