manifold/web/pages/home.tsx

92 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-01-23 00:16:23 +00:00
import React from 'react'
import Router from 'next/router'
import _ from 'lodash'
2022-01-23 00:16:23 +00:00
import { Contract, listAllContracts } from '../lib/firebase/contracts'
2022-01-23 00:16:23 +00:00
import { Page } from '../components/page'
import { ActivityFeed, findActiveContracts } from './activity'
import {
getRecentComments,
Comment,
listAllComments,
} from '../lib/firebase/comments'
2022-02-02 06:40:46 +00:00
import { Bet, getRecentBets, listAllBets } from '../lib/firebase/bets'
2022-01-23 00:16:23 +00:00
import FeedCreate from '../components/feed-create'
import { Spacer } from '../components/layout/spacer'
import { Col } from '../components/layout/col'
import { useUser } from '../hooks/use-user'
import { useContracts } from '../hooks/use-contracts'
2022-01-23 00:16:23 +00:00
export async function getStaticProps() {
2022-02-02 06:40:46 +00:00
const [contracts, recentComments, recentBets] = await Promise.all([
listAllContracts().catch((_) => []),
getRecentComments().catch(() => []),
2022-02-02 06:40:46 +00:00
getRecentBets().catch(() => []),
])
2022-01-23 00:16:23 +00:00
2022-02-02 06:40:46 +00:00
const activeContracts = findActiveContracts(
contracts,
recentComments,
recentBets
)
2022-01-23 00:16:23 +00:00
const activeContractBets = await Promise.all(
activeContracts.map((contract) => listAllBets(contract.id).catch((_) => []))
)
const activeContractComments = await Promise.all(
activeContracts.map((contract) =>
listAllComments(contract.id).catch((_) => [])
)
)
return {
props: {
activeContracts,
activeContractBets,
activeContractComments,
},
revalidate: 60, // regenerate after a minute
}
}
const Home = (props: {
activeContracts: Contract[]
activeContractBets: Bet[][]
activeContractComments: Comment[][]
}) => {
const { activeContracts, activeContractBets, activeContractComments } = props
2022-01-23 00:16:23 +00:00
const user = useUser()
const contracts = useContracts() ?? activeContracts
const contractsMap = _.fromPairs(
contracts.map((contract) => [contract.id, contract])
)
const updatedContracts = activeContracts.map(
(contract) => contractsMap[contract.id]
)
2022-01-23 00:26:56 +00:00
2022-01-23 00:16:23 +00:00
if (user === null) {
Router.replace('/')
return <></>
}
return (
<Page assertUser="signed-in">
2022-01-23 00:16:23 +00:00
<Col className="items-center">
<Col className="max-w-3xl">
<FeedCreate user={user ?? undefined} />
<Spacer h={4} />
<ActivityFeed
contracts={updatedContracts}
contractBets={activeContractBets}
contractComments={activeContractComments}
/>
2022-01-23 00:16:23 +00:00
</Col>
</Col>
</Page>
)
}
export default Home