Files
Echoion/EchoionWeb/frontend/src/lib/router.ts
T
faumaray bdd69f0aad update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-25 23:44:48 +03:00

42 lines
1.2 KiB
TypeScript

// Tiny hash router: keeps the active view in the URL so views are
// bookmarkable and the browser back/forward buttons work. No dependency —
// the app has a single flat set of views.
import { appStore, type ViewName } from './app';
const VIEWS: readonly ViewName[] = [
'dashboard',
'queue',
'search',
'library',
'stats',
'server',
'updates',
'rpc',
];
// The view named by the current hash, defaulting to `dashboard`. Old `#now`
// bookmarks are intentionally canonicalized during router initialization.
export function viewFromHash(hash = location.hash): ViewName {
const raw = hash.replace(/^#\/?/, '');
const base = raw.split(/[/?]/)[0];
if (base === 'now') return 'dashboard';
return (VIEWS as readonly string[]).includes(base)
? (base as ViewName)
: 'dashboard';
}
export function initRouter() {
// Initial view comes from the hash (refresh / shared link).
const initial = viewFromHash();
appStore.set({ view: initial });
if (location.hash.replace(/^#\/?/, '').split(/[/?]/)[0] === 'now') {
history.replaceState(null, '', '#dashboard');
}
window.addEventListener('hashchange', () => {
const view = viewFromHash();
appStore.set({ view });
});
}