manifold/web/pages/home.tsx

148 lines
4.2 KiB
TypeScript
Raw Normal View History

import React from 'react'
2022-01-23 00:16:23 +00:00
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 { 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'
import { listAllFolds } from '../lib/firebase/folds'
import { Fold } from '../../common/fold'
import { filterDefined } from '../../common/util/array'
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'
import { useFollowedFolds } from '../hooks/use-fold'
import { FastFoldFollowing } from '../components/fast-fold-following'
2022-01-23 00:16:23 +00:00
export async function getStaticProps() {
let [contracts, folds] = await Promise.all([
listAllContracts().catch((_) => []),
listAllFolds().catch(() => []),
])
2022-01-23 00:16:23 +00:00
const [recentBets, recentComments] = await Promise.all([
getRecentBets(),
getRecentComments(),
])
2022-01-23 00:16:23 +00:00
return {
props: {
contracts,
recentBets,
recentComments,
folds,
2022-01-23 00:16:23 +00:00
},
revalidate: 60, // regenerate after a minute
}
}
const Home = (props: {
contracts: Contract[]
folds: Fold[]
recentBets: Bet[]
recentComments: Comment[]
}) => {
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-07 02:03:42 +00:00
const followedFoldIds = useFollowedFolds(user)
const followedFolds = filterDefined(
2022-02-07 02:03:42 +00:00
(followedFoldIds ?? []).map((id) => folds.find((fold) => fold.id === id))
)
const tagSet = new Set(
_.flatten(followedFolds.map((fold) => fold.lowercaseTags))
)
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)
)
}
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-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="w-full max-w-3xl">
<FeedCreate user={user ?? undefined} />
2022-02-03 23:40:37 +00:00
<Spacer h={6} />
{followedFoldIds !== undefined && followedFolds.length === 0 && (
<FastFoldFollowing
user={user}
followedFoldSlugs={followedFolds.map((f) => f.slug)}
/>
)}
<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">
<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>
{activeContracts ? (
<ActivityFeed
contracts={activeContracts}
contractBets={activeBets}
contractComments={activeComments}
/>
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