2022-02-05 02:09:34 +00:00
|
|
|
import React from 'react'
|
2022-01-23 00:16:23 +00:00
|
|
|
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'
|
2022-02-18 18:43:13 +00:00
|
|
|
import { Comment, getRecentComments } from '../lib/firebase/comments'
|
|
|
|
import { Bet, getRecentBets } 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-02-05 21:56:42 +00:00
|
|
|
import { useUpdatedContracts } from '../hooks/use-contracts'
|
2022-02-04 23:24:54 +00:00
|
|
|
import { listAllFolds } from '../lib/firebase/folds'
|
2022-02-03 22:54:08 +00:00
|
|
|
import { Fold } from '../../common/fold'
|
|
|
|
import { filterDefined } from '../../common/util/array'
|
2022-02-04 23:24:54 +00:00
|
|
|
import { useUserBetContracts } from '../hooks/use-user-bets'
|
2022-02-03 23:12:09 +00:00
|
|
|
import { LoadingIndicator } from '../components/loading-indicator'
|
2022-02-04 18:30:13 +00:00
|
|
|
import { Row } from '../components/layout/row'
|
|
|
|
import { SparklesIcon } from '@heroicons/react/solid'
|
2022-02-04 23:24:54 +00:00
|
|
|
import { useFollowedFolds } from '../hooks/use-fold'
|
2022-02-19 23:17:36 +00:00
|
|
|
import { FastFoldFollowing } from '../components/fast-fold-following'
|
2022-01-23 00:16:23 +00:00
|
|
|
|
|
|
|
export async function getStaticProps() {
|
2022-02-17 07:02:48 +00:00
|
|
|
let [contracts, folds] = await Promise.all([
|
2022-01-31 04:03:20 +00:00
|
|
|
listAllContracts().catch((_) => []),
|
2022-02-03 22:54:08 +00:00
|
|
|
listAllFolds().catch(() => []),
|
2022-01-31 04:03:20 +00:00
|
|
|
])
|
2022-01-23 00:16:23 +00:00
|
|
|
|
2022-02-18 18:43:13 +00:00
|
|
|
const [recentBets, recentComments] = await Promise.all([
|
|
|
|
getRecentBets(),
|
|
|
|
getRecentComments(),
|
2022-02-05 02:09:34 +00:00
|
|
|
])
|
|
|
|
|
2022-01-23 00:16:23 +00:00
|
|
|
return {
|
|
|
|
props: {
|
2022-02-03 22:54:08 +00:00
|
|
|
contracts,
|
2022-02-18 18:43:13 +00:00
|
|
|
recentBets,
|
|
|
|
recentComments,
|
2022-02-03 22:54:08 +00:00
|
|
|
folds,
|
2022-01-23 00:16:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
revalidate: 60, // regenerate after a minute
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-05 02:09:34 +00:00
|
|
|
const Home = (props: {
|
|
|
|
contracts: Contract[]
|
|
|
|
folds: Fold[]
|
2022-02-18 18:43:13 +00:00
|
|
|
recentBets: Bet[]
|
|
|
|
recentComments: Comment[]
|
2022-02-05 02:09:34 +00:00
|
|
|
}) => {
|
2022-02-18 18:43:13 +00:00
|
|
|
const { folds, recentBets, recentComments } = props
|
2022-01-23 00:16:23 +00:00
|
|
|
|
|
|
|
const user = useUser()
|
|
|
|
|
2022-02-05 21:56:42 +00:00
|
|
|
const contracts = useUpdatedContracts(props.contracts)
|
2022-02-03 22:54:08 +00:00
|
|
|
|
2022-02-07 02:03:42 +00:00
|
|
|
const followedFoldIds = useFollowedFolds(user)
|
2022-02-03 22:54:08 +00:00
|
|
|
const followedFolds = filterDefined(
|
2022-02-07 02:03:42 +00:00
|
|
|
(followedFoldIds ?? []).map((id) => folds.find((fold) => fold.id === id))
|
2022-02-03 22:54:08 +00:00
|
|
|
)
|
|
|
|
const tagSet = new Set(
|
|
|
|
_.flatten(followedFolds.map((fold) => fold.lowercaseTags))
|
2022-01-31 04:03:20 +00:00
|
|
|
)
|
2022-02-03 22:54:08 +00:00
|
|
|
|
2022-02-04 23:24:54 +00:00
|
|
|
const yourBetContractIds = useUserBetContracts(user?.id)
|
|
|
|
const yourBetContracts = yourBetContractIds
|
|
|
|
? new Set(yourBetContractIds)
|
|
|
|
: undefined
|
2022-01-23 00:26:56 +00:00
|
|
|
|
2022-02-07 02:03:42 +00:00
|
|
|
// Show no contracts before your info is loaded.
|
|
|
|
let feedContracts: Contract[] = []
|
|
|
|
if (yourBetContracts && followedFoldIds) {
|
|
|
|
// Show all contracts if no folds are followed.
|
|
|
|
if (followedFoldIds.length === 0) feedContracts = contracts
|
|
|
|
else
|
|
|
|
feedContracts = contracts.filter(
|
|
|
|
(contract) =>
|
|
|
|
contract.lowercaseTags.some((tag) => tagSet.has(tag)) ||
|
|
|
|
yourBetContracts.has(contract.id)
|
|
|
|
)
|
2022-02-07 00:03:36 +00:00
|
|
|
}
|
2022-02-03 22:54:08 +00:00
|
|
|
|
2022-02-18 18:43:13 +00:00
|
|
|
const activeContracts = findActiveContracts(
|
|
|
|
feedContracts,
|
|
|
|
recentComments,
|
|
|
|
recentBets,
|
|
|
|
365
|
|
|
|
)
|
|
|
|
|
|
|
|
const betsByContract = _.groupBy(recentBets, (bet) => bet.contractId)
|
|
|
|
const activeBets = activeContracts.map(
|
|
|
|
(contract) => betsByContract[contract.id] ?? []
|
|
|
|
)
|
|
|
|
|
|
|
|
const commentsByContract = _.groupBy(
|
|
|
|
recentComments,
|
|
|
|
(comment) => comment.contractId
|
|
|
|
)
|
|
|
|
const activeComments = activeContracts.map(
|
|
|
|
(contract) => commentsByContract[contract.id] ?? []
|
|
|
|
)
|
2022-02-03 22:54:08 +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">
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className="w-full max-w-3xl">
|
2022-01-27 22:37:43 +00:00
|
|
|
<FeedCreate user={user ?? undefined} />
|
2022-02-03 23:40:37 +00:00
|
|
|
<Spacer h={6} />
|
2022-02-19 23:17:36 +00:00
|
|
|
|
2022-02-19 23:26:49 +00:00
|
|
|
{followedFoldIds !== undefined && followedFolds.length === 0 && (
|
2022-02-19 23:17:36 +00:00
|
|
|
<FastFoldFollowing
|
|
|
|
user={user}
|
|
|
|
followedFoldSlugs={followedFolds.map((f) => f.slug)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className="mx-3 mb-3 gap-2 text-sm text-gray-800 sm:flex-row">
|
2022-02-08 04:06:58 +00:00
|
|
|
<Row className="gap-2">
|
2022-02-11 18:40:22 +00:00
|
|
|
<SparklesIcon className="inline h-5 w-5" aria-hidden="true" />
|
2022-02-08 04:06:58 +00:00
|
|
|
<span className="whitespace-nowrap">Recent activity</span>
|
|
|
|
</Row>
|
|
|
|
</Col>
|
2022-02-07 00:03:36 +00:00
|
|
|
|
2022-02-03 22:54:08 +00:00
|
|
|
{activeContracts ? (
|
|
|
|
<ActivityFeed
|
|
|
|
contracts={activeContracts}
|
2022-02-05 02:09:34 +00:00
|
|
|
contractBets={activeBets}
|
|
|
|
contractComments={activeComments}
|
2022-02-03 22:54:08 +00:00
|
|
|
/>
|
2022-02-03 23:12:09 +00:00
|
|
|
) : (
|
|
|
|
<LoadingIndicator className="mt-4" />
|
|
|
|
)}
|
2022-01-23 00:16:23 +00:00
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Home
|