2022-09-05 23:09:01 +00:00
|
|
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
2022-09-18 00:18:46 +00:00
|
|
|
import { CPMMContract } from 'common/contract'
|
2022-09-16 21:35:03 +00:00
|
|
|
import { MINUTE_MS } from 'common/util/time'
|
2022-09-18 00:18:46 +00:00
|
|
|
import { useQuery, useQueryClient } from 'react-query'
|
2022-09-05 23:09:01 +00:00
|
|
|
import {
|
|
|
|
getProbChangesNegative,
|
|
|
|
getProbChangesPositive,
|
|
|
|
} from 'web/lib/firebase/contracts'
|
2022-09-16 21:35:03 +00:00
|
|
|
import { getValues } from 'web/lib/firebase/utils'
|
2022-09-18 00:18:46 +00:00
|
|
|
import { getIndexName, searchClient } from 'web/lib/service/algolia'
|
|
|
|
|
|
|
|
export const useProbChangesAlgolia = (userId: string) => {
|
|
|
|
const { data: positiveData } = useQuery(['prob-change-day', userId], () =>
|
|
|
|
searchClient
|
|
|
|
.initIndex(getIndexName('prob-change-day'))
|
|
|
|
.search<CPMMContract>('', { facetFilters: ['uniqueBettorIds:' + userId] })
|
|
|
|
)
|
|
|
|
const { data: negativeData } = useQuery(
|
|
|
|
['prob-change-day-ascending', userId],
|
|
|
|
() =>
|
|
|
|
searchClient
|
|
|
|
.initIndex(getIndexName('prob-change-day-ascending'))
|
|
|
|
.search<CPMMContract>('', {
|
|
|
|
facetFilters: ['uniqueBettorIds:' + userId],
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!positiveData || !negativeData) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
positiveChanges: positiveData.hits,
|
|
|
|
negativeChanges: negativeData.hits,
|
|
|
|
}
|
|
|
|
}
|
2022-09-05 23:09:01 +00:00
|
|
|
|
|
|
|
export const useProbChanges = (userId: string) => {
|
|
|
|
const { data: positiveChanges } = useFirestoreQueryData(
|
|
|
|
['prob-changes-day-positive', userId],
|
|
|
|
getProbChangesPositive(userId)
|
|
|
|
)
|
|
|
|
const { data: negativeChanges } = useFirestoreQueryData(
|
|
|
|
['prob-changes-day-negative', userId],
|
|
|
|
getProbChangesNegative(userId)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!positiveChanges || !negativeChanges) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return { positiveChanges, negativeChanges }
|
|
|
|
}
|
2022-09-16 21:35:03 +00:00
|
|
|
|
|
|
|
export const usePrefetchProbChanges = (userId: string | undefined) => {
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
if (userId) {
|
|
|
|
queryClient.prefetchQuery(
|
|
|
|
['prob-changes-day-positive', userId],
|
|
|
|
() => getValues(getProbChangesPositive(userId)),
|
|
|
|
{ staleTime: MINUTE_MS }
|
|
|
|
)
|
|
|
|
queryClient.prefetchQuery(
|
|
|
|
['prob-changes-day-negative', userId],
|
|
|
|
() => getValues(getProbChangesNegative(userId)),
|
|
|
|
{ staleTime: MINUTE_MS }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|