This commit is contained in:
Stepan Vladovskiy 2024-09-25 21:15:17 -03:00
parent c165742fbf
commit 29dbd67c27

View File

@ -1,6 +1,6 @@
import { RouteDefinition, RouteSectionProps, useLocation } from '@solidjs/router' import { RouteDefinition, RouteSectionProps, createAsync, useLocation } from '@solidjs/router'
import { HttpStatusCode } from '@solidjs/start' import { HttpStatusCode } from '@solidjs/start'
import { ErrorBoundary, Show, Suspense, createEffect, on, createSignal, onMount } from 'solid-js' import { ErrorBoundary, Show, Suspense, createEffect, on, onMount } from 'solid-js'
import { FourOuFourView } from '~/components/Views/FourOuFour' import { FourOuFourView } from '~/components/Views/FourOuFour'
import { Loading } from '~/components/_shared/Loading' import { Loading } from '~/components/_shared/Loading'
import { gaIdentity } from '~/config' import { gaIdentity } from '~/config'
@ -15,21 +15,17 @@ import { ReactionsProvider } from '../../context/reactions'
import AuthorPage, { AuthorPageProps } from '../author/[slug]/[...tab]' import AuthorPage, { AuthorPageProps } from '../author/[slug]/[...tab]'
import TopicPage, { TopicPageProps } from '../topic/[slug]/[...tab]' import TopicPage, { TopicPageProps } from '../topic/[slug]/[...tab]'
// Simplified fetch function for the shout
const fetchShout = async (slug: string): Promise<Shout | undefined> => { const fetchShout = async (slug: string): Promise<Shout | undefined> => {
console.debug('fetchShout called with slug:', slug) if (slug.startsWith('@')) return
if (slug.startsWith('@')) return // Skip fetching article for slugs starting with @ (author pages) const shoutLoader = getShout({ slug })
const result = await getShout({ slug }) const result = await shoutLoader()
console.debug('fetchShout result:', result)
return result return result
} }
export const route: RouteDefinition = { export const route: RouteDefinition = {
load: async ({ params }) => { load: async ({ params }) => ({
const article = await fetchShout(params.slug) article: await fetchShout(params.slug)
console.debug('route.load fetched article:', article) })
return { article }
}
} }
export type ArticlePageProps = { export type ArticlePageProps = {
@ -48,7 +44,6 @@ export type SlugPageProps = {
} }
export default function ArticlePage(props: RouteSectionProps<SlugPageProps>) { export default function ArticlePage(props: RouteSectionProps<SlugPageProps>) {
// If the slug starts with '@', render as author page
if (props.params.slug.startsWith('@')) { if (props.params.slug.startsWith('@')) {
console.debug('[routes] [slug]/[...tab] starts with @, render as author page') console.debug('[routes] [slug]/[...tab] starts with @, render as author page')
const patchedProps = { const patchedProps = {
@ -61,7 +56,6 @@ export default function ArticlePage(props: RouteSectionProps<SlugPageProps>) {
return <AuthorPage {...patchedProps} /> return <AuthorPage {...patchedProps} />
} }
// If the slug starts with '!', render as topic page
if (props.params.slug.startsWith('!')) { if (props.params.slug.startsWith('!')) {
console.debug('[routes] [slug]/[...tab] starts with !, render as topic page') console.debug('[routes] [slug]/[...tab] starts with !, render as topic page')
const patchedProps = { const patchedProps = {
@ -74,40 +68,14 @@ export default function ArticlePage(props: RouteSectionProps<SlugPageProps>) {
return <TopicPage {...patchedProps} /> return <TopicPage {...patchedProps} />
} }
// Handle regular article slugs
function ArticlePage(props: RouteSectionProps<ArticlePageProps>) { function ArticlePage(props: RouteSectionProps<ArticlePageProps>) {
const loc = useLocation() const loc = useLocation()
const { t } = useLocalize() const { t } = useLocalize()
const data = createAsync(async () => props.data?.article || (await fetchShout(props.params.slug)))
// Set up a signal for article data and loading state
const [article, setArticle] = createSignal<Shout | undefined>(props.data?.article)
const [isLoading, setIsLoading] = createSignal(false)
// Fetch article data manually based on slug changes
createEffect(() => {
const slug = props.params.slug
if (!slug) return
setIsLoading(true)
fetchShout(slug)
.then((result) => {
setArticle(result)
if (!result) {
console.warn('No article data found for slug:', slug)
}
})
.catch((error) => {
console.error('Error fetching shout:', error)
})
.finally(() => setIsLoading(false))
})
// onMount to load GA script
onMount(async () => { onMount(async () => {
console.debug('onMount triggered') if (gaIdentity && data()?.id) {
if (gaIdentity && article()?.id) {
try { try {
console.debug('Loading GA script')
await loadGAScript(gaIdentity) await loadGAScript(gaIdentity)
initGA(gaIdentity) initGA(gaIdentity)
} catch (error) { } catch (error) {
@ -116,10 +84,9 @@ export default function ArticlePage(props: RouteSectionProps<SlugPageProps>) {
} }
}) })
// Google Analytics effect
createEffect( createEffect(
on( on(
article, data,
(a?: Shout) => { (a?: Shout) => {
if (!a?.id) return if (!a?.id) return
window?.gtag?.('event', 'page_view', { window?.gtag?.('event', 'page_view', {
@ -136,25 +103,24 @@ export default function ArticlePage(props: RouteSectionProps<SlugPageProps>) {
<ErrorBoundary fallback={() => <HttpStatusCode code={500} />}> <ErrorBoundary fallback={() => <HttpStatusCode code={500} />}>
<Suspense fallback={<Loading />}> <Suspense fallback={<Loading />}>
<Show <Show
when={!isLoading() && article()?.id} when={data()?.id}
fallback={ fallback={
<PageLayout isHeaderFixed={false} hideFooter={true} title={t('Nothing is here')}> <PageLayout isHeaderFixed={false} hideFooter={true} title={t('Nothing is here')}>
{console.warn('Rendering 404 error page - no article data found')}
<FourOuFourView /> <FourOuFourView />
<HttpStatusCode code={404} /> <HttpStatusCode code={404} />
</PageLayout> </PageLayout>
} }
> >
<PageLayout <PageLayout
title={`${t('Discours')}${article()?.title ? ' :: ' : ''}${article()?.title || ''}`} title={`${t('Discours')}${data()?.title ? ' :: ' : ''}${data()?.title || ''}`}
desc={descFromBody(article()?.body || '')} desc={descFromBody(data()?.body || '')}
keywords={keywordsFromTopics(article()?.topics as { title: string }[])} keywords={keywordsFromTopics(data()?.topics as { title: string }[])}
headerTitle={article()?.title || ''} headerTitle={data()?.title || ''}
slug={article()?.slug} slug={data()?.slug}
cover={article()?.cover || ''} cover={data()?.cover || ''}
> >
<ReactionsProvider> <ReactionsProvider>
<FullArticle article={article() as Shout} /> <FullArticle article={data() as Shout} />
</ReactionsProvider> </ReactionsProvider>
</PageLayout> </PageLayout>
</Show> </Show>
@ -162,6 +128,5 @@ export default function ArticlePage(props: RouteSectionProps<SlugPageProps>) {
</ErrorBoundary> </ErrorBoundary>
) )
} }
return <ArticlePage {...props} /> return <ArticlePage {...props} />
} }