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,
urlParamStore,
} from 'web/hooks/use-persistent-state'
import { getProbability } from 'common/calculate'
import { BinaryContract, PseudoNumericContract } from 'common/contract'
const MAX_CONTRACTS_RENDERED = 100
@ -18,13 +20,17 @@ export default function ContractSearchFirestore(props: {
excludeContractIds?: string[]
groupSlug?: string
}
defaultSort?: string
}) {
const { additionalFilter } = props
const contracts = useContracts()
const router = useRouter()
const store = urlParamStore(router)
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) =>
searchInAny(
@ -51,6 +57,10 @@ export default function ContractSearchFirestore(props: {
// Use lodash for stable sort, so previous sort breaks all ties.
matches = sortBy(matches, ({ volume7Days }) => -1 * volume7Days)
matches = sortBy(matches, ({ volume24Hours }) => -1 * volume24Hours)
} else if (sort === 'highest-percent') {
matches = sortBy(matches, (c) =>
getProbability(c as BinaryContract | PseudoNumericContract)
).reverse()
}
if (additionalFilter) {
@ -99,6 +109,7 @@ export default function ContractSearchFirestore(props: {
value={sort}
onChange={(e) => setSort(e.target.value)}
>
<option value="highest-percent">Highest %</option>
<option value="score">Trending</option>
<option value="newest">Newest</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 { usePost } from 'web/hooks/use-post'
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 async function getStaticPropz(props: { params: { slugs: string[] } }) {
@ -220,7 +222,13 @@ export default function GroupPage(props: {
</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
user={user}
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() {
const groupIds = tourneys
.map((data) => data.groupId)