fix(scroll): fix issue with scrolll positino not persisting on reload

This commit is contained in:
Triston Armstrong 2024-10-22 14:30:21 -04:00
parent be10f376f4
commit f505d002f7
Signed by: tristonarmstrong
GPG Key ID: A23B48AE45EB6EFE

View File

@ -10,10 +10,12 @@ import { Logo } from "./Logo"
import { useThemeDetector } from "../utils/useThemeDetector" import { useThemeDetector } from "../utils/useThemeDetector"
import { TextItem } from "./TextItem" import { TextItem } from "./TextItem"
import texts from "../signals/texts" import texts from "../signals/texts"
import { useDebounce } from "../utils/useDebounce"
export default function InfiniteCanvas() { export default function InfiniteCanvas() {
const containerRef = useRef<HTMLDivElement>(null) const containerRef = useRef<HTMLDivElement>(null)
const isDarkTheme = useThemeDetector() const isDarkTheme = useThemeDetector()
const { debounce } = useDebounce()
useEffect(() => { useEffect(() => {
const initPos = getInitialPosition(canvasDimentsion) const initPos = getInitialPosition(canvasDimentsion)
@ -27,22 +29,24 @@ export default function InfiniteCanvas() {
} }
function _updatePosition() { function _updatePosition() {
localStorage.setItem("pos", JSON.stringify({ debounce(() => {
left: window.scrollX, localStorage.setItem("pos", JSON.stringify({
top: window.scrollY left: window.scrollX,
})) top: window.scrollY
}))
})
} }
_updateDimensions() _updateDimensions()
window.addEventListener("resize", _updateDimensions) window.addEventListener("resize", _updateDimensions)
window.addEventListener("scrollend", _updatePosition) window.addEventListener("scroll", _updatePosition)
notes.loadLocalStorage() notes.loadLocalStorage()
images.loadLocalStorage() images.loadLocalStorage()
texts.loadLocalStorage() texts.loadLocalStorage()
return () => { return () => {
window.removeEventListener("resize", _updateDimensions) window.removeEventListener("resize", _updateDimensions)
window.removeEventListener("scrollend", _updatePosition) window.removeEventListener("scroll", _updatePosition)
} }
}, []) }, [])