webapp/src/routes/[slug]/[...tab].tsx

172 lines
5.8 KiB
TypeScript
Raw Normal View History

/**
* [slug].tsx
*
* # Dynamic Slug Route Handler
*
* ## Overview
*
* This file handles dynamic routing based on the `slug` parameter in the URL. Depending on the prefix of the slug, it renders different pages:
*
* - **Author Page**: If the `slug` starts with `@`, it renders the `AuthorPage` component for the specified author.
* - **Topic Page**: If the `slug` starts with `!`, it renders the `TopicPage` component for the specified topic.
* - **Article Page**: For all other slugs, it renders the `ArticlePageComponent`, displaying the full article details.
*
* ## Components
*
* - **SlugPage**: The main component that determines which page to render based on the `slug`.
* - **ArticlePageComponent**: Fetches and displays the detailed view of an article.
* - **AuthorPage**: Displays author-specific information (imported from `../author/[slug]/[...tab]`).
* - **TopicPage**: Displays topic-specific information (imported from `../topic/[slug]/[...tab]`).
*
* ## Data Fetching
*
* - **fetchShout**: Asynchronously fetches article data based on the `slug` using the `getShout` GraphQL query.
* - **createResource**: Utilized in `ArticlePageComponent` to fetch and manage article data reactively.**/
import { RouteDefinition, RouteSectionProps, useLocation, useParams } from '@solidjs/router'
2024-07-13 09:06:49 +00:00
import { HttpStatusCode } from '@solidjs/start'
import { ErrorBoundary, Show, Suspense, createEffect, on, onMount, createResource } from 'solid-js'
2024-07-13 09:06:49 +00:00
import { FourOuFourView } from '~/components/Views/FourOuFour'
import { Loading } from '~/components/_shared/Loading'
import { gaIdentity } from '~/config'
import { useLocalize } from '~/context/localize'
2024-07-13 09:36:23 +00:00
import { getShout } from '~/graphql/api/public'
2024-07-13 10:32:27 +00:00
import type { Author, Reaction, Shout, Topic } from '~/graphql/schema/core.gen'
2024-07-13 09:06:49 +00:00
import { initGA, loadGAScript } from '~/utils/ga'
import { descFromBody, keywordsFromTopics } from '~/utils/meta'
import { FullArticle } from '../../components/Article/FullArticle'
import { PageLayout } from '../../components/_shared/PageLayout'
import { ReactionsProvider } from '../../context/reactions'
2024-07-22 14:40:26 +00:00
import AuthorPage, { AuthorPageProps } from '../author/[slug]/[...tab]'
2024-07-13 10:32:27 +00:00
import TopicPage, { TopicPageProps } from '../topic/[slug]/[...tab]'
2024-07-13 09:06:49 +00:00
const fetchShout = async (slug: string): Promise<Shout | undefined> => {
if (slug.startsWith('@') || slug.startsWith('!')) return
2024-07-13 09:06:49 +00:00
const shoutLoader = getShout({ slug })
const result = await shoutLoader()
return result
}
export const route: RouteDefinition = {
load: async ({ params }) => ({
article: await fetchShout(params.slug)
})
}
2024-09-06 05:13:24 +00:00
export type ArticlePageProps = {
article?: Shout
comments?: Reaction[]
votes?: Reaction[]
author?: Author
}
2024-07-13 09:06:49 +00:00
2024-09-06 04:52:39 +00:00
export type SlugPageProps = {
2024-07-13 10:33:49 +00:00
article?: Shout
comments?: Reaction[]
votes?: Reaction[]
author?: Author
topics: Topic[]
}
2024-07-13 10:32:27 +00:00
export default function SlugPage(props: RouteSectionProps<SlugPageProps>) {
const { t } = useLocalize()
const loc = useLocation()
const params = useParams()
const slug = createMemo(() => params.slug)
if (slug.startsWith('@')) {
2024-07-18 09:22:28 +00:00
console.debug('[routes] [slug]/[...tab] starts with @, render as author page')
2024-07-13 10:32:27 +00:00
const patchedProps = {
...props,
params: {
...props.params,
slug: slug.slice(1)
2024-07-13 10:32:27 +00:00
}
2024-07-22 14:40:26 +00:00
} as RouteSectionProps<AuthorPageProps>
return <AuthorPage {...patchedProps} />
2024-07-13 10:32:27 +00:00
}
if (slug.startsWith('!')) {
2024-07-18 09:22:28 +00:00
console.debug('[routes] [slug]/[...tab] starts with !, render as topic page')
2024-07-13 10:32:27 +00:00
const patchedProps = {
...props,
params: {
...props.params,
slug: slug.slice(1)
2024-07-13 10:32:27 +00:00
}
} as RouteSectionProps<TopicPageProps>
return <TopicPage {...patchedProps} />
}
// Pass slug as a prop to ArticlePageComponent
return <ArticlePageComponent slug={slug()} {...props} />
}
function ArticlePageComponent(props: RouteSectionProps<SlugPageProps> & { slug: string }) {
const loc = useLocation()
const { t } = useLocalize()
const { slug } = props
// Define the fetcher function
const fetchArticle = async (slug: string): Promise<Shout | undefined> => {
return await fetchShout(slug)
}
// Create a resource that fetches the article based on slug
const [article, { refetch, mutate }] = createResource(slug, fetchArticle)
// Handle Google Analytics
createEffect(() => {
const currentArticle = article()
if (gaIdentity && currentArticle?.id) {
loadGAScript(gaIdentity)
.then(() => initGA(gaIdentity))
.catch((error) => {
2024-07-18 09:22:28 +00:00
console.warn('[routes] [slug]/[...tab] Failed to connect Google Analytics:', error)
})
}
})
createEffect(() => {
const currentArticle = article()
if (currentArticle?.id) {
window?.gtag?.('event', 'page_view', {
page_title: currentArticle.title,
page_location: window?.location.href || '',
page_path: loc.pathname
})
}
})
return (
<ErrorBoundary fallback={() => <HttpStatusCode code={500} />}>
<Suspense fallback={<Loading />}>
<Show
when={article()}
fallback={
<PageLayout isHeaderFixed={false} hideFooter={true} title={t('Nothing is here')}>
<FourOuFourView />
<HttpStatusCode code={404} />
2024-07-15 23:11:01 +00:00
</PageLayout>
}
>
<PageLayout
title={`${t('Discours')}${article()?.title ? ' :: ' : ''}${article()?.title || ''}`}
desc={descFromBody(article()?.body || '')}
keywords={keywordsFromTopics(article()?.topics as { title: string }[])}
headerTitle={article()?.title || ''}
slug={article()?.slug}
cover={article()?.cover || ''}
>
<ReactionsProvider>
<FullArticle article={article() as Shout} />
</ReactionsProvider>
</PageLayout>
</Show>
</Suspense>
</ErrorBoundary>
)
2024-07-13 10:32:27 +00:00
}