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,104 +44,137 @@ 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) => { const patchedProps = {
console.debug('Fetching article with slug (useParams):', slug) ...props,
const result = await fetchShout(slug) params: {
console.debug('Fetched article data (useParams):', result) ...props.params,
setData(result) slug
}
} as RouteSectionProps<TopicPageProps>
return <TopicPage {...patchedProps} />
} }
onMount(() => { // ArticlePage logic for rendering articles if neither `@` nor `!` is in the slug
console.debug('onMount triggered') function ArticlePage(props: RouteSectionProps<ArticlePageProps>) {
fetchData(params.slug) const loc = useLocation()
}) const { t } = useLocalize()
const data = createAsync(async () => props.data?.article || (await fetchShout(props.params.slug)))
createEffect(() => { onMount(async () => {
console.debug('Slug changed (useParams):', params.slug) if (gaIdentity && data()?.id) {
fetchData(params.slug) try {
}) await loadGAScript(gaIdentity)
initGA(gaIdentity)
createEffect(() => { } catch (error) {
const article = data() console.warn('[routes] [slug]/[...tab] Failed to connect Google Analytics:', error)
if (!article?.id) return }
console.debug('Page view event for article:', article) }
window?.gtag?.('event', 'page_view', {
page_title: article.title,
page_location: window?.location.href || '',
page_path: loc.pathname
}) })
})
return ( createEffect(
<ErrorBoundary fallback={() => { on(
console.error('Rendering 500 error page') data,
return <HttpStatusCode code={500} /> (a?: Shout) => {
}}> if (!a?.id) return
<Suspense fallback={<Loading />}> window?.gtag?.('event', 'page_view', {
<Show page_title: a.title,
when={data()?.id} page_location: window?.location.href || '',
fallback={ page_path: loc.pathname
<PageLayout isHeaderFixed={false} hideFooter={true} title={t('Nothing is here')}> })
{console.warn('Rendering 404 error page - no article data found')} },
<FourOuFourView /> { defer: true }
<HttpStatusCode code={404} /> )
</PageLayout> )
}
> return (
{console.debug('Rendering article page with data:', data())} <ErrorBoundary fallback={() => <HttpStatusCode code={500} />}>
<PageLayout <Suspense fallback={<Loading />}>
title={`${t('Discours')}${data()?.title ? ' :: ' : ''}${data()?.title || ''}`} <Show
desc={descFromBody(data()?.body || '')} when={data()?.id}
keywords={keywordsFromTopics(data()?.topics as { title: string }[])} fallback={
headerTitle={data()?.title || ''} <PageLayout isHeaderFixed={false} hideFooter={true} title={t('Nothing is here')}>
slug={data()?.slug} <FourOuFourView />
cover={data()?.cover || ''} <HttpStatusCode code={404} />
</PageLayout>
}
> >
<ReactionsProvider> <PageLayout
<FullArticle article={data() as Shout} /> title={`${t('Discours')}${data()?.title ? ' :: ' : ''}${data()?.title || ''}`}
</ReactionsProvider> desc={descFromBody(data()?.body || '')}
</PageLayout> keywords={keywordsFromTopics(data()?.topics as { title: string }[])}
</Show> headerTitle={data()?.title || ''}
</Suspense> slug={data()?.slug}
</ErrorBoundary> cover={data()?.cover || ''}
) >
<ReactionsProvider>
<FullArticle article={data() as Shout} />
</ReactionsProvider>
</PageLayout>
</Show>
</Suspense>
</ErrorBoundary>
)
}
return <ArticlePage {...props} />
} }