35 lines
1.1 KiB
JavaScript
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);
|
|
}
|