manifold/web/pages/index.tsx

87 lines
2.2 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,
getHotContracts,
listAllContracts,
} from '../lib/firebase/contracts'
import { Page } from '../components/page'
import { ActivityFeed, findActiveContracts } from './activity'
import {
getRecentComments,
Comment,
listAllComments,
} from '../lib/firebase/comments'
import { Bet, listAllBets } from '../lib/firebase/bets'
import FeedCreate, { FeedPromo } from '../components/feed-create'
import { Spacer } from '../components/layout/spacer'
import { Col } from '../components/layout/col'
import { useUser } from '../hooks/use-user'
2021-12-19 05:59:34 +00:00
export async function getStaticProps() {
const [contracts, recentComments, hotContracts] = await Promise.all([
listAllContracts().catch((_) => []),
getRecentComments().catch(() => []),
getHotContracts().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,
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[]
}) => {
const {
activeContracts,
activeContractBets,
activeContractComments,
hotContracts,
} = props
const user = useUser()
return (
<Page>
<Col className="items-center">
<Col className="max-w-3xl">
<div className="-mx-2 sm:mx-0">
{user ? (
<FeedCreate user={user} />
) : (
<FeedPromo hotContracts={hotContracts} />
)}
<Spacer h={4} />
<ActivityFeed
contracts={activeContracts}
contractBets={activeContractBets}
contractComments={activeContractComments}
/>
</div>
</Col>
</Col>
</Page>
2022-01-05 06:32:52 +00:00
)
}
export default Home