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>
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
import { uniqBy } from 'lodash'
|
|
import { Contract } from 'common/contract'
|
|
import { useEffect, useRef, useState } from 'react'
|
|
import { usePrivateUser } from './use-user'
|
|
import { isContractBlocked } from 'web/lib/firebase/users'
|
|
import { useEvent } from 'client-common/hooks/use-event'
|
|
import { api } from 'web/lib/api/api'
|
|
|
|
const LIMIT = 10
|
|
|
|
export const useRelatedMarkets = (
|
|
contract: Contract,
|
|
initialContracts: Contract[]
|
|
) => {
|
|
const [savedContracts, setSavedContracts] = useState(initialContracts)
|
|
const offset = useRef(0)
|
|
const privateUser = usePrivateUser()
|
|
let hasLoadedMoreContracts = false
|
|
|
|
const loadMore = useEvent(async () => {
|
|
const { groupContracts } = await api('get-related-markets-by-group', {
|
|
contractId: contract.id,
|
|
limit: LIMIT,
|
|
offset: offset.current,
|
|
})
|
|
|
|
setSavedContracts((sc) => {
|
|
const newContracts = uniqBy([...sc, ...groupContracts], (c) => c.id)
|
|
const filteredContracts = newContracts.filter(
|
|
(c) => !isContractBlocked(privateUser, c) && c.id !== contract.id
|
|
)
|
|
if (filteredContracts.length > sc.length) {
|
|
hasLoadedMoreContracts = true
|
|
}
|
|
offset.current += LIMIT
|
|
return filteredContracts
|
|
})
|
|
|
|
return hasLoadedMoreContracts
|
|
})
|
|
|
|
useEffect(() => {
|
|
if (initialContracts.length === 0) loadMore()
|
|
}, [])
|
|
|
|
return { contracts: savedContracts, loadMore }
|
|
}
|