import { useState, useEffect } from "react"; import "@styles/SectionScroll.scss"; export default function SectionScroll({ sections }: { sections: string[] }) { const [currentSectionIndex, setCurrentSectionIndex] = useState(0); useEffect(() => { const hash = window.location.hash.substring(1); const index = sections.indexOf(hash); if (index !== -1) { setCurrentSectionIndex(index); } }, []); window.onhashchange = () => { const hash = window.location.hash.substring(1); const index = sections.indexOf(hash); if (index !== -1) { setCurrentSectionIndex(index); } }; function scrollTo(direction: number) { let newIndex = currentSectionIndex + direction; if (newIndex < 0) newIndex = sections.length - 1; if (newIndex >= sections.length) newIndex = 0; window.location.hash = sections[newIndex]; setCurrentSectionIndex(newIndex); } return (