webapp/src/components/Pages/TopicPage.tsx

49 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-11-14 17:41:05 +00:00
import { PageWrap } from '../_shared/PageWrap'
import { PRERENDERED_ARTICLES_COUNT, TopicView } from '../Views/Topic'
2022-09-22 09:37:49 +00:00
import type { PageProps } from '../types'
2022-10-05 15:11:14 +00:00
import { createMemo, createSignal, onCleanup, onMount, Show } from 'solid-js'
2022-11-18 02:23:04 +00:00
import { loadShouts, resetSortedArticles } from '../../stores/zine/articles'
2022-10-05 15:11:14 +00:00
import { useRouter } from '../../stores/router'
import { loadTopic } from '../../stores/zine/topics'
2022-10-09 10:56:39 +00:00
import { Loading } from '../Loading'
2022-09-22 09:37:49 +00:00
export const TopicPage = (props: PageProps) => {
2022-11-14 17:41:05 +00:00
const [isLoaded, setIsLoaded] = createSignal(Boolean(props.shouts) && Boolean(props.topic))
2022-10-05 15:11:14 +00:00
const slug = createMemo(() => {
2022-10-25 16:25:42 +00:00
const { page: getPage } = useRouter()
2022-10-05 15:11:14 +00:00
const page = getPage()
2022-10-05 17:55:17 +00:00
if (page.route !== 'topic') {
2022-10-05 15:11:14 +00:00
throw new Error('ts guard')
}
return page.params.slug
})
onMount(async () => {
if (isLoaded()) {
return
}
2022-11-18 02:23:04 +00:00
await loadShouts({ filters: { topic: slug() }, limit: PRERENDERED_ARTICLES_COUNT, offset: 0 })
2022-10-05 15:11:14 +00:00
await loadTopic({ slug: slug() })
setIsLoaded(true)
})
onCleanup(() => resetSortedArticles())
2022-09-22 09:37:49 +00:00
return (
<PageWrap>
2022-10-09 10:56:39 +00:00
<Show when={isLoaded()} fallback={<Loading />}>
2022-11-15 14:24:50 +00:00
<TopicView topic={props.topic} shouts={props.shouts} topicSlug={slug()} />
2022-10-05 15:11:14 +00:00
</Show>
</PageWrap>
2022-09-22 09:37:49 +00:00
)
}
// for lazy loading
export default TopicPage