Files
pardis/src/lib/router.svelte.js
Adrien W. 485116f2da
All checks were successful
Tests et déploiement / validate (push) Successful in 1m55s
Tests et déploiement / deploy (push) Successful in 37s
Nested UI for data & storage options
2026-10-10 12:08:06 +02:00

35 lines
1.1 KiB
JavaScript

/**
* A few screens, so no router dependency — just enough hash handling that the
* Android back button behaves inside an installed PWA.
*
* `route === null` means "wherever the day state says", which is the default the
* worker should land on.
*/
const ROUTES = ['resume', 'today', 'settings', 'stats', 'details', 'automations', 'non-working', 'data'];
function fromHash() {
const [path, query = ''] = location.hash.replace(/^#\/?/, '').split('?');
const date = new URLSearchParams(query).get('date');
return { route: ROUTES.includes(path) ? path : null,
selectedDate: date && /^\d{4}-\d{2}-\d{2}$/.test(date) ? date : '' };
}
export const router = $state(fromHash());
window.addEventListener('hashchange', () => {
const next = fromHash();
router.route = next.route;
if (next.selectedDate) router.selectedDate = next.selectedDate;
});
export function go(route) {
const date = ['resume', 'details'].includes(route) && router.selectedDate ? `?date=${router.selectedDate}` : '';
location.hash = route ? `#/${route}${date}` : '';
}
export function back() {
if (history.length > 1) history.back();
else go(null);
}