2022-09-22 21:57:48 +00:00
|
|
|
import { CPMMBinaryContract } from 'common/contract'
|
|
|
|
import { sortBy, uniqBy } from 'lodash'
|
|
|
|
import { useQuery } from 'react-query'
|
2022-09-05 23:09:01 +00:00
|
|
|
import {
|
2022-09-22 21:57:48 +00:00
|
|
|
probChangeAscendingIndex,
|
|
|
|
probChangeDescendingIndex,
|
|
|
|
} from 'web/lib/service/algolia'
|
2022-09-18 00:18:46 +00:00
|
|
|
|
2022-09-22 21:57:48 +00:00
|
|
|
export const useProbChanges = (
|
|
|
|
filters: { bettorId?: string; groupSlugs?: string[] } = {}
|
|
|
|
) => {
|
|
|
|
const { bettorId, groupSlugs } = filters
|
2022-09-18 00:18:46 +00:00
|
|
|
|
2022-09-22 21:57:48 +00:00
|
|
|
const bettorFilter = bettorId ? `uniqueBettorIds:${bettorId}` : ''
|
|
|
|
const groupFilters = groupSlugs
|
|
|
|
? groupSlugs.map((slug) => `groupLinks.slug:${slug}`)
|
|
|
|
: []
|
2022-09-18 00:18:46 +00:00
|
|
|
|
2022-09-22 21:57:48 +00:00
|
|
|
const facetFilters = [
|
|
|
|
'isResolved:false',
|
|
|
|
'outcomeType:BINARY',
|
|
|
|
bettorFilter,
|
|
|
|
groupFilters,
|
|
|
|
]
|
|
|
|
const searchParams = {
|
|
|
|
facetFilters,
|
|
|
|
hitsPerPage: 50,
|
2022-09-18 00:18:46 +00:00
|
|
|
}
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-22 21:57:48 +00:00
|
|
|
const { data: positiveChanges } = useQuery(
|
|
|
|
['prob-change-day', groupSlugs],
|
|
|
|
() => probChangeDescendingIndex.search<CPMMBinaryContract>('', searchParams)
|
2022-09-05 23:09:01 +00:00
|
|
|
)
|
2022-09-22 21:57:48 +00:00
|
|
|
const { data: negativeChanges } = useQuery(
|
|
|
|
['prob-change-day-ascending', groupSlugs],
|
|
|
|
() => probChangeAscendingIndex.search<CPMMBinaryContract>('', searchParams)
|
2022-09-05 23:09:01 +00:00
|
|
|
)
|
|
|
|
|
2022-09-22 21:57:48 +00:00
|
|
|
if (!positiveChanges || !negativeChanges) return undefined
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-22 21:57:48 +00:00
|
|
|
const hits = uniqBy(
|
|
|
|
[...positiveChanges.hits, ...negativeChanges.hits],
|
|
|
|
(c) => c.id
|
2022-09-23 22:58:05 +00:00
|
|
|
).filter((c) => c.probChanges)
|
2022-09-16 21:35:03 +00:00
|
|
|
|
2022-09-22 21:57:48 +00:00
|
|
|
return sortBy(hits, (c) => Math.abs(c.probChanges.day)).reverse()
|
2022-09-16 21:35:03 +00:00
|
|
|
}
|