webapp/src/pages/topic.page.tsx

72 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-02-17 09:21:02 +00:00
import type { PageProps } from './types'
2024-02-04 11:25:21 +00:00
import { Show, createEffect, createMemo, createSignal, on, onCleanup, onMount } from 'solid-js'
2024-02-04 11:25:21 +00:00
import { PRERENDERED_ARTICLES_COUNT, TopicView } from '../components/Views/Topic'
import { Loading } from '../components/_shared/Loading'
import { PageLayout } from '../components/_shared/PageLayout'
2023-02-28 17:35:33 +00:00
import { ReactionsProvider } from '../context/reactions'
import { useRouter } from '../stores/router'
import { loadShouts, resetSortedArticles } from '../stores/zine/articles'
2022-09-22 09:37:49 +00:00
export const TopicPage = (props: PageProps) => {
const { page } = useRouter()
2024-02-04 09:30:06 +00:00
const slug = createMemo(() => page().params['slug'] as string)
2022-10-05 15:11:14 +00:00
const [isLoaded, setIsLoaded] = createSignal(
Boolean(props.topicShouts) && Boolean(props.topic) && props.topic.slug === slug(),
)
const preload = () =>
Promise.all([
2023-12-25 15:07:12 +00:00
loadShouts({
filters: { topic: slug(), featured: true },
2023-12-25 15:07:12 +00:00
limit: PRERENDERED_ARTICLES_COUNT,
offset: 0,
}),
])
2022-10-05 15:11:14 +00:00
onMount(async () => {
if (isLoaded()) {
return
}
await preload()
2022-10-05 15:11:14 +00:00
setIsLoaded(true)
})
createEffect(
on(
() => slug(),
async () => {
setIsLoaded(false)
resetSortedArticles()
await preload()
setIsLoaded(true)
},
{ defer: true },
),
)
2022-10-05 15:11:14 +00:00
onCleanup(() => resetSortedArticles())
const usePrerenderedData = props.topic?.slug === slug()
2022-09-22 09:37:49 +00:00
return (
<PageLayout title={props.seo.title}>
2023-02-28 17:35:33 +00:00
<ReactionsProvider>
<Show when={isLoaded()} fallback={<Loading />}>
<TopicView
topic={usePrerenderedData ? props.topic : null}
shouts={usePrerenderedData ? props.topicShouts : null}
topicSlug={slug()}
/>
</Show>
2023-02-28 17:35:33 +00:00
</ReactionsProvider>
2023-02-17 09:21:02 +00:00
</PageLayout>
2022-09-22 09:37:49 +00:00
)
}
2023-02-17 09:21:02 +00:00
export const Page = TopicPage