Support Firestore search over a reduced set of contracts

This commit is contained in:
Austin Chen 2022-09-02 18:50:17 -07:00
parent 9a101601fb
commit 2b71e299d3
2 changed files with 23 additions and 4 deletions

View File

@ -3,13 +3,18 @@ import { Answer } from 'common/answer'
import { searchInAny } from 'common/util/parse' import { searchInAny } from 'common/util/parse'
import { sortBy } from 'lodash' import { sortBy } from 'lodash'
import { ContractsGrid } from 'web/components/contract/contracts-grid' import { ContractsGrid } from 'web/components/contract/contracts-grid'
import { useContracts } from 'web/hooks/use-contracts'
import { import {
usePersistentState, usePersistentState,
urlParamStore, urlParamStore,
} from 'web/hooks/use-persistent-state' } from 'web/hooks/use-persistent-state'
import { getProbability } from 'common/calculate' import { getProbability } from 'common/calculate'
import { BinaryContract, PseudoNumericContract } from 'common/contract' import {
BinaryContract,
Contract,
PseudoNumericContract,
} from 'common/contract'
import { useEffect, useState } from 'react'
import { listAllContracts } from 'web/lib/firebase/contracts'
const MAX_CONTRACTS_RENDERED = 100 const MAX_CONTRACTS_RENDERED = 100
@ -21,9 +26,10 @@ export default function ContractSearchFirestore(props: {
groupSlug?: string groupSlug?: string
} }
defaultSort?: string defaultSort?: string
getContracts?: () => Promise<Contract[]>
}) { }) {
const { additionalFilter } = props const { additionalFilter, getContracts } = props
const contracts = useContracts() const [contracts, setContracts] = useState<Contract[]>([])
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 })
@ -32,6 +38,15 @@ export default function ContractSearchFirestore(props: {
store, store,
}) })
useEffect(() => {
// Either set contracts from getContracts, or fallback to getting all of them
if (getContracts) {
getContracts().then(setContracts)
} else {
listAllContracts(100000).then(setContracts)
}
}, [getContracts])
let matches = (contracts ?? []).filter((c) => let matches = (contracts ?? []).filter((c) =>
searchInAny( searchInAny(
query, query,

View File

@ -227,6 +227,10 @@ export default function GroupPage(props: {
<ContractSearchFirestore <ContractSearchFirestore
additionalFilter={{ groupSlug: group.slug }} additionalFilter={{ groupSlug: group.slug }}
defaultSort="highest-percent" defaultSort="highest-percent"
getContracts={async () => {
const contracts = await listContractsByGroupSlug(group.slug)
return contracts.filter((c) => !c.isResolved)
}}
/> />
) : ( ) : (
<ContractSearch <ContractSearch