debug: With load topics and logs in slug tab to see why it is not loading topic from user page

This commit is contained in:
Stepan Vladovskiy 2024-09-25 09:59:12 -03:00
parent 6dc25260bb
commit aa11c8d8b8

View File

@ -1,11 +1,12 @@
import { RouteDefinition, RouteSectionProps, useLocation, useParams } 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, 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'
import { useLocalize } from '~/context/localize' import { useLocalize } from '~/context/localize'
import { getShout } from '~/graphql/api/public' import { getShout } from '~/graphql/api/public'
import { useTopics } from '~/context/topics' // Import Topics context
import type { Author, Reaction, Shout, Topic } from '~/graphql/schema/core.gen' import type { Author, Reaction, Shout, Topic } from '~/graphql/schema/core.gen'
import { initGA, loadGAScript } from '~/utils/ga' import { initGA, loadGAScript } from '~/utils/ga'
import { descFromBody, keywordsFromTopics } from '~/utils/meta' import { descFromBody, keywordsFromTopics } from '~/utils/meta'
@ -43,90 +44,120 @@ export type SlugPageProps = {
topics: Topic[] topics: Topic[]
} }
export default function SlugPage(props: RouteSectionProps<SlugPageProps>) { export default function ArticlePage(props: RouteSectionProps<SlugPageProps>) {
const params = useParams() const { topicEntities, loadTopics } = useTopics() // Get topics context
const slug = props.params.slug
if (params.slug.startsWith('@')) { // Handle author page if slug starts with '@'
if (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 = {
...props, ...props,
params: { params: {
...props.params, ...props.params,
slug: params.slug.slice(1) slug: slug.slice(1)
} }
} as RouteSectionProps<AuthorPageProps> } as RouteSectionProps<AuthorPageProps>
return <AuthorPage {...patchedProps} /> return <AuthorPage {...patchedProps} />
} }
if (params.slug.startsWith('!')) { // Handle topic page if slug starts with '!'
if (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 topicSlug = slug.slice(1) // Remove '!' from slug
const topic = topicEntities()[topicSlug] // Check if the topic is already loaded
// If the topic is not loaded, fetch topics
if (!topic) {
onMount(async () => {
console.debug('Loading topics for the first time...')
await loadTopics() // Load topics if not already available
})
return <Loading /> // Show a loading state while fetching the topics
}
const patchedProps = { const patchedProps = {
...props, ...props,
params: { params: {
...props.params, ...props.params,
slug: params.slug.slice(1) slug: topicSlug
} }
} as RouteSectionProps<TopicPageProps> } as RouteSectionProps<TopicPageProps>
return <TopicPage {...patchedProps} /> return <TopicPage {...patchedProps} />
} }
return <ArticlePage {...props} /> // Handle topic pages without '!' or '@'
} if (!slug.startsWith('@') && !slug.startsWith('!')) {
console.debug('[routes] [slug]/[...tab] regular topic page')
function ArticlePage(props: RouteSectionProps<ArticlePageProps>) { const topic = topicEntities()[slug] // Check if the topic is already loaded
const loc = useLocation()
const { t } = useLocalize()
const params = useParams()
console.debug('Initial slug from useParams:', params.slug) // If the topic is not loaded, trigger topic loading
if (!topic) {
onMount(async () => {
console.debug('Topic not found, loading topics...')
await loadTopics()
})
const [data, setData] = createSignal<Shout | undefined>(undefined) return <Loading /> // Show a loading state while fetching the topics
const fetchData = async (slug: string) => {
console.debug('Fetching article with slug (useParams):', slug)
const result = await fetchShout(slug)
console.debug('Fetched article data (useParams):', result)
setData(result)
} }
onMount(() => { const patchedProps = {
console.debug('onMount triggered') ...props,
fetchData(params.slug) params: {
...props.params,
slug
}
} as RouteSectionProps<TopicPageProps>
return <TopicPage {...patchedProps} />
}
// ArticlePage logic for rendering articles if neither `@` nor `!` is in the slug
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) {
console.warn('[routes] [slug]/[...tab] Failed to connect Google Analytics:', error)
}
}
}) })
createEffect(() => { createEffect(
console.debug('Slug changed (useParams):', params.slug) on(
fetchData(params.slug) data,
}) (a?: Shout) => {
if (!a?.id) return
createEffect(() => {
const article = data()
if (!article?.id) return
console.debug('Page view event for article:', article)
window?.gtag?.('event', 'page_view', { window?.gtag?.('event', 'page_view', {
page_title: article.title, page_title: a.title,
page_location: window?.location.href || '', page_location: window?.location.href || '',
page_path: loc.pathname page_path: loc.pathname
}) })
}) },
{ defer: true }
)
)
return ( return (
<ErrorBoundary fallback={() => { <ErrorBoundary fallback={() => <HttpStatusCode code={500} />}>
console.error('Rendering 500 error page')
return <HttpStatusCode code={500} />
}}>
<Suspense fallback={<Loading />}> <Suspense fallback={<Loading />}>
<Show <Show
when={data()?.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>
} }
> >
{console.debug('Rendering article page with data:', data())}
<PageLayout <PageLayout
title={`${t('Discours')}${data()?.title ? ' :: ' : ''}${data()?.title || ''}`} title={`${t('Discours')}${data()?.title ? ' :: ' : ''}${data()?.title || ''}`}
desc={descFromBody(data()?.body || '')} desc={descFromBody(data()?.body || '')}
@ -143,4 +174,7 @@ function ArticlePage(props: RouteSectionProps<ArticlePageProps>) {
</Suspense> </Suspense>
</ErrorBoundary> </ErrorBoundary>
) )
}
return <ArticlePage {...props} />
} }