2021-12-12 22:14:52 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Contract } from 'common/contract'
|
2022-02-21 03:06:10 +00:00
|
|
|
import {
|
|
|
|
Bet,
|
|
|
|
listenForBets,
|
|
|
|
listenForRecentBets,
|
2022-07-10 18:05:44 +00:00
|
|
|
listenForUnfilledBets,
|
2022-02-21 03:06:10 +00:00
|
|
|
withoutAnteBets,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/lib/firebase/bets'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { LimitBet } from 'common/bet'
|
2022-10-07 03:16:49 +00:00
|
|
|
import { getUser } from 'web/lib/firebase/users'
|
2021-12-12 22:14:52 +00:00
|
|
|
|
2022-08-04 21:27:02 +00:00
|
|
|
export const useBets = (
|
|
|
|
contractId: string,
|
|
|
|
options?: { filterChallenges: boolean; filterRedemptions: boolean }
|
|
|
|
) => {
|
2022-01-15 00:16:25 +00:00
|
|
|
const [bets, setBets] = useState<Bet[] | undefined>()
|
2022-08-09 15:50:11 +00:00
|
|
|
const filterChallenges = !!options?.filterChallenges
|
|
|
|
const filterRedemptions = !!options?.filterRedemptions
|
2021-12-12 22:14:52 +00:00
|
|
|
useEffect(() => {
|
2022-08-04 21:27:02 +00:00
|
|
|
if (contractId)
|
|
|
|
return listenForBets(contractId, (bets) => {
|
2022-08-09 15:50:11 +00:00
|
|
|
if (filterChallenges || filterRedemptions)
|
2022-08-04 21:27:02 +00:00
|
|
|
setBets(
|
|
|
|
bets.filter(
|
|
|
|
(bet) =>
|
2022-08-09 15:50:11 +00:00
|
|
|
(filterChallenges ? !bet.challengeSlug : true) &&
|
|
|
|
(filterRedemptions ? !bet.isRedemption : true)
|
2022-08-04 21:27:02 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
else setBets(bets)
|
|
|
|
})
|
2022-08-09 15:50:11 +00:00
|
|
|
}, [contractId, filterChallenges, filterRedemptions])
|
2021-12-12 22:14:52 +00:00
|
|
|
|
|
|
|
return bets
|
|
|
|
}
|
2022-02-10 20:36:15 +00:00
|
|
|
|
2022-02-17 23:39:18 +00:00
|
|
|
export const useBetsWithoutAntes = (contract: Contract, initialBets: Bet[]) => {
|
|
|
|
const [bets, setBets] = useState<Bet[]>(
|
2022-02-18 00:18:40 +00:00
|
|
|
withoutAnteBets(contract, initialBets)
|
2022-02-10 20:36:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-07-10 18:05:44 +00:00
|
|
|
|
|
|
|
export const useUnfilledBets = (contractId: string) => {
|
|
|
|
const [unfilledBets, setUnfilledBets] = useState<LimitBet[] | undefined>()
|
|
|
|
useEffect(
|
|
|
|
() => listenForUnfilledBets(contractId, setUnfilledBets),
|
|
|
|
[contractId]
|
|
|
|
)
|
|
|
|
return unfilledBets
|
|
|
|
}
|
2022-10-07 03:16:49 +00:00
|
|
|
|
|
|
|
export const useUnfilledBetsAndBalanceByUserId = (contractId: string) => {
|
|
|
|
const [data, setData] = useState<{
|
|
|
|
unfilledBets: LimitBet[]
|
|
|
|
balanceByUserId: { [userId: string]: number }
|
|
|
|
}>({ unfilledBets: [], balanceByUserId: {} })
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let requestCount = 0
|
|
|
|
|
|
|
|
return listenForUnfilledBets(contractId, (unfilledBets) => {
|
|
|
|
requestCount++
|
|
|
|
const count = requestCount
|
|
|
|
|
|
|
|
Promise.all(unfilledBets.map((bet) => getUser(bet.userId))).then(
|
|
|
|
(users) => {
|
|
|
|
if (count === requestCount) {
|
|
|
|
const balanceByUserId = Object.fromEntries(
|
|
|
|
users.map((user) => [user.id, user.balance])
|
|
|
|
)
|
|
|
|
setData({ unfilledBets, balanceByUserId })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}, [contractId])
|
|
|
|
return data
|
|
|
|
}
|