Allow sorting by highest %

This commit is contained in:
Austin Chen 2022-09-02 18:26:26 -07:00
parent 57b74a5d09
commit 1f6cab8cb4
3 changed files with 25 additions and 2 deletions

View File

@ -8,6 +8,8 @@ import {
usePersistentState, usePersistentState,
urlParamStore, urlParamStore,
} from 'web/hooks/use-persistent-state' } from 'web/hooks/use-persistent-state'
import { getProbability } from 'common/calculate'
import { BinaryContract, PseudoNumericContract } from 'common/contract'
const MAX_CONTRACTS_RENDERED = 100 const MAX_CONTRACTS_RENDERED = 100
@ -18,13 +20,17 @@ export default function ContractSearchFirestore(props: {
excludeContractIds?: string[] excludeContractIds?: string[]
groupSlug?: string groupSlug?: string
} }
defaultSort?: string
}) { }) {
const { additionalFilter } = props const { additionalFilter } = props
const contracts = useContracts() const contracts = useContracts()
const router = useRouter() const router = useRouter()
const store = urlParamStore(router) const store = urlParamStore(router)
const [query, setQuery] = usePersistentState('', { key: 'q', store }) const [query, setQuery] = usePersistentState('', { key: 'q', store })
const [sort, setSort] = usePersistentState('score', { key: 'sort', store }) const [sort, setSort] = usePersistentState(props.defaultSort ?? 'score', {
key: 'sort',
store,
})
let matches = (contracts ?? []).filter((c) => let matches = (contracts ?? []).filter((c) =>
searchInAny( searchInAny(
@ -51,6 +57,10 @@ export default function ContractSearchFirestore(props: {
// Use lodash for stable sort, so previous sort breaks all ties. // Use lodash for stable sort, so previous sort breaks all ties.
matches = sortBy(matches, ({ volume7Days }) => -1 * volume7Days) matches = sortBy(matches, ({ volume7Days }) => -1 * volume7Days)
matches = sortBy(matches, ({ volume24Hours }) => -1 * volume24Hours) matches = sortBy(matches, ({ volume24Hours }) => -1 * volume24Hours)
} else if (sort === 'highest-percent') {
matches = sortBy(matches, (c) =>
getProbability(c as BinaryContract | PseudoNumericContract)
).reverse()
} }
if (additionalFilter) { if (additionalFilter) {
@ -99,6 +109,7 @@ export default function ContractSearchFirestore(props: {
value={sort} value={sort}
onChange={(e) => setSort(e.target.value)} onChange={(e) => setSort(e.target.value)}
> >
<option value="highest-percent">Highest %</option>
<option value="score">Trending</option> <option value="score">Trending</option>
<option value="newest">Newest</option> <option value="newest">Newest</option>
<option value="most-traded">Most traded</option> <option value="most-traded">Most traded</option>

View File

@ -52,6 +52,8 @@ import { Post } from 'common/post'
import { Spacer } from 'web/components/layout/spacer' import { Spacer } from 'web/components/layout/spacer'
import { usePost } from 'web/hooks/use-post' import { usePost } from 'web/hooks/use-post'
import { useAdmin } from 'web/hooks/use-admin' import { useAdmin } from 'web/hooks/use-admin'
import { isTournament } from 'web/pages/tournaments'
import ContractSearchFirestore from 'web/pages/contract-search-firestore'
export const getStaticProps = fromPropz(getStaticPropz) export const getStaticProps = fromPropz(getStaticPropz)
export async function getStaticPropz(props: { params: { slugs: string[] } }) { export async function getStaticPropz(props: { params: { slugs: string[] } }) {
@ -220,7 +222,13 @@ export default function GroupPage(props: {
</Col> </Col>
) )
const questionsTab = ( // Use Firestore search for Tournament groups, so they can sort by highest
const questionsTab = isTournament(group.id) ? (
<ContractSearchFirestore
additionalFilter={{ groupSlug: group.slug }}
defaultSort="highest-percent"
/>
) : (
<ContractSearch <ContractSearch
user={user} user={user}
defaultSort={'newest'} defaultSort={'newest'}

View File

@ -107,6 +107,10 @@ const tourneys: Tourney[] = [
// }, // },
] ]
export function isTournament(groupId: string) {
return tourneys.map((t) => t.groupId).includes(groupId)
}
export async function getStaticProps() { export async function getStaticProps() {
const groupIds = tourneys const groupIds = tourneys
.map((data) => data.groupId) .map((data) => data.groupId)