From ef7ceb5d33b08c99b9ee627fc463489fcfd7d82a Mon Sep 17 00:00:00 2001 From: dufok Date: Tue, 24 Sep 2024 20:51:32 -0300 Subject: [PATCH] debug: backed slug to dev code just rename ArticlePage inner function --- src/routes/[slug]/[...tab].tsx | 105 ++++++++++++++------------------- 1 file changed, 44 insertions(+), 61 deletions(-) diff --git a/src/routes/[slug]/[...tab].tsx b/src/routes/[slug]/[...tab].tsx index a0d63feb..88cde259 100644 --- a/src/routes/[slug]/[...tab].tsx +++ b/src/routes/[slug]/[...tab].tsx @@ -14,19 +14,14 @@ * ## Components * * - **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]`). * - **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, useLocation, useParams } from '@solidjs/router' +import { RouteDefinition, RouteSectionProps, createAsync, useLocation } from '@solidjs/router' 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 { Loading } from '~/components/_shared/Loading' import { gaIdentity } from '~/config' @@ -42,7 +37,7 @@ import AuthorPage, { AuthorPageProps } from '../author/[slug]/[...tab]' import TopicPage, { TopicPageProps } from '../topic/[slug]/[...tab]' const fetchShout = async (slug: string): Promise => { - if (slug.startsWith('@') || slug.startsWith('!')) return + if (slug.startsWith('@')) return const shoutLoader = getShout({ slug }) const result = await shoutLoader() return result @@ -69,82 +64,70 @@ export type SlugPageProps = { topics: Topic[] } -export default function SlugPage(props: RouteSectionProps) { - const { t } = useLocalize() - const loc = useLocation() - - const params = useParams() - const slug = createMemo(() => params.slug) - - if (slug.startsWith('@')) { +export default function ArticlePage(props: RouteSectionProps) { + if (props.params.slug.startsWith('@')) { console.debug('[routes] [slug]/[...tab] starts with @, render as author page') const patchedProps = { ...props, params: { ...props.params, - slug: slug.slice(1) + slug: props.params.slug.slice(1, props.params.slug.length) } } as RouteSectionProps return } - if (slug.startsWith('!')) { + if (props.params.slug.startsWith('!')) { console.debug('[routes] [slug]/[...tab] starts with !, render as topic page') const patchedProps = { ...props, params: { ...props.params, - slug: slug.slice(1) + slug: props.params.slug.slice(1, props.params.slug.length) } } as RouteSectionProps return } - // Pass slug as a prop to ArticlePageComponent - return + return } -function ArticlePageComponent(props: RouteSectionProps & { slug: string }) { +function InnerArticlePage(props: RouteSectionProps) { const loc = useLocation() const { t } = useLocalize() - const { slug } = props + const data = createAsync(async () => props.data?.article || (await fetchShout(props.params.slug))) - // Define the fetcher function - const fetchArticle = async (slug: string): Promise => { - return await fetchShout(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) + } + } + }) - // Create a resource that fetches the article based on slug - const [article, { refetch, mutate }] = createResource(slug, fetchArticle) - - // Handle Google Analytics - createEffect(() => { - const currentArticle = article() - if (gaIdentity && currentArticle?.id) { - loadGAScript(gaIdentity) - .then(() => initGA(gaIdentity)) - .catch((error) => { - console.warn('[routes] [slug]/[...tab] Failed to connect Google Analytics:', error) + 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 }) - } - }) - - 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 - }) - } - }) + }, + { defer: true } + ) + ) return ( }> }> @@ -153,15 +136,15 @@ function ArticlePageComponent(props: RouteSectionProps & { slug: } > - +