manifold/web/hooks/use-bets.ts

48 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2021-12-12 22:14:52 +00:00
import { useEffect, useState } from 'react'
import { Contract } from '../../common/contract'
2022-02-21 03:06:10 +00:00
import {
Bet,
getRecentBets,
2022-02-21 03:06:10 +00:00
listenForBets,
listenForRecentBets,
withoutAnteBets,
} from '../lib/firebase/bets'
2021-12-12 22:14:52 +00:00
export const useBets = (contractId: string) => {
const [bets, setBets] = useState<Bet[] | undefined>()
2021-12-12 22:14:52 +00:00
useEffect(() => {
if (contractId) return listenForBets(contractId, setBets)
}, [contractId])
return bets
}
2022-02-17 23:39:18 +00:00
export const useBetsWithoutAntes = (contract: Contract, initialBets: Bet[]) => {
const [bets, setBets] = useState<Bet[]>(
withoutAnteBets(contract, initialBets)
)
useEffect(() => {
return listenForBets(contract.id, (bets) => {
setBets(withoutAnteBets(contract, bets))
})
}, [contract])
return bets
}
2022-02-21 03:06:10 +00:00
export const useRecentBets = () => {
const [recentBets, setRecentBets] = useState<Bet[] | undefined>()
useEffect(() => listenForRecentBets(setRecentBets), [])
return recentBets
}
export const useGetRecentBets = () => {
const [recentBets, setRecentBets] = useState<Bet[] | undefined>()
useEffect(() => {
getRecentBets().then(setRecentBets)
}, [])
return recentBets
}