import { siteConfig } from '@/config/siteConfig'; import { NextSeo } from 'next-seo'; import { useTheme } from 'next-themes'; import Link from 'next/link'; import { useCallback, useEffect, useState } from 'react'; import Nav from './Nav'; import { Hero } from './Hero'; import { Navigation } from './Navigation'; function useTableOfContents(tableOfContents) { const [currentSection, setCurrentSection] = useState(tableOfContents[0]?.id); const getHeadings = useCallback((toc) => { return toc .flatMap((node) => [node.id, ...node.children.map((child) => child.id)]) .map((id) => { const el = document.getElementById(id); if (!el) return null; const style = window.getComputedStyle(el); const scrollMt = parseFloat(style.scrollMarginTop); const top = window.scrollY + el.getBoundingClientRect().top - scrollMt; return { id, top }; }) .filter((el) => !!el); }, []); console.log(getHeadings,"getheadings"); useEffect(() => { if (tableOfContents.length === 0) return; const headings = getHeadings(tableOfContents); function onScroll() { const top = window.scrollY + 4.5; let current = headings[0].id; headings.forEach((heading) => { if (top >= heading.top) { current = heading.id; } return current; }); setCurrentSection(current); } window.addEventListener('scroll', onScroll, { passive: true }); onScroll(); return () => { window.removeEventListener('scroll', onScroll); }; }, [getHeadings, tableOfContents]); return currentSection; } export default function Layout({ children, title, description, tableOfContents = [], isHomePage = false, sidebarTree = [], }: { children; title?: string; description?: string; tableOfContents?; urlPath?: string; sidebarTree?: []; isHomePage?: boolean; }) { console.log(children) // const { toc } = children.props; const { theme, setTheme } = useTheme(); const currentSection = useTableOfContents(tableOfContents); function isActive(section) { if (section.id === currentSection) { return true; } if (!section.children) { return false; } return section.children.findIndex(isActive) > -1; } return ( <> {title && }