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-03 22:54:08 +00:00
|
|
|
import { Comment, listAllComments } from '../lib/firebase/comments'
|
|
|
|
import { Bet, 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-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-03 23:40:37 +00:00
|
|
|
import { FoldTagList } from '../components/tags-list'
|
2022-02-04 18:30:13 +00:00
|
|
|
import { SearchIcon } from '@heroicons/react/outline'
|
|
|
|
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-01-23 00:16:23 +00:00
|
|
|
|
|
|
|
export async function getStaticProps() {
|
2022-02-03 22:54:08 +00:00
|
|
|
const [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-05 02:09:34 +00:00
|
|
|
const [contractBets, contractComments] = await Promise.all([
|
|
|
|
Promise.all(contracts.map((contract) => listAllBets(contract.id))),
|
|
|
|
Promise.all(contracts.map((contract) => listAllComments(contract.id))),
|
|
|
|
])
|
|
|
|
|
2022-01-23 00:16:23 +00:00
|
|
|
return {
|
|
|
|
props: {
|
2022-02-03 22:54:08 +00:00
|
|
|
contracts,
|
2022-02-05 02:09:34 +00:00
|
|
|
contractBets,
|
|
|
|
contractComments,
|
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[]
|
|
|
|
contractBets: Bet[][]
|
|
|
|
contractComments: Comment[][]
|
|
|
|
folds: Fold[]
|
|
|
|
}) => {
|
|
|
|
const { contractBets, contractComments, folds } = 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-05 02:09:34 +00:00
|
|
|
const contractIdToIndex = _.fromPairs(
|
|
|
|
contracts.map((contract, index) => [contract.id, index])
|
|
|
|
)
|
2022-02-03 22:54:08 +00:00
|
|
|
|
2022-02-04 23:24:54 +00:00
|
|
|
const followedFoldIds = useFollowedFolds(user)
|
2022-02-03 22:54:08 +00:00
|
|
|
const followedFolds = filterDefined(
|
|
|
|
(followedFoldIds ?? []).map((id) => folds.find((fold) => fold.id === id))
|
|
|
|
)
|
|
|
|
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-03 22:54:08 +00:00
|
|
|
const feedContracts =
|
2022-02-04 23:24:54 +00:00
|
|
|
followedFoldIds && yourBetContracts
|
2022-02-03 22:54:08 +00:00
|
|
|
? contracts.filter(
|
|
|
|
(contract) =>
|
|
|
|
contract.lowercaseTags.some((tag) => tagSet.has(tag)) ||
|
|
|
|
yourBetContracts.has(contract.id)
|
|
|
|
)
|
|
|
|
: undefined
|
|
|
|
|
|
|
|
const oneDayMS = 24 * 60 * 60 * 1000
|
2022-02-05 02:09:34 +00:00
|
|
|
const recentBets = (feedContracts ?? [])
|
|
|
|
.map((c) => contractBets[contractIdToIndex[c.id]])
|
|
|
|
.flat()
|
|
|
|
.filter((bet) => bet.createdTime > Date.now() - oneDayMS)
|
|
|
|
const feedComments = (feedContracts ?? [])
|
|
|
|
.map((c) => contractComments[contractIdToIndex[c.id]])
|
|
|
|
.flat()
|
2022-02-03 22:54:08 +00:00
|
|
|
|
|
|
|
const activeContracts =
|
|
|
|
feedContracts &&
|
2022-02-05 02:09:34 +00:00
|
|
|
findActiveContracts(feedContracts, feedComments, recentBets, 365)
|
2022-02-03 22:54:08 +00:00
|
|
|
|
2022-02-05 02:09:34 +00:00
|
|
|
const activeBets = activeContracts
|
2022-02-03 22:54:08 +00:00
|
|
|
? activeContracts.map(
|
2022-02-05 02:09:34 +00:00
|
|
|
(contract) => contractBets[contractIdToIndex[contract.id]]
|
2022-02-03 22:54:08 +00:00
|
|
|
)
|
|
|
|
: []
|
2022-02-05 02:09:34 +00:00
|
|
|
const activeComments = activeContracts
|
2022-02-03 22:54:08 +00:00
|
|
|
? activeContracts.map(
|
2022-02-05 02:09:34 +00:00
|
|
|
(contract) => contractComments[contractIdToIndex[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-03 22:54:08 +00:00
|
|
|
<Col className="max-w-3xl w-full">
|
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-04 18:30:13 +00:00
|
|
|
<Row className="text-sm text-gray-800 mx-3 mb-3 gap-2 items-center">
|
|
|
|
<SearchIcon className="inline w-5 h-5" aria-hidden="true" />
|
|
|
|
Explore our communities
|
|
|
|
</Row>
|
2022-02-03 23:40:37 +00:00
|
|
|
<FoldTagList
|
2022-02-03 19:00:49 +00:00
|
|
|
className="mx-2"
|
2022-02-04 01:00:17 +00:00
|
|
|
noLabel
|
2022-02-03 23:40:37 +00:00
|
|
|
folds={[
|
|
|
|
{ name: 'Politics', slug: 'politics' },
|
|
|
|
{ name: 'Crypto', slug: 'crypto' },
|
|
|
|
{ name: 'Sports', slug: 'sports' },
|
|
|
|
{ name: 'Science', slug: 'science' },
|
2022-02-04 01:00:17 +00:00
|
|
|
{ name: 'Covid', slug: 'covid' },
|
|
|
|
{ name: 'AI', slug: 'ai' },
|
2022-02-03 23:40:37 +00:00
|
|
|
{
|
|
|
|
name: 'Manifold Markets',
|
|
|
|
slug: 'manifold-markets',
|
|
|
|
},
|
2022-02-03 19:00:49 +00:00
|
|
|
]}
|
|
|
|
/>
|
2022-02-04 18:30:13 +00:00
|
|
|
<Spacer h={10} />
|
|
|
|
<Row className="text-sm text-gray-800 mx-3 mb-3 gap-2 items-center">
|
|
|
|
<SparklesIcon className="inline w-5 h-5" aria-hidden="true" />
|
|
|
|
Recent activity
|
|
|
|
</Row>
|
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
|