manifold/web/hooks/use-prob-changes.tsx
James Grugett c6d034545a
Home: Prob change cards. Sort by daily score. (#925)
* Add dailyScore: product of unique bettors (3 days) and probChanges.day

* Increase memory and duration of scoreContracts

* Home: Smaller prob change card for groups. Use dailyScore for sort order (algolia)

* Add back hover
2022-09-22 16:57:48 -05:00

48 lines
1.3 KiB
TypeScript

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