996b4795ea
* Convert useUserBets to react query * Fix duplicate key warnings * Fix react-query workaround to use refetchOnMount: always' * Use react query for portfolio history * Fix useUserBet workaround * Script to back fill unique bettors in all contracts * React query for user bet contracts, using uniqueBettorsId! * Prefetch user bets / portfolio data
50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
|
import { useEffect, useState } from 'react'
|
|
import {
|
|
Bet,
|
|
getUserBetsQuery,
|
|
listenForUserContractBets,
|
|
} from 'web/lib/firebase/bets'
|
|
|
|
export const useUserBets = (userId: string) => {
|
|
const result = useFirestoreQueryData(
|
|
['bets', userId],
|
|
getUserBetsQuery(userId),
|
|
{ subscribe: true, includeMetadataChanges: true },
|
|
// Temporary workaround for react-query bug:
|
|
// https://github.com/invertase/react-query-firebase/issues/25
|
|
{ refetchOnMount: 'always' }
|
|
)
|
|
return result.data
|
|
}
|
|
|
|
export const useUserContractBets = (
|
|
userId: string | undefined,
|
|
contractId: string | undefined
|
|
) => {
|
|
const [bets, setBets] = useState<Bet[] | undefined>(undefined)
|
|
|
|
useEffect(() => {
|
|
if (userId && contractId)
|
|
return listenForUserContractBets(userId, contractId, setBets)
|
|
}, [userId, contractId])
|
|
|
|
return bets
|
|
}
|
|
|
|
export const useGetUserBetContractIds = (userId: string | undefined) => {
|
|
const [contractIds, setContractIds] = useState<string[] | undefined>()
|
|
|
|
useEffect(() => {
|
|
if (userId) {
|
|
const key = `user-bet-contractIds-${userId}`
|
|
const userBetContractJson = localStorage.getItem(key)
|
|
if (userBetContractJson) {
|
|
setContractIds(JSON.parse(userBetContractJson))
|
|
}
|
|
}
|
|
}, [userId])
|
|
|
|
return contractIds
|
|
}
|