predikt/oracle/web/components/widgets/visibility-observer.tsx
caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
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>
2026-07-06 23:29:50 +03:00

59 lines
1.6 KiB
TypeScript

import { useEffect, useRef, useState } from 'react'
import { useEvent } from 'client-common/hooks/use-event'
export function VisibilityObserver(props: {
className?: string
onVisibilityUpdated: (visible: boolean) => void
}) {
const { className } = props
const [elem, setElem] = useState<HTMLElement | null>(null)
const onVisibilityUpdated = useEvent(props.onVisibilityUpdated)
useEffect(() => {
if (elem) {
const observer = new IntersectionObserver(([entry]) => {
onVisibilityUpdated(entry.isIntersecting)
}, {})
observer.observe(elem)
return () => observer.unobserve(elem)
}
}, [elem, onVisibilityUpdated])
return <div ref={setElem} className={className}></div>
}
export function LoadMoreUntilNotVisible(props: {
// Returns true if there are more results.
loadMore: () => Promise<boolean>
}) {
const { loadMore } = props
const isVisibleRef = useRef(false)
const loadMoreIfVisible = useEvent(async () => {
if (isVisibleRef.current && loadMore) {
const hasMoreResults = await loadMore()
if (hasMoreResults) {
setTimeout(() => {
if (isVisibleRef.current) {
loadMoreIfVisible()
}
}, 500)
}
}
})
const onVisibilityUpdated = useEvent((visible: boolean) => {
isVisibleRef.current = visible
loadMoreIfVisible()
})
return (
<div className="relative">
<VisibilityObserver
// Loads more as soon as one screen height of content is left.
className="pointer-events-none absolute bottom-0 h-screen w-full select-none"
onVisibilityUpdated={onVisibilityUpdated}
/>
</div>
)
}