function parse() {
const [path, qs] = (location.hash.slice(1) || '/logs').split('?')
return { path, params: Object.fromEntries(new URLSearchParams(qs || '')) }
}
export const route = $state({ ...parse(), nonce: 0 })
function sync() {
const next = parse()
route.path = next.path
route.params = next.params
route.nonce++
}
addEventListener('hashchange', sync)
export function go(path, params = {}) {
const qs = new URLSearchParams(
Object.entries(params).filter(([, v]) => v !== '' && v != null),
).toString()
const next = '#' + path + (qs ? '?' + qs : '')
if (location.hash === next || (!location.hash && next === '#/logs')) sync0(path, params)
else location.hash = next
}
function sync0(path, params) {
route.path = path
route.params = { ...params }
route.nonce++
}