Add closing soon section to home

This commit is contained in:
jahooma 2022-01-17 16:54:00 -06:00
parent 56c7e2597d
commit 4d975ff512
4 changed files with 86 additions and 40 deletions

View File

@ -12,15 +12,16 @@ import {
import { Col } from './layout/col' import { Col } from './layout/col'
import { parseTags } from '../lib/util/parse' import { parseTags } from '../lib/util/parse'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { TrendingUpIcon } from '@heroicons/react/solid' import { TrendingUpIcon, ClockIcon } from '@heroicons/react/solid'
import { DateTimeTooltip } from './datetime-tooltip' import { DateTimeTooltip } from './datetime-tooltip'
export function ContractCard(props: { export function ContractCard(props: {
contract: Contract contract: Contract
showHotVolume?: boolean showHotVolume?: boolean
showCloseTime?: boolean
className?: string className?: string
}) { }) {
const { contract, showHotVolume, className } = props const { contract, showHotVolume, showCloseTime, className } = props
const { question, resolution } = contract const { question, resolution } = contract
const { probPercent } = contractMetrics(contract) const { probPercent } = contractMetrics(contract)
@ -42,7 +43,11 @@ export function ContractCard(props: {
probPercent={probPercent} probPercent={probPercent}
/> />
</Row> </Row>
<AbbrContractDetails contract={contract} showHotVolume={showHotVolume} /> <AbbrContractDetails
contract={contract}
showHotVolume={showHotVolume}
showCloseTime={showCloseTime}
/>
</div> </div>
) )
} }
@ -99,9 +104,10 @@ export function ResolutionOrChance(props: {
export function AbbrContractDetails(props: { export function AbbrContractDetails(props: {
contract: Contract contract: Contract
showHotVolume?: boolean showHotVolume?: boolean
showCloseTime?: boolean
}) { }) {
const { contract, showHotVolume } = props const { contract, showHotVolume, showCloseTime } = props
const { volume24Hours, creatorName, creatorUsername } = contract const { volume24Hours, creatorName, creatorUsername, closeTime } = contract
const { truePool } = contractMetrics(contract) const { truePool } = contractMetrics(contract)
return ( return (
@ -118,6 +124,11 @@ export function AbbrContractDetails(props: {
<TrendingUpIcon className="h-5 w-5 text-gray-500 inline" />{' '} <TrendingUpIcon className="h-5 w-5 text-gray-500 inline" />{' '}
{formatMoney(volume24Hours)} {formatMoney(volume24Hours)}
</div> </div>
) : showCloseTime ? (
<div className="whitespace-nowrap">
<ClockIcon className="h-5 w-5 text-gray-400 inline" />{' '}
{dayjs(closeTime).format('MMM D')}
</div>
) : ( ) : (
<div className="whitespace-nowrap">{formatMoney(truePool)} pool</div> <div className="whitespace-nowrap">{formatMoney(truePool)} pool</div>
)} )}

View File

@ -18,8 +18,9 @@ import { Sort, useQueryAndSortParams } from '../hooks/use-sort-and-query-params'
export function ContractsGrid(props: { export function ContractsGrid(props: {
contracts: Contract[] contracts: Contract[]
showHotVolume?: boolean showHotVolume?: boolean
showCloseTime?: boolean
}) { }) {
const { showHotVolume } = props const { showHotVolume, showCloseTime } = props
const [resolvedContracts, activeContracts] = _.partition( const [resolvedContracts, activeContracts] = _.partition(
props.contracts, props.contracts,
@ -51,6 +52,7 @@ export function ContractsGrid(props: {
contract={contract} contract={contract}
key={contract.id} key={contract.id}
showHotVolume={showHotVolume} showHotVolume={showHotVolume}
showCloseTime={showCloseTime}
/> />
))} ))}
</ul> </ul>

View File

@ -14,6 +14,7 @@ import {
updateDoc, updateDoc,
limit, limit,
} from 'firebase/firestore' } from 'firebase/firestore'
import _ from 'lodash'
import { app } from './init' import { app } from './init'
import { getValues, listenForValues } from './utils' import { getValues, listenForValues } from './utils'
@ -123,6 +124,13 @@ export function listenForContract(
}) })
} }
function chooseRandomSubset(contracts: Contract[], count: number) {
const fiveMinutes = 5 * 60 * 1000
const seed = Math.round(Date.now() / fiveMinutes).toString()
shuffle(contracts, createRNG(seed))
return contracts.slice(0, count)
}
const hotContractsQuery = query( const hotContractsQuery = query(
contractCollection, contractCollection,
where('isResolved', '==', false), where('isResolved', '==', false),
@ -131,21 +139,38 @@ const hotContractsQuery = query(
limit(16) limit(16)
) )
function chooseHotContracts(contracts: Contract[]) {
const fiveMinutes = 5 * 60 * 1000
const seed = Math.round(Date.now() / fiveMinutes).toString()
shuffle(contracts, createRNG(seed))
return contracts.slice(0, 4)
}
export function listenForHotContracts( export function listenForHotContracts(
setHotContracts: (contracts: Contract[]) => void setHotContracts: (contracts: Contract[]) => void
) { ) {
return listenForValues<Contract>(hotContractsQuery, (contracts) => return listenForValues<Contract>(hotContractsQuery, (contracts) => {
setHotContracts(chooseHotContracts(contracts)) const hotContracts = _.sortBy(
chooseRandomSubset(contracts, 4),
(contract) => contract.volume24Hours
)
setHotContracts(hotContracts)
})
}
export async function getHotContracts() {
const contracts = await getValues<Contract>(hotContractsQuery)
return _.sortBy(
chooseRandomSubset(contracts, 4),
(contract) => -1 * contract.volume24Hours
) )
} }
export function getHotContracts() { const closingSoonQuery = query(
return getValues<Contract>(hotContractsQuery).then(chooseHotContracts) contractCollection,
where('isResolved', '==', false),
where('closeTime', '>', Date.now()),
orderBy('closeTime', 'asc'),
limit(6)
)
export async function getClosingSoonContracts() {
const contracts = await getValues<Contract>(closingSoonQuery)
return _.sortBy(
chooseRandomSubset(contracts, 2),
(contract) => contract.closeTime
)
} }

View File

@ -2,6 +2,7 @@ import React from 'react'
import _ from 'lodash' import _ from 'lodash'
import { import {
Contract, Contract,
getClosingSoonContracts,
getHotContracts, getHotContracts,
listAllContracts, listAllContracts,
} from '../lib/firebase/contracts' } from '../lib/firebase/contracts'
@ -14,14 +15,15 @@ import {
Comment, Comment,
listAllComments, listAllComments,
} from '../lib/firebase/comments' } from '../lib/firebase/comments'
import { Col } from '../components/layout/col'
import { ContractCard } from '../components/contract-card'
import { Bet, listAllBets } from '../lib/firebase/bets' import { Bet, listAllBets } from '../lib/firebase/bets'
import { ContractsGrid } from '../components/contracts-list'
export async function getStaticProps() { export async function getStaticProps() {
const [contracts, hotContracts, recentComments] = await Promise.all([ const [contracts, hotContracts, closingSoonContracts, recentComments] =
await Promise.all([
listAllContracts().catch((_) => []), listAllContracts().catch((_) => []),
getHotContracts().catch(() => []), getHotContracts().catch(() => []),
getClosingSoonContracts().catch(() => []),
getRecentComments().catch(() => []), getRecentComments().catch(() => []),
]) ])
@ -39,6 +41,7 @@ export async function getStaticProps() {
activeContractBets, activeContractBets,
activeContractComments, activeContractComments,
hotContracts, hotContracts,
closingSoonContracts,
}, },
revalidate: 60, // regenerate after a minute revalidate: 60, // regenerate after a minute
@ -50,17 +53,21 @@ const Home = (props: {
activeContractBets: Bet[][] activeContractBets: Bet[][]
activeContractComments: Comment[][] activeContractComments: Comment[][]
hotContracts: Contract[] hotContracts: Contract[]
closingSoonContracts: Contract[]
}) => { }) => {
const { const {
activeContracts, activeContracts,
activeContractBets, activeContractBets,
activeContractComments, activeContractComments,
hotContracts, hotContracts,
closingSoonContracts,
} = props } = props
return ( return (
<Page> <Page>
<HotMarkets hotContracts={hotContracts} /> <HotMarkets contracts={hotContracts} />
<Spacer h={10} />
<ClosingSoonMarkets contracts={closingSoonContracts} />
<Spacer h={10} /> <Spacer h={10} />
<ActivityFeed <ActivityFeed
contracts={activeContracts} contracts={activeContracts}
@ -71,25 +78,26 @@ const Home = (props: {
) )
} }
const HotMarkets = (props: { hotContracts: Contract[] }) => { const HotMarkets = (props: { contracts: Contract[] }) => {
const { hotContracts } = props const { contracts } = props
if (hotContracts.length < 4) return <></> if (contracts.length === 0) return <></>
const [c1, c2, c3, c4] = hotContracts
return ( return (
<div className="w-full bg-indigo-50 border-2 border-indigo-100 p-6 rounded-lg shadow-md"> <div className="w-full bg-indigo-50 border-2 border-indigo-100 p-6 rounded-lg shadow-md">
<Title className="mt-0" text="🔥 Markets" /> <Title className="mt-0" text="🔥 Markets" />
<Col className="gap-6"> <ContractsGrid contracts={contracts} showHotVolume />
<Col className="md:flex-row items-start gap-6"> </div>
<ContractCard className="flex-1" contract={c1} showHotVolume /> )
<ContractCard className="flex-1" contract={c2} showHotVolume /> }
</Col>
<Col className="md:flex-row items-start gap-6"> const ClosingSoonMarkets = (props: { contracts: Contract[] }) => {
<ContractCard className="flex-1" contract={c3} showHotVolume /> const { contracts } = props
<ContractCard className="flex-1" contract={c4} showHotVolume /> if (contracts.length === 0) return <></>
</Col>
</Col> return (
<div className="w-full bg-green-50 border-2 border-green-100 p-6 rounded-lg shadow-md">
<Title className="mt-0" text="⏰ Closing soon" />
<ContractsGrid contracts={contracts} showCloseTime />
</div> </div>
) )
} }