manifold/web/hooks/use-contracts.ts

32 lines
779 B
TypeScript
Raw Normal View History

2022-01-05 06:32:52 +00:00
import _ from 'lodash'
import { useEffect, useState } from 'react'
2022-01-05 06:32:52 +00:00
import { Bet, listenForRecentBets } from '../lib/firebase/bets'
import {
computeHotContracts,
Contract,
listenForContracts,
} from '../lib/firebase/contracts'
export const useContracts = () => {
const [contracts, setContracts] = useState<Contract[] | 'loading'>('loading')
useEffect(() => {
return listenForContracts(setContracts)
}, [])
return contracts
}
2022-01-05 06:32:52 +00:00
export const useHotContracts = () => {
const [recentBets, setRecentBets] = useState<Bet[] | 'loading'>('loading')
useEffect(() => {
const oneDay = 1000 * 60 * 60 * 24
return listenForRecentBets(oneDay, setRecentBets)
}, [])
if (recentBets === 'loading') return 'loading'
return computeHotContracts(recentBets)
}