Files
it-tools/src/router.ts
T
Dark_Phoenix_andShareVB be25e17687 Added loading delay (#92)
Added a loading delay of 750ms to prevent the loading screen from appearing too quickly. This caused a flickering effect when a page did load quickly
Added extra logic to prevent the loading screen from popping up when query parameters were changed (as some tools update query params when you change values)
Made the background color of the loading screen lighter to better see the black wrench in the logo of it-tools (#101014 -> #292929)
Improvements for #32

---------

Co-authored-by: ShareVB <sharevb@gmail.com>
2025-06-15 12:50:15 +02:00

64 lines
1.7 KiB
TypeScript

import { createRouter, createWebHistory } from 'vue-router';
import { type ActiveLoader, useLoading } from 'vue-loading-overlay';
import { layouts } from './layouts/index';
import HomePage from './pages/Home.page.vue';
import NotFound from './pages/404.page.vue';
import { tools } from './tools';
import { config } from './config';
import { routes as demoRoutes } from './ui/demo/demo.routes';
const $loading = useLoading();
const toolsRoutes = tools.map(({ path, name, component, ...config }) => ({
path,
name,
component,
meta: { isTool: true, layout: layouts.toolLayout, name, ...config },
}));
const toolsRedirectRoutes = tools
.filter(({ redirectFrom }) => redirectFrom && redirectFrom.length > 0)
.flatMap(
({ path, redirectFrom }) => redirectFrom?.map(redirectSource => ({ path: redirectSource, redirect: path })) ?? [],
);
const router = createRouter({
history: createWebHistory(config.app.baseUrl),
routes: [
{
path: '/',
name: 'home',
component: HomePage,
},
{
path: '/about',
name: 'about',
component: () => import('./pages/About.vue'),
},
...toolsRoutes,
...toolsRedirectRoutes,
...(config.app.env === 'development' ? demoRoutes : []),
{ path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
],
});
let loader: ActiveLoader | null = null;
router.beforeEach((to, from) => {
// Only show loading for actual route changes, not just query param changes
if (to.path !== from.path) {
loader = $loading?.show({
color: '#fff',
backgroundColor: '#292929',
});
}
});
router.afterEach(() => {
if (loader) {
loader.hide();
loader = null;
}
});
export default router;