2022-01-23 00:16:23 +00:00
|
|
|
import React from 'react'
|
|
|
|
import Router from 'next/router'
|
2022-01-31 04:03:20 +00:00
|
|
|
import _ from 'lodash'
|
2022-01-23 00:16:23 +00:00
|
|
|
|
2022-01-31 04:03:20 +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'
|
2022-01-31 04:03:20 +00:00
|
|
|
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([
|
2022-01-31 04:03:20 +00:00
|
|
|
listAllContracts().catch((_) => []),
|
|
|
|
getRecentComments().catch(() => []),
|
2022-02-02 06:40:46 +00:00
|
|
|
getRecentBets().catch(() => []),
|
2022-01-31 04:03:20 +00:00
|
|
|
])
|
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[][]
|
|
|
|
}) => {
|
2022-01-31 04:03:20 +00:00
|
|
|
const { activeContracts, activeContractBets, activeContractComments } = props
|
2022-01-23 00:16:23 +00:00
|
|
|
|
|
|
|
const user = useUser()
|
|
|
|
|
2022-01-31 04:03:20 +00:00
|
|
|
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 (
|
2022-01-27 23:06:31 +00:00
|
|
|
<Page assertUser="signed-in">
|
2022-01-23 00:16:23 +00:00
|
|
|
<Col className="items-center">
|
|
|
|
<Col className="max-w-3xl">
|
2022-01-27 22:37:43 +00:00
|
|
|
<FeedCreate user={user ?? undefined} />
|
|
|
|
<Spacer h={4} />
|
|
|
|
<ActivityFeed
|
2022-01-31 04:03:20 +00:00
|
|
|
contracts={updatedContracts}
|
|
|
|
contractBets={activeContractBets}
|
|
|
|
contractComments={activeContractComments}
|
2022-01-27 22:37:43 +00:00
|
|
|
/>
|
2022-01-23 00:16:23 +00:00
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Home
|