webapp/src/components/Pages/ArticlePage.tsx

50 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-11-14 17:41:05 +00:00
import { PageWrap } from '../_shared/PageWrap'
2022-09-22 09:37:49 +00:00
import { ArticleView } from '../Views/Article'
import type { PageProps } from '../types'
2022-11-15 14:24:50 +00:00
import { loadShoutsBy, useArticlesStore } from '../../stores/zine/articles'
2022-09-22 09:37:49 +00:00
import { createMemo, onMount, Show } from 'solid-js'
import type { Shout } from '../../graphql/types.gen'
import { useRouter } from '../../stores/router'
2022-10-09 10:56:39 +00:00
import { Loading } from '../Loading'
2022-09-22 09:37:49 +00:00
export const ArticlePage = (props: PageProps) => {
2022-11-15 14:24:50 +00:00
const shouts = props.article ? [props.article] : []
2022-09-22 09:37:49 +00:00
2022-10-05 15:11:14 +00:00
const slug = createMemo(() => {
2022-10-25 16:25:42 +00:00
const { page: 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-11-15 14:24:50 +00:00
shouts
2022-09-22 09:37:49 +00:00
})
2022-10-05 15:11:14 +00:00
const article = createMemo<Shout>(() => articleEntities()[slug()])
2022-09-22 09:37:49 +00:00
2022-11-15 14:24:50 +00:00
onMount(async () => {
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-11-15 14:24:50 +00:00
await loadShoutsBy({ by: { slug: slug() }, limit: 1, offset: 0 })
2022-09-22 09:37:49 +00:00
}
})
return (
<PageWrap headerTitle={article()?.title || ''}>
2022-10-09 10:56:39 +00:00
<Show when={Boolean(article())} fallback={<Loading />}>
2022-09-22 09:37:49 +00:00
<ArticleView article={article()} />
</Show>
</PageWrap>
2022-09-22 09:37:49 +00:00
)
}
// for lazy loading
export default ArticlePage