Merge branch 'main' into fast-fold-following
This commit is contained in:
commit
c5218cff98
|
@ -24,17 +24,15 @@ export const updateUserMetrics = functions.pubsub
|
|||
|
||||
await Promise.all(
|
||||
users.map(async (user) => {
|
||||
const investmentValue = await computeInvestmentValue(
|
||||
user,
|
||||
contractsDict
|
||||
)
|
||||
const totalValue = user.balance + investmentValue
|
||||
const [investmentValue, creatorVolume] = await Promise.all([
|
||||
computeInvestmentValue(user, contractsDict),
|
||||
computeTotalPool(user, contractsDict),
|
||||
])
|
||||
|
||||
const totalValue = user.balance + investmentValue
|
||||
const totalPnL = totalValue - user.totalDeposits
|
||||
|
||||
const creatorVolume = await computeTotalVolume(user, contractsDict)
|
||||
|
||||
return firestore.collection('users').doc(user.id).update({
|
||||
await firestore.collection('users').doc(user.id).update({
|
||||
totalPnLCached: totalPnL,
|
||||
creatorVolumeCached: creatorVolume,
|
||||
})
|
||||
|
@ -58,15 +56,17 @@ const computeInvestmentValue = async (
|
|||
})
|
||||
}
|
||||
|
||||
const computeTotalVolume = async (
|
||||
const computeTotalPool = async (
|
||||
user: User,
|
||||
contractsDict: _.Dictionary<Contract>
|
||||
) => {
|
||||
const creatorContracts = Object.values(contractsDict).filter(
|
||||
(contract) => contract.creatorId === user.id
|
||||
)
|
||||
const volumes = await Promise.all(creatorContracts.map(computeVolume))
|
||||
return _.sum(volumes)
|
||||
const pools = creatorContracts.map((contract) =>
|
||||
_.sum(Object.values(contract.pool))
|
||||
)
|
||||
return _.sum(pools)
|
||||
}
|
||||
|
||||
const computeVolume = async (contract: Contract) => {
|
||||
|
|
|
@ -350,6 +350,7 @@ function AnswerBetPanel(props: {
|
|||
|
||||
function CreateAnswerInput(props: { contract: Contract }) {
|
||||
const { contract } = props
|
||||
const user = useUser()
|
||||
const [text, setText] = useState('')
|
||||
const [betAmount, setBetAmount] = useState<number | undefined>(10)
|
||||
const [amountError, setAmountError] = useState<string | undefined>()
|
||||
|
@ -451,17 +452,28 @@ function CreateAnswerInput(props: { contract: Contract }) {
|
|||
</Col>
|
||||
</>
|
||||
)}
|
||||
<button
|
||||
className={clsx(
|
||||
'btn self-end mt-2',
|
||||
canSubmit ? 'btn-outline' : 'btn-disabled',
|
||||
isSubmitting && 'loading'
|
||||
)}
|
||||
disabled={!canSubmit}
|
||||
onClick={submitAnswer}
|
||||
>
|
||||
Submit answer & bet
|
||||
</button>
|
||||
{user ? (
|
||||
<button
|
||||
className={clsx(
|
||||
'btn self-end mt-2',
|
||||
canSubmit ? 'btn-outline' : 'btn-disabled',
|
||||
isSubmitting && 'loading'
|
||||
)}
|
||||
disabled={!canSubmit}
|
||||
onClick={submitAnswer}
|
||||
>
|
||||
Submit answer & bet
|
||||
</button>
|
||||
) : (
|
||||
text && (
|
||||
<button
|
||||
className="btn self-end whitespace-nowrap border-none bg-gradient-to-r from-teal-500 to-green-500 px-10 text-lg font-medium normal-case hover:from-teal-600 hover:to-green-600"
|
||||
onClick={firebaseLogin}
|
||||
>
|
||||
Sign in
|
||||
</button>
|
||||
)
|
||||
)}
|
||||
</Col>
|
||||
</Col>
|
||||
</Col>
|
||||
|
|
|
@ -5,8 +5,8 @@ import _ from 'lodash'
|
|||
import { Contract, listAllContracts } from '../lib/firebase/contracts'
|
||||
import { Page } from '../components/page'
|
||||
import { ActivityFeed, findActiveContracts } from './activity'
|
||||
import { Comment, listAllComments } from '../lib/firebase/comments'
|
||||
import { Bet, listAllBets } from '../lib/firebase/bets'
|
||||
import { Comment, getRecentComments } from '../lib/firebase/comments'
|
||||
import { Bet, getRecentBets } from '../lib/firebase/bets'
|
||||
import FeedCreate from '../components/feed-create'
|
||||
import { Spacer } from '../components/layout/spacer'
|
||||
import { Col } from '../components/layout/col'
|
||||
|
@ -28,16 +28,16 @@ export async function getStaticProps() {
|
|||
listAllFolds().catch(() => []),
|
||||
])
|
||||
|
||||
const [contractBets, contractComments] = await Promise.all([
|
||||
Promise.all(contracts.map((contract) => listAllBets(contract.id))),
|
||||
Promise.all(contracts.map((contract) => listAllComments(contract.id))),
|
||||
const [recentBets, recentComments] = await Promise.all([
|
||||
getRecentBets(),
|
||||
getRecentComments(),
|
||||
])
|
||||
|
||||
return {
|
||||
props: {
|
||||
contracts,
|
||||
contractBets,
|
||||
contractComments,
|
||||
recentBets,
|
||||
recentComments,
|
||||
folds,
|
||||
},
|
||||
|
||||
|
@ -47,18 +47,15 @@ export async function getStaticProps() {
|
|||
|
||||
const Home = (props: {
|
||||
contracts: Contract[]
|
||||
contractBets: Bet[][]
|
||||
contractComments: Comment[][]
|
||||
folds: Fold[]
|
||||
recentBets: Bet[]
|
||||
recentComments: Comment[]
|
||||
}) => {
|
||||
const { contractBets, contractComments, folds } = props
|
||||
const { folds, recentBets, recentComments } = props
|
||||
|
||||
const user = useUser()
|
||||
|
||||
const contracts = useUpdatedContracts(props.contracts)
|
||||
const contractIdToIndex = _.fromPairs(
|
||||
contracts.map((contract, index) => [contract.id, index])
|
||||
)
|
||||
|
||||
const followedFoldIds = useFollowedFolds(user)
|
||||
const followedFolds = filterDefined(
|
||||
|
@ -86,29 +83,25 @@ const Home = (props: {
|
|||
)
|
||||
}
|
||||
|
||||
const oneDayMS = 24 * 60 * 60 * 1000
|
||||
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()
|
||||
const activeContracts = findActiveContracts(
|
||||
feedContracts,
|
||||
recentComments,
|
||||
recentBets,
|
||||
365
|
||||
)
|
||||
|
||||
const activeContracts =
|
||||
feedContracts &&
|
||||
findActiveContracts(feedContracts, feedComments, recentBets, 365)
|
||||
const betsByContract = _.groupBy(recentBets, (bet) => bet.contractId)
|
||||
const activeBets = activeContracts.map(
|
||||
(contract) => betsByContract[contract.id] ?? []
|
||||
)
|
||||
|
||||
const activeBets = activeContracts
|
||||
? activeContracts.map(
|
||||
(contract) => contractBets[contractIdToIndex[contract.id]]
|
||||
)
|
||||
: []
|
||||
const activeComments = activeContracts
|
||||
? activeContracts.map(
|
||||
(contract) => contractComments[contractIdToIndex[contract.id]]
|
||||
)
|
||||
: []
|
||||
const commentsByContract = _.groupBy(
|
||||
recentComments,
|
||||
(comment) => comment.contractId
|
||||
)
|
||||
const activeComments = activeContracts.map(
|
||||
(contract) => commentsByContract[contract.id] ?? []
|
||||
)
|
||||
|
||||
if (user === null) {
|
||||
Router.replace('/')
|
||||
|
|
Loading…
Reference in New Issue
Block a user