webapp/src/components/Root.tsx

104 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-09-22 09:37:49 +00:00
// FIXME: breaks on vercel, research
// import 'solid-devtools'
2022-10-25 16:25:42 +00:00
import { MODALS, setLocale, showModal } from '../stores/ui'
import { Component, createEffect, createMemo, onMount } from 'solid-js'
2022-09-22 09:37:49 +00:00
import { Routes, useRouter } from '../stores/router'
2022-09-24 10:34:57 +00:00
import { Dynamic, isServer } from 'solid-js/web'
2022-09-22 09:37:49 +00:00
import { getLogger } from '../utils/logger'
import type { PageProps } from './types'
2022-10-07 09:23:19 +00:00
import { HomePage } from './Pages/HomePage'
import { AllTopicsPage } from './Pages/AllTopicsPage'
import { TopicPage } from './Pages/TopicPage'
import { AllAuthorsPage } from './Pages/AllAuthorsPage'
import { AuthorPage } from './Pages/AuthorPage'
import { FeedPage } from './Pages/FeedPage'
import { ArticlePage } from './Pages/ArticlePage'
import { SearchPage } from './Pages/SearchPage'
import { FourOuFourPage } from './Pages/FourOuFourPage'
2022-10-31 20:42:20 +00:00
import { DiscussionRulesPage } from './Pages/about/DiscussionRulesPage'
2022-10-07 09:23:19 +00:00
import { DogmaPage } from './Pages/about/DogmaPage'
import { GuidePage } from './Pages/about/GuidePage'
import { HelpPage } from './Pages/about/HelpPage'
import { ManifestPage } from './Pages/about/ManifestPage'
import { PartnersPage } from './Pages/about/PartnersPage'
2022-10-31 20:42:20 +00:00
import { PrinciplesPage } from './Pages/about/PrinciplesPage'
2022-10-07 09:23:19 +00:00
import { ProjectsPage } from './Pages/about/ProjectsPage'
import { TermsOfUsePage } from './Pages/about/TermsOfUsePage'
import { ThanksPage } from './Pages/about/ThanksPage'
import { CreatePage } from './Pages/CreatePage'
2022-11-02 22:11:30 +00:00
import { ConnectPage } from './Pages/ConnectPage'
2022-10-25 16:25:42 +00:00
import { renewSession } from '../stores/auth'
2022-11-12 18:59:29 +00:00
import { LayoutShoutsPage } from './Pages/LayoutShoutsPage'
2022-10-07 09:23:19 +00:00
// TODO: lazy load
2022-11-12 18:14:20 +00:00
// const SomePage = lazy(() => import('./Pages/SomePage'))
2022-09-22 09:37:49 +00:00
const log = getLogger('root')
2022-10-25 16:25:42 +00:00
type RootSearchParams = {
modal: string
lang: string
}
2022-09-22 09:37:49 +00:00
const pagesMap: Record<keyof Routes, Component<PageProps>> = {
2022-11-12 18:59:29 +00:00
layout: LayoutShoutsPage,
2022-11-02 22:11:30 +00:00
connect: ConnectPage,
2022-10-05 11:42:13 +00:00
create: CreatePage,
2022-09-22 09:37:49 +00:00
home: HomePage,
topics: AllTopicsPage,
topic: TopicPage,
authors: AllAuthorsPage,
author: AuthorPage,
feed: FeedPage,
article: ArticlePage,
2022-09-23 18:27:05 +00:00
search: SearchPage,
2022-10-31 20:42:20 +00:00
discussionRules: DiscussionRulesPage,
2022-09-23 18:27:05 +00:00
dogma: DogmaPage,
guide: GuidePage,
help: HelpPage,
manifest: ManifestPage,
projects: ProjectsPage,
partners: PartnersPage,
2022-10-31 20:42:20 +00:00
principles: PrinciplesPage,
2022-09-23 18:27:05 +00:00
termsOfUse: TermsOfUsePage,
thanks: ThanksPage
2022-09-22 09:37:49 +00:00
}
export const Root = (props: PageProps) => {
2022-10-25 16:25:42 +00:00
const { page, searchParams } = useRouter<RootSearchParams>()
2022-09-22 09:37:49 +00:00
2022-10-25 16:25:42 +00:00
createEffect(() => {
const modal = MODALS[searchParams().modal]
if (modal) {
showModal(modal)
}
})
2022-09-22 09:37:49 +00:00
2022-10-25 16:25:42 +00:00
onMount(() => {
renewSession()
})
2022-09-22 09:37:49 +00:00
2022-10-25 16:25:42 +00:00
const pageComponent = createMemo(() => {
const result = pagesMap[page().route]
2022-09-22 09:37:49 +00:00
2022-10-25 16:25:42 +00:00
if (!result || page().path === '/404') {
2022-09-22 09:37:49 +00:00
return FourOuFourPage
}
return result
})
2022-09-24 10:34:57 +00:00
if (!isServer) {
createEffect(() => {
2022-10-25 16:25:42 +00:00
const lang = searchParams().lang || 'ru'
2022-09-24 10:34:57 +00:00
console.log('[root] client locale is', lang)
setLocale(lang)
2022-10-25 16:25:42 +00:00
})
2022-09-24 10:34:57 +00:00
}
2022-09-22 09:37:49 +00:00
return <Dynamic component={pageComponent()} {...props} />
}