Simple recommended contracts based on contract creator

This commit is contained in:
James Grugett 2022-08-24 12:59:21 -05:00 committed by mantikoros
parent 0743d4ae1c
commit 2fcfeb786a
2 changed files with 38 additions and 2 deletions

View File

@ -295,6 +295,26 @@ export async function getClosingSoonContracts() {
return sortBy(chooseRandomSubset(data, 2), (contract) => contract.closeTime) return sortBy(chooseRandomSubset(data, 2), (contract) => contract.closeTime)
} }
export const getRandTopCreatorContracts = async (
creatorId: string,
count: number,
excluding: string[] = []
) => {
const creatorContractsQuery = query(
contracts,
where('isResolved', '==', false),
where('creatorId', '==', creatorId),
orderBy('popularityScore', 'desc'),
limit(Math.max(count * 2, 15))
)
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 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,6 +11,7 @@ import { Spacer } from 'web/components/layout/spacer'
import { import {
Contract, Contract,
getContractFromSlug, getContractFromSlug,
getRandTopCreatorContracts,
tradingAllowed, tradingAllowed,
} from 'web/lib/firebase/contracts' } from 'web/lib/firebase/contracts'
import { SEO } from 'web/components/SEO' import { SEO } from 'web/components/SEO'
@ -39,6 +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'
export const getStaticProps = fromPropz(getStaticPropz) export const getStaticProps = fromPropz(getStaticPropz)
export async function getStaticPropz(props: { export async function getStaticPropz(props: {
@ -48,9 +51,12 @@ export async function getStaticPropz(props: {
const contract = (await getContractFromSlug(contractSlug)) || null const contract = (await getContractFromSlug(contractSlug)) || null
const contractId = contract?.id const contractId = contract?.id
const [bets, comments] = await Promise.all([ const [bets, comments, recommendedContracts] = await Promise.all([
contractId ? listAllBets(contractId) : [], contractId ? listAllBets(contractId) : [],
contractId ? listAllComments(contractId) : [], contractId ? listAllComments(contractId) : [],
contract
? getRandTopCreatorContracts(contract.creatorId, 4, [contract?.id])
: [],
]) ])
return { return {
@ -61,6 +67,7 @@ export async function getStaticPropz(props: {
// Limit the data sent to the client. Client will still load all bets and comments directly. // Limit the data sent to the client. Client will still load all bets and comments directly.
bets: bets.slice(0, 5000), bets: bets.slice(0, 5000),
comments: comments.slice(0, 1000), comments: comments.slice(0, 1000),
recommendedContracts,
}, },
revalidate: 60, // regenerate after a minute revalidate: 60, // regenerate after a minute
@ -77,6 +84,7 @@ export default function ContractPage(props: {
bets: Bet[] bets: Bet[]
comments: ContractComment[] comments: ContractComment[]
slug: string slug: string
recommendedContracts: Contract[]
backToHome?: () => void backToHome?: () => void
}) { }) {
props = usePropz(props, getStaticPropz) ?? { props = usePropz(props, getStaticPropz) ?? {
@ -84,6 +92,7 @@ export default function ContractPage(props: {
username: '', username: '',
comments: [], comments: [],
bets: [], bets: [],
recommendedContracts: [],
slug: '', slug: '',
} }
@ -145,7 +154,7 @@ export function ContractPageContent(
user?: User | null user?: User | null
} }
) { ) {
const { backToHome, comments, user } = props const { backToHome, comments, user, recommendedContracts } = props
const contract = useContractWithPreload(props.contract) ?? props.contract const contract = useContractWithPreload(props.contract) ?? props.contract
@ -258,6 +267,13 @@ export function ContractPageContent(
tips={tips} tips={tips}
comments={comments} comments={comments}
/> />
{recommendedContracts?.length > 0 && (
<Col className="mx-2 gap-2 sm:mx-0">
<Subtitle text="Recommended" />
<ContractsGrid contracts={recommendedContracts} />
</Col>
)}
</Col> </Col>
</Page> </Page>
) )