manifold/web/pages/index.tsx

106 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-12-03 01:18:00 +00:00
import React from 'react'
import _ from 'lodash'
2022-01-05 06:32:52 +00:00
import {
Contract,
2022-01-17 22:54:00 +00:00
getClosingSoonContracts,
2022-01-05 06:32:52 +00:00
getHotContracts,
listAllContracts,
} from '../lib/firebase/contracts'
import { Spacer } from '../components/layout/spacer'
import { Page } from '../components/page'
import { Title } from '../components/title'
import { ActivityFeed, findActiveContracts } from './activity'
import {
getRecentComments,
Comment,
listAllComments,
} from '../lib/firebase/comments'
import { Bet, listAllBets } from '../lib/firebase/bets'
2022-01-17 22:54:00 +00:00
import { ContractsGrid } from '../components/contracts-list'
2021-12-19 05:59:34 +00:00
export async function getStaticProps() {
2022-01-17 22:54:00 +00:00
const [contracts, hotContracts, closingSoonContracts, recentComments] =
await Promise.all([
listAllContracts().catch((_) => []),
getHotContracts().catch(() => []),
getClosingSoonContracts().catch(() => []),
getRecentComments().catch(() => []),
])
2021-12-19 05:59:34 +00:00
const activeContracts = findActiveContracts(contracts, recentComments)
const activeContractBets = await Promise.all(
activeContracts.map((contract) => listAllBets(contract.id))
)
const activeContractComments = await Promise.all(
activeContracts.map((contract) => listAllComments(contract.id))
)
2021-12-19 05:59:34 +00:00
return {
props: {
activeContracts,
activeContractBets,
activeContractComments,
hotContracts,
2022-01-17 22:54:00 +00:00
closingSoonContracts,
2021-12-19 05:59:34 +00:00
},
revalidate: 60, // regenerate after a minute
}
}
const Home = (props: {
activeContracts: Contract[]
activeContractBets: Bet[][]
activeContractComments: Comment[][]
hotContracts: Contract[]
2022-01-17 22:54:00 +00:00
closingSoonContracts: Contract[]
}) => {
const {
activeContracts,
activeContractBets,
activeContractComments,
hotContracts,
2022-01-17 22:54:00 +00:00
closingSoonContracts,
} = props
return (
<Page>
2022-01-17 22:54:00 +00:00
<HotMarkets contracts={hotContracts} />
<Spacer h={10} />
<ClosingSoonMarkets contracts={closingSoonContracts} />
<Spacer h={10} />
<ActivityFeed
contracts={activeContracts}
contractBets={activeContractBets}
contractComments={activeContractComments}
/>
</Page>
2022-01-05 06:32:52 +00:00
)
}
2022-01-17 22:54:00 +00:00
const HotMarkets = (props: { contracts: Contract[] }) => {
const { contracts } = props
if (contracts.length === 0) return <></>
return (
<div className="w-full bg-indigo-50 border-2 border-indigo-100 p-6 rounded-lg shadow-md">
<Title className="mt-0" text="🔥 Markets" />
2022-01-17 22:54:00 +00:00
<ContractsGrid contracts={contracts} showHotVolume />
</div>
)
}
const ClosingSoonMarkets = (props: { contracts: Contract[] }) => {
const { contracts } = props
if (contracts.length === 0) return <></>
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>
)
}
export default Home