From 8ac2f595392acf444654ce3cc1e3d135c61a8132 Mon Sep 17 00:00:00 2001 From: rebeccahongsf Date: Tue, 8 Oct 2024 10:13:05 -0700 Subject: [PATCH] SUL23-638 | configure related links; memo-ize headings; add link styles --- app/[...slug]/page.tsx | 2 +- src/components/patterns/on-the-page.tsx | 92 +++++++++++++++---------- src/lib/format-html.tsx | 3 +- 3 files changed, 59 insertions(+), 38 deletions(-) diff --git a/app/[...slug]/page.tsx b/app/[...slug]/page.tsx index a4124906..44d33eae 100644 --- a/app/[...slug]/page.tsx +++ b/app/[...slug]/page.tsx @@ -73,7 +73,7 @@ const NodePage = async ({params, previewMode}: PageProps) => { - {entity.__typename === "NodeStanfordPage" && } + {entity.__typename === "NodeStanfordPage" && } )} diff --git a/src/components/patterns/on-the-page.tsx b/src/components/patterns/on-the-page.tsx index 4921fb47..23bcee78 100644 --- a/src/components/patterns/on-the-page.tsx +++ b/src/components/patterns/on-the-page.tsx @@ -1,8 +1,9 @@ "use client" -import React, {useState, useEffect} from "react" +import React, {useState, useEffect, useMemo} from "react" import parse from "html-react-parser" import {NodeStanfordPage} from "@/lib/gql/__generated__/drupal.d" +import {twMerge} from "tailwind-merge" interface Heading { text: string @@ -11,28 +12,31 @@ interface Heading { const OnThePageLink = ({node}: {node: NodeStanfordPage}) => { const [activeHeading, setActiveHeading] = useState("") - const headings: Heading[] = [] + const relLinkHeading = node.sulRelLinksHeading + const relLinks = node.sulRelLinks - const content = - node.suPageComponents && - node.suPageComponents - .filter(item => item.__typename === "ParagraphStanfordWysiwyg") - .map(item => item.suWysiwygText.processed) - .join(" ") + const headings = useMemo(() => { + const content = + node?.suPageComponents && + node?.suPageComponents + .filter(item => item.__typename === "ParagraphStanfordWysiwyg") + .map(item => item.suWysiwygText.processed) + .join(" ") - console.log("CONTENT", content) + const parsedContent = parse(content) + const headingsArr: Heading[] = [] - const parsedContent = parse(content) + React.Children.forEach(parsedContent, child => { + if (React.isValidElement(child) && child.type === "h2") { + const text = child.props.children + const id = text.toLowerCase().replace(/\s+/g, "-") + headingsArr.push({text, id}) + } + }) - React.Children.forEach(parsedContent, child => { - if (React.isValidElement(child) && child.type === "h2") { - const text = child.props.children - const id = text.toLowerCase().replace(/\s+/g, "-") - headings.push({text, id}) - } - }) + return headingsArr + }, [node]) - // Set up Intersection Observer useEffect(() => { const options: IntersectionObserverInit = { root: null, @@ -61,24 +65,40 @@ const OnThePageLink = ({node}: {node: NodeStanfordPage}) => { }, [headings]) return ( - +
+ + {relLinks && ( +
+

{relLinkHeading || "Related content"}

+ +
+ )} +
) } diff --git a/src/lib/format-html.tsx b/src/lib/format-html.tsx index 91fe42ab..1491dea0 100644 --- a/src/lib/format-html.tsx +++ b/src/lib/format-html.tsx @@ -96,10 +96,11 @@ const options: HTMLReactParserOptions = { return {domToReact(domNode.children as DOMNode[], options)} case "h2": + console.log("DATA:", domNode.children[0]) const text = domNode.children[0].data const id = text.toLowerCase().replace(/\s+/g, "-") return ( - + {domToReact(domNode.children as DOMNode[], options)} )