import { useTableContentObserver } from "@/hooks/useTableContentObserver"; import Link from "next/link"; import { useEffect, useState } from "react"; type Heading = { id: string; text: string | null; level: number; }; export const SideNavigation = ({ pathname }) => { const [headings, setHeadings] = useState([]); const [selectedId, setSelectedId] = useState(null); useTableContentObserver(setSelectedId, pathname); useEffect(() => { const getHeadings = () => { // Select all heading elements (h2, h3, h4) with an 'id' attribute const headingElements = document.querySelectorAll("h2[id], h3[id], h4[id]"); // Convert the NodeList of heading elements into an array and map them to an array of 'Heading' objects const headings: Heading[] = Array.from(headingElements).map((heading) => ({ id: heading.id, text: heading.textContent, level: parseInt(heading.tagName.slice(1)), })); // Check if there are any h2 headings in the list const hasH2 = headings.some((heading) => heading.level === 2); // Update the 'headings' state with the retrieved headings, but only if there are h2 headings setHeadings(hasH2 ? headings : []); }; getHeadings(); }, [pathname]); const renderHeading = (items: Heading[], currentLevel: number) => ( ); if (headings.length) { return ( ); } return null; };