"use client"; import React, { useEffect, useState } from "react"; interface IHeading { id: string; tagName: string; textContent: string; } interface ITableOfContents { id: string; } function TableOfContents({ id }: ITableOfContents) { const [headings, setHeadings] = useState([]); useEffect(() => { const targetElement = document.getElementById(id); if (!targetElement) return; const headingElements = targetElement.querySelectorAll( "h1, h2, h3, h4, h5, h6" ); const foundHeadings = Array.from(headingElements).map((heading) => { return { id: heading.id, tagName: heading.tagName, textContent: heading.textContent ?? "", }; }); setHeadings(foundHeadings); }, [id]); return ( ); } export default TableOfContents;