diff --git a/src/components/RightSidebar/TableOfContents.tsx b/src/components/RightSidebar/TableOfContents.tsx index 94916dd8c..2ca5c5988 100644 --- a/src/components/RightSidebar/TableOfContents.tsx +++ b/src/components/RightSidebar/TableOfContents.tsx @@ -1,16 +1,21 @@ +/** @jsxImportSource react */ import type { MarkdownHeading } from "astro"; -import type { FunctionalComponent } from "preact"; +import type { FC } from "react"; import { unescape } from "html-escaper"; -import { useState, useEffect, useRef } from "preact/hooks"; +import { useState, useEffect, useRef } from "react"; +import { useStore } from "@nanostores/react"; +import { $tabs } from "@store/tabStore"; type ItemOffsets = { id: string; topOffset: number; }; -const TableOfContents: FunctionalComponent<{ headings: MarkdownHeading[] }> = ({ +const TableOfContents: FC<{ headings: MarkdownHeading[] }> = ({ headings = [], }) => { + const [headingsLocal, setHeadingsLocal] = useState(headings); + const tabs = useStore($tabs); const toc = useRef(null); const onThisPageID = "on-this-page-heading"; const itemOffsets = useRef([]); @@ -67,6 +72,18 @@ const TableOfContents: FunctionalComponent<{ headings: MarkdownHeading[] }> = ({ return () => headingsObserver.disconnect(); }, [toc.current]); + useEffect(() => { + if (tabs.items.length) { + // logic for removing tabs labels from TOC + const filteredHeadings = headings.filter( + (heading) => + heading.depth === 3 && + !tabs.items.find((tab) => tab.label === heading.text) + ); + setHeadingsLocal(filteredHeadings); + } + }, [tabs]); + const onLinkClick = (e) => { setCurrentID(e.target.getAttribute("href").replace("#", "")); }; @@ -77,7 +94,7 @@ const TableOfContents: FunctionalComponent<{ headings: MarkdownHeading[] }> = ({ On this page