Better recommended contracts. Include from first group.

This commit is contained in:
James Grugett 2022-08-27 22:26:37 -05:00
parent e7f369e2b4
commit cb08a114ae
2 changed files with 49 additions and 14 deletions

View File

@ -12,7 +12,7 @@ import {
updateDoc, updateDoc,
where, where,
} from 'firebase/firestore' } from 'firebase/firestore'
import { sortBy, sum } from 'lodash' import { sortBy, sum, uniqBy } from 'lodash'
import { coll, getValues, listenForValue, listenForValues } from './utils' import { coll, getValues, listenForValue, listenForValues } from './utils'
import { BinaryContract, Contract } from 'common/contract' import { BinaryContract, Contract } from 'common/contract'
@ -305,7 +305,7 @@ export const getRandTopCreatorContracts = async (
where('isResolved', '==', false), where('isResolved', '==', false),
where('creatorId', '==', creatorId), where('creatorId', '==', creatorId),
orderBy('popularityScore', 'desc'), orderBy('popularityScore', 'desc'),
limit(Math.max(count * 2, 15)) limit(count * 2)
) )
const data = await getValues<Contract>(creatorContractsQuery) const data = await getValues<Contract>(creatorContractsQuery)
const open = data const open = data
@ -315,6 +315,44 @@ export const getRandTopCreatorContracts = async (
return chooseRandomSubset(open, count) return chooseRandomSubset(open, count)
} }
export const getRandTopGroupContracts = async (
groupSlug: string,
count: number,
excluding: string[] = []
) => {
const creatorContractsQuery = query(
contracts,
where('groupSlugs', 'array-contains', groupSlug),
where('isResolved', '==', false),
orderBy('popularityScore', 'desc'),
limit(count * 2)
)
const data = await getValues<Contract>(creatorContractsQuery)
const open = data
.filter((c) => c.closeTime && c.closeTime > Date.now())
.filter((c) => !excluding.includes(c.id))
return chooseRandomSubset(open, count)
}
export const getRecommendedContracts = async (
contract: Contract,
count: number
) => {
const { creatorId, groupSlugs, id } = contract
const [userContracts, groupContracts] = await Promise.all([
getRandTopCreatorContracts(creatorId, count, [id]),
groupSlugs && groupSlugs[0]
? getRandTopGroupContracts(groupSlugs[0], count, [id])
: [],
])
const combined = uniqBy([...userContracts, ...groupContracts], (c) => c.id)
return chooseRandomSubset(combined, count)
}
export async function getRecentBetsAndComments(contract: Contract) { export async function getRecentBetsAndComments(contract: Contract) {
const contractDoc = doc(contracts, contract.id) const contractDoc = doc(contracts, contract.id)

View File

@ -11,7 +11,7 @@ import { Spacer } from 'web/components/layout/spacer'
import { import {
Contract, Contract,
getContractFromSlug, getContractFromSlug,
getRandTopCreatorContracts, getRecommendedContracts,
tradingAllowed, tradingAllowed,
} from 'web/lib/firebase/contracts' } from 'web/lib/firebase/contracts'
import { SEO } from 'web/components/SEO' import { SEO } from 'web/components/SEO'
@ -40,8 +40,8 @@ import {
ContractLeaderboard, ContractLeaderboard,
ContractTopTrades, ContractTopTrades,
} from 'web/components/contract/contract-leaderboard' } from 'web/components/contract/contract-leaderboard'
import { Subtitle } from 'web/components/subtitle'
import { ContractsGrid } from 'web/components/contract/contracts-grid' import { ContractsGrid } from 'web/components/contract/contracts-grid'
import { Title } from 'web/components/title'
export const getStaticProps = fromPropz(getStaticPropz) export const getStaticProps = fromPropz(getStaticPropz)
export async function getStaticPropz(props: { export async function getStaticPropz(props: {
@ -54,9 +54,7 @@ export async function getStaticPropz(props: {
const [bets, comments, recommendedContracts] = await Promise.all([ const [bets, comments, recommendedContracts] = await Promise.all([
contractId ? listAllBets(contractId) : [], contractId ? listAllBets(contractId) : [],
contractId ? listAllComments(contractId) : [], contractId ? listAllComments(contractId) : [],
contract contract ? getRecommendedContracts(contract, 6) : [],
? getRandTopCreatorContracts(contract.creatorId, 4, [contract?.id])
: [],
]) ])
return { return {
@ -190,12 +188,11 @@ export function ContractPageContent(
props.recommendedContracts props.recommendedContracts
) )
useEffect(() => { useEffect(() => {
if (recommendedContracts.length === 0) { if (contract && recommendedContracts.length === 0) {
getRandTopCreatorContracts(contract.creatorId, 4, [contract.id]).then( getRecommendedContracts(contract, 6).then(setRecommendedMarkets)
setRecommendedMarkets
)
} }
}, [contract.id, contract.creatorId, recommendedContracts]) // eslint-disable-next-line react-hooks/exhaustive-deps
}, [contract.id, recommendedContracts])
const { isResolved, question, outcomeType } = contract const { isResolved, question, outcomeType } = contract
@ -282,8 +279,8 @@ export function ContractPageContent(
</Col> </Col>
{recommendedContracts.length > 0 && ( {recommendedContracts.length > 0 && (
<Col className="gap-2 px-2 sm:px-0"> <Col className="mt-2 gap-2 px-2 sm:px-0">
<Subtitle text="Recommended" /> <Title className="text-gray-700" text="Recommended" />
<ContractsGrid contracts={recommendedContracts} /> <ContractsGrid contracts={recommendedContracts} />
</Col> </Col>
)} )}