2022-08-28 23:03:00 +00:00
|
|
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
2022-09-13 22:47:29 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-01-05 06:32:52 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
|
|
|
listenForContracts,
|
2022-01-09 20:26:51 +00:00
|
|
|
listenForHotContracts,
|
2022-02-27 21:37:04 +00:00
|
|
|
listenForInactiveContracts,
|
2022-09-02 02:38:09 +00:00
|
|
|
getUserBetContracts,
|
2022-08-28 23:03:00 +00:00
|
|
|
getUserBetContractsQuery,
|
2022-09-16 06:37:17 +00:00
|
|
|
listAllContracts,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/lib/firebase/contracts'
|
2022-09-22 21:57:48 +00:00
|
|
|
import { QueryClient, useQuery, useQueryClient } from 'react-query'
|
2022-09-23 20:30:44 +00:00
|
|
|
import { MINUTE_MS, sleep } from 'common/util/time'
|
2022-10-01 01:07:49 +00:00
|
|
|
import {
|
|
|
|
dailyScoreIndex,
|
|
|
|
newIndex,
|
|
|
|
trendingIndex,
|
|
|
|
} from 'web/lib/service/algolia'
|
2022-09-22 21:57:48 +00:00
|
|
|
import { CPMMBinaryContract } from 'common/contract'
|
|
|
|
import { zipObject } from 'lodash'
|
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-10-01 01:07:49 +00:00
|
|
|
export const useTrendingContracts = (maxContracts: number) => {
|
|
|
|
const { data } = useQuery(['trending-contracts', maxContracts], () =>
|
|
|
|
trendingIndex.search<CPMMBinaryContract>('', {
|
2022-10-03 22:33:51 +00:00
|
|
|
facetFilters: ['isResolved:false', 'visibility:public'],
|
2022-10-01 01:07:49 +00:00
|
|
|
hitsPerPage: maxContracts,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
if (!data) return undefined
|
|
|
|
return data.hits
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useNewContracts = (maxContracts: number) => {
|
|
|
|
const { data } = useQuery(['newest-contracts', maxContracts], () =>
|
|
|
|
newIndex.search<CPMMBinaryContract>('', {
|
2022-10-03 22:33:51 +00:00
|
|
|
facetFilters: ['isResolved:false', 'visibility:public'],
|
2022-10-01 01:07:49 +00:00
|
|
|
hitsPerPage: maxContracts,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
if (!data) return undefined
|
|
|
|
return data.hits
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useContractsByDailyScoreNotBetOn = (
|
|
|
|
userId: string | null | undefined,
|
|
|
|
maxContracts: number
|
|
|
|
) => {
|
|
|
|
const { data } = useQuery(['daily-score', userId, maxContracts], () =>
|
|
|
|
dailyScoreIndex.search<CPMMBinaryContract>('', {
|
2022-10-03 22:33:51 +00:00
|
|
|
facetFilters: [
|
|
|
|
'isResolved:false',
|
|
|
|
'visibility:public',
|
|
|
|
`uniqueBettors:-${userId}`,
|
|
|
|
],
|
2022-10-01 01:07:49 +00:00
|
|
|
hitsPerPage: maxContracts,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
if (!userId || !data) return undefined
|
|
|
|
return data.hits.filter((c) => c.dailyScore)
|
|
|
|
}
|
|
|
|
|
2022-09-22 21:57:48 +00:00
|
|
|
export const useContractsByDailyScoreGroups = (
|
|
|
|
groupSlugs: string[] | undefined
|
|
|
|
) => {
|
|
|
|
const { data } = useQuery(['daily-score', groupSlugs], () =>
|
|
|
|
Promise.all(
|
|
|
|
(groupSlugs ?? []).map((slug) =>
|
|
|
|
dailyScoreIndex.search<CPMMBinaryContract>('', {
|
2022-10-01 01:07:49 +00:00
|
|
|
facetFilters: ['isResolved:false', `groupLinks.slug:${slug}`],
|
2022-09-22 21:57:48 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if (!groupSlugs || !data || data.length !== groupSlugs.length)
|
|
|
|
return undefined
|
|
|
|
|
|
|
|
return zipObject(
|
|
|
|
groupSlugs,
|
|
|
|
data.map((d) => d.hits.filter((c) => c.dailyScore))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-16 06:37:17 +00:00
|
|
|
const q = new QueryClient()
|
|
|
|
export const getCachedContracts = async () =>
|
2022-10-04 13:38:07 +00:00
|
|
|
q.fetchQuery(['contracts'], () => listAllContracts(10000), {
|
2022-09-16 06:37:17 +00:00
|
|
|
staleTime: Infinity,
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
2022-09-02 02:38:09 +00:00
|
|
|
export const usePrefetchUserBetContracts = (userId: string) => {
|
|
|
|
const queryClient = useQueryClient()
|
2022-09-08 21:59:05 +00:00
|
|
|
return queryClient.prefetchQuery(
|
|
|
|
['contracts', 'bets', userId],
|
2022-09-23 20:30:44 +00:00
|
|
|
() => sleep(1000).then(() => getUserBetContracts(userId)),
|
2022-09-08 21:59:05 +00:00
|
|
|
{ staleTime: 5 * MINUTE_MS }
|
2022-09-01 04:59:58 +00:00
|
|
|
)
|
2022-09-02 02:38:09 +00:00
|
|
|
}
|
2022-09-01 04:59:58 +00:00
|
|
|
|
2022-08-28 23:03:00 +00:00
|
|
|
export const useUserBetContracts = (userId: string) => {
|
|
|
|
const result = useFirestoreQueryData(
|
|
|
|
['contracts', 'bets', userId],
|
2022-08-29 21:47:19 +00:00
|
|
|
getUserBetContractsQuery(userId)
|
2022-08-28 23:03:00 +00:00
|
|
|
)
|
|
|
|
return result.data
|
|
|
|
}
|