2022-08-28 23:03:00 +00:00
|
|
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
2022-05-22 08:36:05 +00:00
|
|
|
import { isEqual } from 'lodash'
|
2022-05-01 16:36:54 +00:00
|
|
|
import { useEffect, useRef, useState } from 'react'
|
2022-01-05 06:32:52 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
2022-02-25 08:11:10 +00:00
|
|
|
listenForActiveContracts,
|
2022-05-01 16:36:54 +00:00
|
|
|
listenForContract,
|
2022-01-05 06:32:52 +00:00
|
|
|
listenForContracts,
|
2022-01-09 20:26:51 +00:00
|
|
|
listenForHotContracts,
|
2022-02-27 21:37:04 +00:00
|
|
|
listenForInactiveContracts,
|
2022-04-20 21:59:34 +00:00
|
|
|
listenForNewContracts,
|
2022-08-28 23:03:00 +00:00
|
|
|
getUserBetContractsQuery,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/lib/firebase/contracts'
|
2021-12-17 04:44:48 +00:00
|
|
|
|
|
|
|
export const useContracts = () => {
|
2022-01-09 20:51:20 +00:00
|
|
|
const [contracts, setContracts] = useState<Contract[] | undefined>()
|
2021-12-17 04:44:48 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return listenForContracts(setContracts)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return contracts
|
|
|
|
}
|
2022-01-05 06:32:52 +00:00
|
|
|
|
2022-02-25 08:11:10 +00:00
|
|
|
export const useActiveContracts = () => {
|
2022-04-20 21:59:34 +00:00
|
|
|
const [activeContracts, setActiveContracts] = useState<
|
|
|
|
Contract[] | undefined
|
|
|
|
>()
|
|
|
|
const [newContracts, setNewContracts] = useState<Contract[] | undefined>()
|
2022-02-25 08:11:10 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-04-20 21:59:34 +00:00
|
|
|
return listenForActiveContracts(setActiveContracts)
|
2022-02-25 08:11:10 +00:00
|
|
|
}, [])
|
|
|
|
|
2022-04-20 21:59:34 +00:00
|
|
|
useEffect(() => {
|
|
|
|
return listenForNewContracts(setNewContracts)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
if (!activeContracts || !newContracts) return undefined
|
|
|
|
|
|
|
|
return [...activeContracts, ...newContracts]
|
2022-02-25 08:11:10 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 21:37:04 +00:00
|
|
|
export const useInactiveContracts = () => {
|
|
|
|
const [contracts, setContracts] = useState<Contract[] | undefined>()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return listenForInactiveContracts(setContracts)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return contracts
|
|
|
|
}
|
|
|
|
|
2022-01-05 06:32:52 +00:00
|
|
|
export const useHotContracts = () => {
|
2022-01-09 20:26:51 +00:00
|
|
|
const [hotContracts, setHotContracts] = useState<Contract[] | undefined>()
|
2022-01-05 06:32:52 +00:00
|
|
|
|
2022-01-09 20:26:51 +00:00
|
|
|
useEffect(() => listenForHotContracts(setHotContracts), [])
|
2022-01-05 06:32:52 +00:00
|
|
|
|
2022-01-09 20:26:51 +00:00
|
|
|
return hotContracts
|
2022-01-05 06:32:52 +00:00
|
|
|
}
|
2022-05-01 16:36:54 +00:00
|
|
|
|
|
|
|
export const useUpdatedContracts = (contracts: Contract[] | undefined) => {
|
|
|
|
const [__, triggerUpdate] = useState(0)
|
|
|
|
const contractDict = useRef<{ [id: string]: Contract }>({})
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (contracts === undefined) return
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
contractDict.current = Object.fromEntries(contracts.map((c) => [c.id, c]))
|
2022-05-01 16:36:54 +00:00
|
|
|
|
|
|
|
const disposes = contracts.map((contract) => {
|
|
|
|
const { id } = contract
|
|
|
|
|
|
|
|
return listenForContract(id, (contract) => {
|
|
|
|
const curr = contractDict.current[id]
|
2022-05-22 08:36:05 +00:00
|
|
|
if (!isEqual(curr, contract)) {
|
2022-05-01 16:36:54 +00:00
|
|
|
contractDict.current[id] = contract as Contract
|
|
|
|
triggerUpdate((n) => n + 1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
triggerUpdate((n) => n + 1)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
disposes.forEach((dispose) => dispose())
|
|
|
|
}
|
|
|
|
}, [!!contracts])
|
|
|
|
|
|
|
|
return contracts && Object.keys(contractDict.current).length > 0
|
|
|
|
? contracts.map((c) => contractDict.current[c.id])
|
|
|
|
: undefined
|
|
|
|
}
|
2022-08-28 23:03:00 +00:00
|
|
|
|
|
|
|
export const useUserBetContracts = (userId: string) => {
|
|
|
|
const result = useFirestoreQueryData(
|
|
|
|
['contracts', 'bets', userId],
|
|
|
|
getUserBetContractsQuery(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
|
|
|
|
}
|