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>
49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import { useEffect, useRef } from 'react'
|
|
import { useEvent } from 'client-common/hooks/use-event'
|
|
|
|
export function useIsVisible(
|
|
onSeenStart: () => void,
|
|
triggerOnce = false,
|
|
isLoaded = true,
|
|
onSeenEnd?: () => void
|
|
) {
|
|
const ref = useRef<HTMLDivElement | null>(null)
|
|
const seenOnce = useRef(false)
|
|
const eventOnVisible = useEvent(onSeenStart)
|
|
const eventOnInvisible = useEvent(onSeenEnd)
|
|
|
|
useEffect(() => {
|
|
const element = ref.current
|
|
if (!element || !isLoaded) return
|
|
|
|
const innerHeight = window.innerHeight * 0.9
|
|
const elementHeight = element.getBoundingClientRect().height
|
|
const threshold = elementHeight >= innerHeight ? 0.01 : 0.9
|
|
|
|
const observer = new IntersectionObserver(
|
|
(entries) => {
|
|
entries.forEach((entry) => {
|
|
if (entry.isIntersecting) {
|
|
eventOnVisible()
|
|
seenOnce.current = true
|
|
if (triggerOnce && !onSeenEnd) observer.unobserve(element)
|
|
} else if (seenOnce.current) {
|
|
if (onSeenEnd) eventOnInvisible()
|
|
if (triggerOnce) observer.unobserve(element)
|
|
}
|
|
})
|
|
},
|
|
{ threshold }
|
|
)
|
|
|
|
if (!triggerOnce || (triggerOnce && !seenOnce.current))
|
|
observer.observe(element)
|
|
|
|
return () => {
|
|
observer.unobserve(element)
|
|
}
|
|
}, [ref, triggerOnce, eventOnVisible, eventOnInvisible, isLoaded])
|
|
|
|
return { ref }
|
|
}
|