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'
|
|
|
|
|
|
|
|
// Types for :params in route templates
|
|
|
|
interface Routes {
|
|
|
|
home: void // TODO: more
|
|
|
|
topics: void
|
|
|
|
authors: void
|
|
|
|
feed: void
|
|
|
|
post: 'slug'
|
|
|
|
article: 'slug'
|
|
|
|
expo: 'slug'
|
|
|
|
create: 'collab'
|
|
|
|
search: 'q'
|
|
|
|
inbox: 'chat'
|
|
|
|
author: 'slug'
|
|
|
|
topic: 'slug'
|
|
|
|
}
|
|
|
|
|
2022-09-09 13:30:25 +00:00
|
|
|
export const params = createSearchParams()
|
2022-09-09 11:53:35 +00:00
|
|
|
export const router = createRouter<Routes>(
|
|
|
|
{
|
|
|
|
home: '/',
|
|
|
|
topics: '/topics',
|
|
|
|
authors: '/authors',
|
|
|
|
feed: '/feed',
|
|
|
|
create: '/create/:collab?',
|
|
|
|
inbox: '/inbox/:chat?',
|
|
|
|
search: '/search/:q?',
|
|
|
|
post: '/:slug',
|
|
|
|
article: '/articles/:slug',
|
|
|
|
expo: '/expo/:layout/:topic/:slug',
|
|
|
|
author: '/author/:slug',
|
|
|
|
topic: '/topic/:slug'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// enabling search query params passing
|
|
|
|
search: true,
|
|
|
|
links: false
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-09-14 11:28:43 +00:00
|
|
|
export const handleClientRouteLinkClick = (ev) => {
|
|
|
|
const href = ev.target.href
|
|
|
|
console.log('[router] faster link', href)
|
|
|
|
ev.stopPropagation()
|
|
|
|
ev.preventDefault()
|
|
|
|
router.open(href)
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isServer) {
|
2022-09-14 11:28:43 +00:00
|
|
|
const { pathname, search } = window.location
|
|
|
|
router.open(pathname + search)
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|