fix(SW-441): fixes after PR
This commit is contained in:
@@ -3,22 +3,26 @@
|
|||||||
import { useParams } from "next/navigation"
|
import { useParams } from "next/navigation"
|
||||||
import { useEffect, useState } from "react"
|
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() {
|
export default function useHash() {
|
||||||
const [isClient, setIsClient] = useState(false)
|
const [hash, setHash] = useState<string | undefined>(undefined)
|
||||||
const [hash, setHash] = useState(getHash())
|
|
||||||
const params = useParams()
|
const params = useParams()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setIsClient(true)
|
const updateHash = () => {
|
||||||
setHash(getHash())
|
const newHash = window.location.hash
|
||||||
}, [params, setHash, setIsClient])
|
? 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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,12 +46,9 @@ export default function useScrollSpy(
|
|||||||
const observer = new IntersectionObserver(handleIntersection, mergedOptions)
|
const observer = new IntersectionObserver(handleIntersection, mergedOptions)
|
||||||
const elements = sectionIds
|
const elements = sectionIds
|
||||||
.map((id) => document.getElementById(id))
|
.map((id) => document.getElementById(id))
|
||||||
.filter(Boolean)
|
.filter((el): el is HTMLElement => !!el)
|
||||||
|
|
||||||
elements.forEach((element) => {
|
elements.forEach((element) => {
|
||||||
if (!element) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
observer.observe(element)
|
observer.observe(element)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user