diff --git a/web/components/contract/contracts-list.tsx b/web/components/contract/contracts-list.tsx index f55ca757..f886e982 100644 --- a/web/components/contract/contracts-list.tsx +++ b/web/components/contract/contracts-list.tsx @@ -4,6 +4,8 @@ import { Col } from '../layout/col' import { SiteLink } from '../site-link' import { ContractCard } from './contract-card' import { ContractSearch } from '../contract-search' +import { useIsVisible } from 'web/hooks/use-is-visible' +import { useEffect, useState } from 'react' export function ContractsGrid(props: { contracts: Contract[] @@ -13,6 +15,16 @@ export function ContractsGrid(props: { }) { const { contracts, showCloseTime, hasMore, loadMore } = props + const [elem, setElem] = useState(null) + const isBottomVisible = useIsVisible(elem) + + useEffect(() => { + console.log({ isBottomVisible, hasMore }) + if (isBottomVisible) { + loadMore() + } + }, [isBottomVisible, hasMore, loadMore]) + if (contracts.length === 0) { return (

@@ -35,14 +47,7 @@ export function ContractsGrid(props: { /> ))} - {hasMore && ( - - )} +

) } diff --git a/web/components/feed/feed-items.tsx b/web/components/feed/feed-items.tsx index 55d46837..533bf9bf 100644 --- a/web/components/feed/feed-items.tsx +++ b/web/components/feed/feed-items.tsx @@ -59,11 +59,11 @@ export function FeedItems(props: { const { contract, items, className, betRowClassName } = props const { outcomeType } = contract - const ref = useRef(null) - useSaveSeenContract(ref, contract) + const [elem, setElem] = useState(null) + useSaveSeenContract(elem, contract) return ( -
+
{items.map((item, activityItemIdx) => (
diff --git a/web/hooks/use-is-visible.ts b/web/hooks/use-is-visible.ts index baf26afa..a3e2c1fa 100644 --- a/web/hooks/use-is-visible.ts +++ b/web/hooks/use-is-visible.ts @@ -1,11 +1,11 @@ -import { RefObject, useEffect, useState } from 'react' +import { useEffect, useState } from 'react' -export function useIsVisible(elementRef: RefObject) { - return !!useIntersectionObserver(elementRef)?.isIntersecting +export function useIsVisible(element: HTMLElement | null) { + return !!useIntersectionObserver(element)?.isIntersecting } function useIntersectionObserver( - elementRef: RefObject + elem: HTMLElement | null ): IntersectionObserverEntry | undefined { const [entry, setEntry] = useState() @@ -14,16 +14,15 @@ function useIntersectionObserver( } useEffect(() => { - const node = elementRef?.current const hasIOSupport = !!window.IntersectionObserver - if (!hasIOSupport || !node) return + if (!hasIOSupport || !elem) return const observer = new IntersectionObserver(updateEntry, {}) - observer.observe(node) + observer.observe(elem) return () => observer.disconnect() - }, [elementRef]) + }, [elem]) return entry } diff --git a/web/hooks/use-seen-contracts.ts b/web/hooks/use-seen-contracts.ts index 6157f8c7..efe65743 100644 --- a/web/hooks/use-seen-contracts.ts +++ b/web/hooks/use-seen-contracts.ts @@ -1,5 +1,5 @@ import _ from 'lodash' -import { useEffect, RefObject, useState } from 'react' +import { useEffect, useState } from 'react' import { Contract } from 'common/contract' import { trackView } from 'web/lib/firebase/tracking' import { useIsVisible } from './use-is-visible' @@ -17,10 +17,10 @@ export const useSeenContracts = () => { } export const useSaveSeenContract = ( - ref: RefObject, + elem: HTMLElement | null, contract: Contract ) => { - const isVisible = useIsVisible(ref) + const isVisible = useIsVisible(elem) useEffect(() => { if (isVisible) {