mirror of
https://github.com/45Drives/cockpit-navigator.git
synced 2025-07-28 08:04:39 +02:00
fix routing
This commit is contained in:
parent
a716954e1f
commit
8270d02f9d
@ -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;
|
||||
})
|
||||
|
||||
|
@ -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',
|
||||
}
|
||||
];
|
||||
|
||||
|
@ -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: {
|
||||
|
Loading…
x
Reference in New Issue
Block a user