From 12537a424cee11e83f8ea6f17f7717364a89e41e Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 16 Jun 2023 00:41:59 +0100 Subject: [PATCH] feat: server side rendering for page content --- src/components/common/PageContent.tsx | 57 +++++++------------ .../common/PageContentContainer.tsx | 48 ++++++++++++++++ src/components/site/PostToc.tsx | 2 + src/components/ui/Mention.tsx | 2 + 4 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 src/components/common/PageContentContainer.tsx diff --git a/src/components/common/PageContent.tsx b/src/components/common/PageContent.tsx index 24e9b779..3deffc5d 100644 --- a/src/components/common/PageContent.tsx +++ b/src/components/common/PageContent.tsx @@ -1,15 +1,12 @@ -"use client" - -import { MutableRefObject, useEffect, useMemo } from "react" +import { type MutableRefObject } from "react" import { PostActions } from "~/components/site/PostActions" import { PostToc } from "~/components/site/PostToc" -import { useCodeCopy } from "~/hooks/useCodeCopy" -import { useHash } from "~/hooks/useHash" import { ExpandedCharacter, ExpandedNote } from "~/lib/types" -import { cn, scrollTo } from "~/lib/utils" +import { cn } from "~/lib/utils" import { renderPageContent } from "~/markdown" -import { useReportStats } from "~/queries/page" + +import { PageContentContainer } from "./PageContentContainer" export const PageContent = ({ className, @@ -36,40 +33,26 @@ export const PageContent = ({ site?: ExpandedCharacter withActions?: boolean }) => { - useCodeCopy() - - const inParsedContent = useMemo(() => { - if (parsedContent) { - return parsedContent - } else if (content) { - const result = renderPageContent(content, false, isComment) - return result - } else { - return null - } - }, [content, isComment, parsedContent]) - - const hash = useHash() - useEffect(() => { - scrollTo(hash) - }, [hash]) - - useReportStats({ - characterId: page?.characterId, - noteId: page?.noteId, - }) + let inParsedContent + if (parsedContent) { + inParsedContent = parsedContent + } else if (content) { + inParsedContent = renderPageContent(content, false, isComment) + } return ( -
onScroll?.((e.target as any)?.scrollTop)} + element={inParsedContent?.element} > -
- {inParsedContent?.element} -
- {toc && inParsedContent?.toc && } - {withActions && } -
+ <> + {toc && inParsedContent?.toc && } + {withActions && } + + ) } diff --git a/src/components/common/PageContentContainer.tsx b/src/components/common/PageContentContainer.tsx new file mode 100644 index 00000000..9fa95e65 --- /dev/null +++ b/src/components/common/PageContentContainer.tsx @@ -0,0 +1,48 @@ +"use client" + +import { type MutableRefObject, useEffect } from "react" + +import { useCodeCopy } from "~/hooks/useCodeCopy" +import { useHash } from "~/hooks/useHash" +import { ExpandedNote } from "~/lib/types" +import { cn, scrollTo } from "~/lib/utils" +import { useReportStats } from "~/queries/page" + +export const PageContentContainer = ({ + className, + page, + inputRef, + onScroll, + onMouseEnter, + children, + element, +}: { + className?: string + page?: ExpandedNote + inputRef?: MutableRefObject + onScroll?: (scrollTop: number) => void + onMouseEnter?: () => void + children?: JSX.Element + element?: JSX.Element +}) => { + useCodeCopy() + + const hash = useHash() + useEffect(() => { + scrollTo(hash) + }, [hash]) + + useReportStats({ + characterId: page?.characterId, + noteId: page?.noteId, + }) + + return ( +
+
+ {element} +
+ {children} +
+ ) +} diff --git a/src/components/site/PostToc.tsx b/src/components/site/PostToc.tsx index e1d23e01..2945236a 100644 --- a/src/components/site/PostToc.tsx +++ b/src/components/site/PostToc.tsx @@ -1,3 +1,5 @@ +"use client" + import { toHtml } from "hast-util-to-html" import DOMPurify from "isomorphic-dompurify" import katex from "katex" diff --git a/src/components/ui/Mention.tsx b/src/components/ui/Mention.tsx index 8f1d120f..9c4ba9ed 100644 --- a/src/components/ui/Mention.tsx +++ b/src/components/ui/Mention.tsx @@ -1,3 +1,5 @@ +"use client" + import React, { memo } from "react" import { getSiteLink } from "~/lib/helpers"