funkwhale/front/src/router/index.ts

43 wiersze
1.1 KiB
TypeScript

import { useLocalStorage } from '@vueuse/core'
2022-07-01 10:36:02 +00:00
import { createRouter, createWebHistory } from 'vue-router'
import { forceInstanceChooser } from './guards'
import routesV1 from './routes'
import routesV2 from '~/ui/routes'
// TODO:
// Research...
// - "What is the use case for this toggle?"
// - "Is Local Storage (persistence on a specific browser
// on a specific machine) the right place?"
const isUIv2 = useLocalStorage('ui-v2', true)
const routes = isUIv2.value ? routesV2 : routesV1
const router = createRouter({
2022-04-18 16:17:51 +00:00
history: createWebHistory(import.meta.env.VUE_APP_ROUTER_BASE_URL as string ?? '/'),
linkActiveClass: 'active',
2022-07-01 10:36:02 +00:00
routes,
2022-06-30 03:37:03 +00:00
scrollBehavior (to, from, savedPosition) {
if (to.meta.preserveScrollPosition) {
return savedPosition ?? { left: 0, top: 0 }
}
2022-04-30 18:03:22 +00:00
2022-06-30 03:37:03 +00:00
return new Promise(resolve => {
setTimeout(() => {
if (to.hash) {
resolve({ el: to.hash, behavior: 'smooth' })
}
2022-04-30 18:03:22 +00:00
2022-06-30 03:37:03 +00:00
resolve(savedPosition ?? { left: 0, top: 0 })
}, 100)
})
2022-07-01 10:36:02 +00:00
}
})
router.beforeEach((to, from, next) => {
return forceInstanceChooser(to, from, next)
})
export default router