2024-07-13 09:36:23 +00:00
|
|
|
import { RouteDefinition, RouteSectionProps, createAsync, useLocation } from '@solidjs/router'
|
2024-07-13 09:06:49 +00:00
|
|
|
import { HttpStatusCode } from '@solidjs/start'
|
2024-09-06 04:52:39 +00:00
|
|
|
import { ErrorBoundary, Show, Suspense, createEffect, on, onMount } 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> => {
|
2024-07-30 19:06:17 +00:00
|
|
|
if (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
|
|
|
|
2024-09-06 04:52:39 +00:00
|
|
|
export default function ArticlePage(props: RouteSectionProps<SlugPageProps>) {
|
2024-07-13 10:32:27 +00:00
|
|
|
if (props.params.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: props.params.slug.slice(1, props.params.slug.length)
|
|
|
|
}
|
2024-07-22 14:40:26 +00:00
|
|
|
} as RouteSectionProps<AuthorPageProps>
|
|
|
|
return <AuthorPage {...patchedProps} />
|
2024-07-13 10:32:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (props.params.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: props.params.slug.slice(1, props.params.slug.length)
|
|
|
|
}
|
|
|
|
} as RouteSectionProps<TopicPageProps>
|
|
|
|
return <TopicPage {...patchedProps} />
|
|
|
|
}
|
|
|
|
|
2024-07-15 23:11:01 +00:00
|
|
|
function ArticlePage(props: RouteSectionProps<ArticlePageProps>) {
|
|
|
|
const loc = useLocation()
|
|
|
|
const { t } = useLocalize()
|
|
|
|
const data = createAsync(async () => props.data?.article || (await fetchShout(props.params.slug)))
|
|
|
|
|
|
|
|
onMount(async () => {
|
|
|
|
if (gaIdentity && data()?.id) {
|
|
|
|
try {
|
|
|
|
await loadGAScript(gaIdentity)
|
|
|
|
initGA(gaIdentity)
|
|
|
|
} catch (error) {
|
2024-07-18 09:22:28 +00:00
|
|
|
console.warn('[routes] [slug]/[...tab] Failed to connect Google Analytics:', error)
|
2024-07-15 23:11:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
createEffect(
|
|
|
|
on(
|
|
|
|
data,
|
|
|
|
(a?: Shout) => {
|
|
|
|
if (!a?.id) return
|
|
|
|
window?.gtag?.('event', 'page_view', {
|
|
|
|
page_title: a.title,
|
|
|
|
page_location: window?.location.href || '',
|
|
|
|
page_path: loc.pathname
|
|
|
|
})
|
|
|
|
},
|
|
|
|
{ defer: true }
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ErrorBoundary fallback={() => <HttpStatusCode code={500} />}>
|
|
|
|
<Suspense fallback={<Loading />}>
|
|
|
|
<Show
|
|
|
|
when={data()?.id}
|
|
|
|
fallback={
|
|
|
|
<PageLayout isHeaderFixed={false} hideFooter={true} title={t('Nothing is here')}>
|
|
|
|
<FourOuFourView />
|
|
|
|
<HttpStatusCode code={404} />
|
|
|
|
</PageLayout>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<PageLayout
|
|
|
|
title={`${t('Discours')}${data()?.title ? ' :: ' : ''}${data()?.title || ''}`}
|
|
|
|
desc={descFromBody(data()?.body || '')}
|
|
|
|
keywords={keywordsFromTopics(data()?.topics as { title: string }[])}
|
|
|
|
headerTitle={data()?.title || ''}
|
|
|
|
slug={data()?.slug}
|
|
|
|
cover={data()?.cover || ''}
|
|
|
|
>
|
|
|
|
<ReactionsProvider>
|
2024-09-06 04:52:39 +00:00
|
|
|
<FullArticle article={data() as Shout} />
|
2024-07-15 23:11:01 +00:00
|
|
|
</ReactionsProvider>
|
|
|
|
</PageLayout>
|
|
|
|
</Show>
|
|
|
|
</Suspense>
|
|
|
|
</ErrorBoundary>
|
|
|
|
)
|
|
|
|
}
|
2024-07-13 10:32:27 +00:00
|
|
|
return <ArticlePage {...props} />
|
|
|
|
}
|