diff --git a/functions/src/update-user-metrics.ts b/functions/src/update-user-metrics.ts index 4c5fbb60..f59d4a34 100644 --- a/functions/src/update-user-metrics.ts +++ b/functions/src/update-user-metrics.ts @@ -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 ) => { 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) => { diff --git a/web/components/answers-panel.tsx b/web/components/answers-panel.tsx index 483190ef..4309d8a2 100644 --- a/web/components/answers-panel.tsx +++ b/web/components/answers-panel.tsx @@ -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(10) const [amountError, setAmountError] = useState() @@ -451,17 +452,28 @@ function CreateAnswerInput(props: { contract: Contract }) { )} - + {user ? ( + + ) : ( + text && ( + + ) + )} diff --git a/web/pages/home.tsx b/web/pages/home.tsx index 038ed3b9..f834e18f 100644 --- a/web/pages/home.tsx +++ b/web/pages/home.tsx @@ -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('/')