debug: backed slug to dev code just rename ArticlePage inner function

This commit is contained in:
Stepan Vladovskiy 2024-09-24 20:51:32 -03:00
parent 343b71defd
commit ef7ceb5d33

View File

@ -14,19 +14,14 @@
* ## Components * ## Components
* *
* - **SlugPage**: The main component that determines which page to render based on the `slug`. * - **SlugPage**: The main component that determines which page to render based on the `slug`.
* - **ArticlePageComponent**: Fetches and displays the detailed view of an article. * - **InnerArticlePage**: Fetches and displays the detailed view of an article.
* - **AuthorPage**: Displays author-specific information (imported from `../author/[slug]/[...tab]`). * - **AuthorPage**: Displays author-specific information (imported from `../author/[slug]/[...tab]`).
* - **TopicPage**: Displays topic-specific information (imported from `../topic/[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, createAsync, useLocation } from '@solidjs/router'
import { RouteDefinition, RouteSectionProps, useLocation, useParams } from '@solidjs/router'
import { HttpStatusCode } from '@solidjs/start' import { HttpStatusCode } from '@solidjs/start'
import { ErrorBoundary, Show, Suspense, createEffect, on, onMount, createResource } 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'
@ -42,7 +37,7 @@ import AuthorPage, { AuthorPageProps } from '../author/[slug]/[...tab]'
import TopicPage, { TopicPageProps } from '../topic/[slug]/[...tab]' import TopicPage, { TopicPageProps } from '../topic/[slug]/[...tab]'
const fetchShout = async (slug: string): Promise<Shout | undefined> => { const fetchShout = async (slug: string): Promise<Shout | undefined> => {
if (slug.startsWith('@') || slug.startsWith('!')) return if (slug.startsWith('@')) return
const shoutLoader = getShout({ slug }) const shoutLoader = getShout({ slug })
const result = await shoutLoader() const result = await shoutLoader()
return result return result
@ -69,82 +64,70 @@ export type SlugPageProps = {
topics: Topic[] topics: Topic[]
} }
export default function SlugPage(props: RouteSectionProps<SlugPageProps>) { export default function ArticlePage(props: RouteSectionProps<SlugPageProps>) {
const { t } = useLocalize() if (props.params.slug.startsWith('@')) {
const loc = useLocation()
const params = useParams()
const slug = createMemo(() => params.slug)
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: slug.slice(1) slug: props.params.slug.slice(1, props.params.slug.length)
} }
} as RouteSectionProps<AuthorPageProps> } as RouteSectionProps<AuthorPageProps>
return <AuthorPage {...patchedProps} /> return <AuthorPage {...patchedProps} />
} }
if (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 = {
...props, ...props,
params: { params: {
...props.params, ...props.params,
slug: slug.slice(1) slug: props.params.slug.slice(1, props.params.slug.length)
} }
} as RouteSectionProps<TopicPageProps> } as RouteSectionProps<TopicPageProps>
return <TopicPage {...patchedProps} /> return <TopicPage {...patchedProps} />
} }
// Pass slug as a prop to ArticlePageComponent return <InnerArticlePage {...props} />
return <ArticlePageComponent slug={slug()} {...props} />
} }
function ArticlePageComponent(props: RouteSectionProps<SlugPageProps> & { slug: string }) { function InnerArticlePage(props: RouteSectionProps<ArticlePageProps>) {
const loc = useLocation() const loc = useLocation()
const { t } = useLocalize() const { t } = useLocalize()
const { slug } = props const data = createAsync(async () => props.data?.article || (await fetchShout(props.params.slug)))
// Define the fetcher function onMount(async () => {
const fetchArticle = async (slug: string): Promise<Shout | undefined> => { if (gaIdentity && data()?.id) {
return await fetchShout(slug) try {
} await loadGAScript(gaIdentity)
initGA(gaIdentity)
} catch (error) {
console.warn('[routes] [slug]/[...tab] Failed to connect Google Analytics:', error)
}
}
})
// Create a resource that fetches the article based on slug createEffect(
const [article, { refetch, mutate }] = createResource(slug, fetchArticle) on(
data,
// Handle Google Analytics (a?: Shout) => {
createEffect(() => { if (!a?.id) return
const currentArticle = article() window?.gtag?.('event', 'page_view', {
if (gaIdentity && currentArticle?.id) { page_title: a.title,
loadGAScript(gaIdentity) page_location: window?.location.href || '',
.then(() => initGA(gaIdentity)) page_path: loc.pathname
.catch((error) => {
console.warn('[routes] [slug]/[...tab] Failed to connect Google Analytics:', error)
}) })
} },
}) { defer: true }
)
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 ( return (
<ErrorBoundary fallback={() => <HttpStatusCode code={500} />}> <ErrorBoundary fallback={() => <HttpStatusCode code={500} />}>
<Suspense fallback={<Loading />}> <Suspense fallback={<Loading />}>
<Show <Show
when={article()} 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')}>
<FourOuFourView /> <FourOuFourView />
@ -153,15 +136,15 @@ function ArticlePageComponent(props: RouteSectionProps<SlugPageProps> & { slug:
} }
> >
<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>