webapp/src/stores/router.ts

136 lines
3.6 KiB
TypeScript
Raw Normal View History

2022-09-22 09:37:49 +00:00
import type { Accessor } from 'solid-js'
2022-09-09 13:30:25 +00:00
import { createRouter, createSearchParams } from '@nanostores/router'
2022-09-09 11:53:35 +00:00
import { isServer } from 'solid-js/web'
2022-09-22 09:37:49 +00:00
import { useStore } from '@nanostores/solid'
2022-09-09 11:53:35 +00:00
2023-02-10 01:19:20 +00:00
export const ROUTES = {
home: '/',
inbox: '/inbox',
connect: '/connect',
create: '/create',
createSettings: '/create/settings',
topics: '/topics',
topic: '/topic/:slug',
authors: '/authors',
author: '/author/:slug',
feed: '/feed',
search: '/search/:q?',
article: '/:slug',
dogma: '/about/dogma',
discussionRules: '/about/discussion-rules',
guide: '/about/guide',
help: '/about/help',
manifest: '/about/manifest',
partners: '/about/partners',
principles: '/about/principles',
projects: '/about/projects',
termsOfUse: '/about/terms-of-use',
thanks: '/about/thanks',
expo: '/expo/:layout',
profileSettings: '/profile/settings',
profileSecurity: '/profile/security',
profileSubscriptions: '/profile/subscriptions'
} as const
2022-09-09 11:53:35 +00:00
2022-09-22 09:37:49 +00:00
const searchParamsStore = createSearchParams()
2023-02-10 01:19:20 +00:00
const routerStore = createRouter(ROUTES, {
search: false,
links: false
})
2022-09-09 11:53:35 +00:00
2022-09-22 09:37:49 +00:00
export const router = routerStore
const handleClientRouteLinkClick = (event) => {
2022-09-22 09:37:49 +00:00
const link = event.target.closest('a')
if (
link &&
event.button === 0 &&
link.target !== '_blank' &&
link.rel !== 'external' &&
!link.download &&
!event.metaKey &&
!event.ctrlKey &&
!event.shiftKey &&
!event.altKey
) {
const url = new URL(link.href)
if (url.origin === location.origin) {
event.preventDefault()
2022-10-25 16:25:42 +00:00
2022-12-07 18:38:05 +00:00
if (url.hash) {
2023-01-19 14:46:14 +00:00
let selector = url.hash
if (/^#\d+/.test(selector)) {
// id="1" fix
// https://stackoverflow.com/questions/20306204/using-queryselector-with-ids-that-are-numbers
selector = `[id="${selector.replace('#', '')}"]`
}
const anchor = document.querySelector(selector)
2022-12-07 18:38:05 +00:00
const headerOffset = 80 // 100px for header
2023-02-17 09:21:02 +00:00
const elementPosition = anchor ? anchor.getBoundingClientRect().top : 0
2022-12-07 18:38:05 +00:00
const newScrollTop = elementPosition + window.scrollY - headerOffset
window.scrollTo({
top: newScrollTop,
behavior: 'smooth'
})
return
}
2022-09-22 09:37:49 +00:00
routerStore.open(url.pathname)
2022-10-25 16:25:42 +00:00
const params = Object.fromEntries(new URLSearchParams(url.search))
searchParamsStore.open(params)
2022-09-29 14:40:11 +00:00
window.scrollTo({
top: 0,
left: 0
})
2022-09-22 09:37:49 +00:00
}
}
}
2023-02-17 09:21:02 +00:00
export const initRouter = (pathname: string, search: Record<string, string>) => {
2022-09-22 09:37:49 +00:00
routerStore.open(pathname)
const params = Object.fromEntries(new URLSearchParams(search))
searchParamsStore.open(params)
if (!isServer) {
document.addEventListener('click', handleClientRouteLinkClick)
}
2022-09-09 11:53:35 +00:00
}
if (!isServer) {
2022-09-14 11:28:43 +00:00
const { pathname, search } = window.location
2023-02-17 09:21:02 +00:00
const searchParams = Object.fromEntries(new URLSearchParams(search))
initRouter(pathname, searchParams)
2022-09-22 09:37:49 +00:00
}
export const useRouter = <TSearchParams extends Record<string, string> = Record<string, string>>() => {
2022-10-25 16:25:42 +00:00
const page = useStore(routerStore)
const searchParams = useStore(searchParamsStore) as unknown as Accessor<TSearchParams>
const changeSearchParam = <TKey extends keyof TSearchParams>(
key: TKey,
value: TSearchParams[TKey],
replace = false
) => {
const newSearchParams = { ...searchParamsStore.get() }
if (value === null) {
delete newSearchParams[key.toString()]
} else {
newSearchParams[key.toString()] = value
}
2022-09-22 09:37:49 +00:00
2022-10-25 16:25:42 +00:00
searchParamsStore.open(newSearchParams, replace)
2022-09-22 09:37:49 +00:00
}
return {
2022-10-25 16:25:42 +00:00
page,
searchParams,
2022-11-17 06:56:24 +00:00
changeSearchParam,
handleClientRouteLinkClick
2022-09-22 09:37:49 +00:00
}
2022-09-09 11:53:35 +00:00
}