Something went wrong. Try again.
Testing building apps with atproto
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164/** * Code adapted (manually, no slop) * from https://github.com/nanostores/router/blob/main/index.js * to work with echoes. * * It is not currently used in the app, * but leaving it here as a reference as * we might need it. (I wanted to * procrastinate a bit so I built it hehehe) */import { echo } from "./echoes"
export function createRouter(routes, opts = {}) { let router = { page: echo(null), open: null, routes: null, cleanup: null } router.routes = Object.keys(routes).map(name => { let pattern = routes[name]
if (typeof pattern !== 'string') { return [name, ...[pattern].flat()] }
pattern = pattern.replace(/\/$/g, '') || '/'
let regexp = pattern .replace(/[\s!#$()+,.:<=?[\\\]^{|}]/g, '\\$&') .replace(/\/\\:(\w+)\\\?/g, '(?:/(?<$1>(?<=/)[^/]+))?') .replace(/\/\\:(\w+)/g, '/(?<$1>[^/]+)')
return [name, RegExp('^' + regexp + '$', 'i'), null, pattern] })
let prev let parse = href => { let url = new URL(href.replace(/#$/, ''), 'http://a') let cache = url.pathname + url.search + url.hash if (prev === cache) return false prev = cache
let path = opts.search ? url.pathname + url.search : url.pathname path = path.replace(/\/($|\?)/, '$1') || '/'
for (let [route, regexp, callback] of router.routes) { let match = path.match(regexp) if (match) { return { hash: url.hash, params: callback ? callback(...match.slice(1)) : Object.keys({ ...match.groups }).reduce((params, key) => { params[key] = match.groups[key] ? decodeURIComponent(match.groups[key]) : '' return params }, {}), path, route, search: Object.fromEntries(url.searchParams) } } } }
let click = event => { let link = event.target.closest('a') if ( link && event.button === 0 && // Left mouse button link.target !== '_blank' && // Not for new tab link.origin === location.origin && // Not external link link.rel !== 'external' && // Not external link link.target !== '_self' && // Now manually disabled !link.download && // Not download link !event.altKey && // Not download link by user !event.metaKey && // Not open in new tab by user !event.ctrlKey && // Not open in new tab by user !event.shiftKey && // Not open in new window by user !event.defaultPrevented // Click was not cancelled ) { event.preventDefault() let hashChanged = location.hash !== link.hash if (router.open(link.href) && hashChanged) { location.hash = link.hash if (link.hash === '' || link.hash === '#') { window.dispatchEvent(new HashChangeEvent('hashchange')) } } } }
let change = () => { let page = parse(location.href) if (page !== false) router.page.next(page) }
if (typeof window !== 'undefined' && typeof location !== 'undefined') { change() if (opts.links !== false) document.body.addEventListener('click', click) window.addEventListener('popstate', change) window.addEventListener('hashchange', change) router.cleanup = () => { prev = undefined document.body.removeEventListener('click', click) window.removeEventListener('popstate', change) window.removeEventListener('hashchange', change) }
} else { router.page.next(parse('/')) }
router.open = (path, redirect) => { let page = parse(path) if (page !== false) { router.page.next(page) if (router.page.value === page) { if (typeof history !== 'undefined') { history[redirect ? 'replaceState' : 'pushState'](null, null, path) } return page } prev = undefined } }
return router}
export function getPagePath(router, name, params, search) { if (typeof name === 'object') { search = params params = name.params name = name.route } let route = router.routes.find(i => i[0] === name) let path = route[3] .replace(/\/:\w+\?/g, i => { let param = params && params[i.slice(2, -1)] if (param) { return '/' + encodeURIComponent(param) } else { return '' } }) .replace(/\/:\w+/g, i => '/' + encodeURIComponent(params[i.slice(2)])) if (search) { let postfix = '' + new URLSearchParams(search) if (postfix) return path + '?' + postfix } return path}
export function openPage(router, name, params, search) { router.open(getPagePath(router, name, params, search))}
export function redirectPage(router, name, params, search) { router.open(getPagePath(router, name, params, search), true)}