From 55a1dd81d359bd4dd8478ba454b04c7e2b2dc46d Mon Sep 17 00:00:00 2001 From: Bohdan Zavhorodskyi Date: Sat, 2 Sep 2023 19:33:51 +0300 Subject: [PATCH] feat: updated TOC component --- .../RightSidebar/TableOfContents.tsx | 25 ++++++++++++++++--- src/components/atlas/BuildTabs.tsx | 11 ++++---- src/store/tabStore.ts | 11 +++++++- 3 files changed, 37 insertions(+), 10 deletions(-) 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
    - {headings + {headingsLocal .filter(({ depth }) => depth > 1 && depth < 4) .map((heading) => (
  • { - if ( - currentTabs.isTabsHaveSync && - tabs.currentSync - ) { + if (currentTabs.isTabsHaveSync && tabs.currentSync) { const activeTabId = currentTabs.items.find( (tab) => tab.sync === tabs.currentSync )?.id; @@ -67,6 +64,10 @@ const BuildTabs: FC<{ } }, [tabs.currentSync]); + useEffect(() => { + updateTabItems(items); + }, []); + return ( <> ({ + items: [], currentSync: "", }); export const changeSyncValue = (sync: string) => { - $tabs.set({ currentSync: sync }); + $tabs.set({ ...$tabs.get(), currentSync: sync }); +}; + +export const updateTabItems = (newItems: TabItem[]) => { + const tabsData = $tabs.get(); + $tabs.set({ ...tabsData, items: [...tabsData.items, ...newItems] }); };