fix(SW-441): fixes after PR

This commit is contained in:
Erik Tiekstra
2024-10-08 15:43:24 +02:00
parent d63e8856ec
commit 4212f5c97b
2 changed files with 18 additions and 17 deletions

View File

@@ -3,22 +3,26 @@
import { useParams } from "next/navigation"
import { useEffect, useState } from "react"
function getHash() {
if (typeof window === "undefined" || !window.location.hash) {
return undefined
}
return window.location.hash.split("#")[1]
}
export default function useHash() {
const [isClient, setIsClient] = useState(false)
const [hash, setHash] = useState(getHash())
const [hash, setHash] = useState<string | undefined>(undefined)
const params = useParams()
useEffect(() => {
setIsClient(true)
setHash(getHash())
}, [params, setHash, setIsClient])
const updateHash = () => {
const newHash = window.location.hash
? window.location.hash.slice(1)
: undefined
setHash(newHash)
}
return isClient ? hash : null
updateHash()
window.addEventListener("hashchange", updateHash)
return () => {
window.removeEventListener("hashchange", updateHash)
}
}, [params])
return hash
}

View File

@@ -46,12 +46,9 @@ export default function useScrollSpy(
const observer = new IntersectionObserver(handleIntersection, mergedOptions)
const elements = sectionIds
.map((id) => document.getElementById(id))
.filter(Boolean)
.filter((el): el is HTMLElement => !!el)
elements.forEach((element) => {
if (!element) {
return
}
observer.observe(element)
})