manifold/web/pages/index.tsx

111 lines
3.1 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,
getClosingSoonContracts,
2022-01-05 06:32:52 +00:00
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 { useContracts } from '../hooks/use-contracts'
import { useRecentComments } from '../hooks/use-comments'
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'
import { ClosingSoonMarkets, HotMarkets } from './markets'
2021-12-19 05:59:34 +00:00
export async function getStaticProps() {
const [contracts, recentComments, hotContracts, closingSoonContracts] =
await Promise.all([
listAllContracts().catch((_) => []),
getRecentComments().catch(() => []),
getHotContracts().catch(() => []),
getClosingSoonContracts().catch(() => []),
])
2021-12-19 05:59:34 +00:00
const activeContracts = findActiveContracts(contracts, recentComments)
const activeContractBets = await Promise.all(
2022-01-22 21:21:52 +00:00
activeContracts.map((contract) => listAllBets(contract.id).catch((_) => []))
)
const activeContractComments = await Promise.all(
2022-01-22 21:21:52 +00:00
activeContracts.map((contract) =>
listAllComments(contract.id).catch((_) => [])
)
)
2021-12-19 05:59:34 +00:00
return {
props: {
activeContracts,
activeContractBets,
activeContractComments,
hotContracts,
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[]
closingSoonContracts: Contract[]
}) => {
const {
activeContractBets,
activeContractComments,
hotContracts,
closingSoonContracts,
} = props
const contracts = useContracts() ?? props.activeContracts
const recentComments = useRecentComments()
const activeContracts = recentComments
? findActiveContracts(contracts, recentComments)
: props.activeContracts
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} />
<Spacer h={4} />
<HotMarkets contracts={hotContracts.slice(0, 4)} />
<Spacer h={4} />
<ClosingSoonMarkets contracts={closingSoonContracts} />
<Spacer h={10} />
<ActivityFeed
contracts={activeContracts}
contractBets={activeContractBets}
contractComments={activeContractComments}
/>
</>
) : (
<>
<FeedPromo hotContracts={hotContracts} />
</>
)}
</div>
</Col>
</Col>
</Page>
2022-01-05 06:32:52 +00:00
)
}
export default Home