Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
35 lines
1004 B
TypeScript
35 lines
1004 B
TypeScript
import { useEffect } from 'react'
|
|
import { debounce } from 'lodash'
|
|
import { safeLocalStorage } from 'web/lib/util/local'
|
|
import { usePersistentInMemoryState } from 'client-common/hooks/use-persistent-in-memory-state'
|
|
|
|
export const useSaveScroll = (
|
|
locationKey: string,
|
|
clearWhenMemoryClears?: boolean
|
|
) => {
|
|
const key = `${locationKey}-scroll`
|
|
const [savedInMemory, setSavedInMemory] = usePersistentInMemoryState(
|
|
false,
|
|
key
|
|
)
|
|
useEffect(() => {
|
|
const onScroll = debounce(() => {
|
|
safeLocalStorage?.setItem(key, window.scrollY.toString())
|
|
setSavedInMemory(true)
|
|
}, 100)
|
|
window.addEventListener('scroll', onScroll)
|
|
return () => window.removeEventListener('scroll', onScroll)
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
const scrollY = safeLocalStorage?.getItem(key)
|
|
if (scrollY && !savedInMemory && clearWhenMemoryClears) {
|
|
safeLocalStorage?.removeItem(key)
|
|
return
|
|
}
|
|
if (scrollY) {
|
|
window.scrollTo(0, parseInt(scrollY))
|
|
}
|
|
}, [])
|
|
}
|