webapp/src/components/Pages/ArticlePage.tsx

50 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-09-22 09:37:49 +00:00
import { MainLayout } from '../Layouts/MainLayout'
import { ArticleView } from '../Views/Article'
import type { PageProps } from '../types'
import { loadArticle, useArticlesStore } from '../../stores/zine/articles'
import { createMemo, onMount, Show } from 'solid-js'
import { t } from '../../utils/intl'
import type { Shout } from '../../graphql/types.gen'
import { useRouter } from '../../stores/router'
export const ArticlePage = (props: PageProps) => {
const sortedArticles = props.article ? [props.article] : []
2022-10-05 15:11:14 +00:00
const slug = createMemo(() => {
const { getPage } = useRouter()
2022-09-22 09:37:49 +00:00
2022-10-05 15:11:14 +00:00
const page = getPage()
2022-09-22 09:37:49 +00:00
2022-10-05 15:11:14 +00:00
if (page.route !== 'article') {
throw new Error('ts guard')
}
return page.params.slug
})
2022-09-22 09:37:49 +00:00
2022-09-28 20:16:44 +00:00
const { articleEntities } = useArticlesStore({
2022-09-22 09:37:49 +00:00
sortedArticles
})
2022-10-05 15:11:14 +00:00
const article = createMemo<Shout>(() => articleEntities()[slug()])
2022-09-22 09:37:49 +00:00
onMount(() => {
2022-10-05 15:11:14 +00:00
const articleValue = articleEntities()[slug()]
2022-09-22 09:37:49 +00:00
2022-09-28 20:16:44 +00:00
if (!articleValue || !articleValue.body) {
2022-10-05 15:11:14 +00:00
loadArticle({ slug: slug() })
2022-09-22 09:37:49 +00:00
}
})
return (
<MainLayout headerTitle={article()?.title || ''}>
<Show when={Boolean(article())} fallback={t('Loading')}>
<ArticleView article={article()} />
</Show>
</MainLayout>
)
}
// for lazy loading
export default ArticlePage