Skip to content

Commit

Permalink
feat: updated TOC component
Browse files Browse the repository at this point in the history
  • Loading branch information
bzavhorodskyi committed Sep 2, 2023
1 parent 14f06d3 commit 55a1dd8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
25 changes: 21 additions & 4 deletions src/components/RightSidebar/TableOfContents.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLUListElement>(null);
const onThisPageID = "on-this-page-heading";
const itemOffsets = useRef<ItemOffsets[]>([]);
Expand Down Expand Up @@ -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("#", ""));
};
Expand All @@ -77,7 +94,7 @@ const TableOfContents: FunctionalComponent<{ headings: MarkdownHeading[] }> = ({
On this page
</h2>
<ul ref={toc}>
{headings
{headingsLocal
.filter(({ depth }) => depth > 1 && depth < 4)
.map((heading) => (
<li
Expand Down
11 changes: 6 additions & 5 deletions src/components/atlas/BuildTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Tabs } from "@adjust/components";
import { useStore } from "@nanostores/react";

import { htmlWithTitles } from "@components/utils/htmlWithTitles";
import { $tabs, changeSyncValue } from "@store/tabStore";
import { $tabs, changeSyncValue, updateTabItems } from "@store/tabStore";

import type { TabItemType } from "@adjust/components/build/Tabs/TabItem";

Expand Down Expand Up @@ -51,10 +51,7 @@ const BuildTabs: FC<{
};

useEffect(() => {
if (
currentTabs.isTabsHaveSync &&
tabs.currentSync
) {
if (currentTabs.isTabsHaveSync && tabs.currentSync) {
const activeTabId = currentTabs.items.find(
(tab) => tab.sync === tabs.currentSync
)?.id;
Expand All @@ -67,6 +64,10 @@ const BuildTabs: FC<{
}
}, [tabs.currentSync]);

useEffect(() => {
updateTabItems(items);
}, []);

return (
<>
<Tabs
Expand Down
11 changes: 10 additions & 1 deletion src/store/tabStore.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { map } from "nanostores";

import type { TabItem } from "@components/atlas/BuildTabs";

interface TabStore {
items: TabItem[];
currentSync: string;
}

export const $tabs = map<TabStore>({
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] });
};

0 comments on commit 55a1dd8

Please sign in to comment.