From cd590031e710d12c76e6d59235533bacdffddac0 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 25 Feb 2022 01:59:53 -0600 Subject: [PATCH] Feed updates: 1. Order by any bet instead of top 10 & comments 2. No resolved contracts 3. Show loading while fetching recent bets clientside so order preserved 4. Don't change order from new bets --- web/hooks/use-active-contracts.ts | 8 ++----- web/hooks/use-bets.ts | 9 ++++++++ web/pages/activity.tsx | 36 +++++++++++------------------ web/pages/fold/[...slugs]/index.tsx | 3 +-- web/pages/home.tsx | 11 +++++++-- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/web/hooks/use-active-contracts.ts b/web/hooks/use-active-contracts.ts index 0bd099e2..927073ae 100644 --- a/web/hooks/use-active-contracts.ts +++ b/web/hooks/use-active-contracts.ts @@ -9,8 +9,6 @@ import { Comment, getRecentComments } from '../lib/firebase/comments' import { Contract, listAllContracts } from '../lib/firebase/contracts' import { listAllFolds } from '../lib/firebase/folds' import { findActiveContracts } from '../pages/activity' -import { useRecentBets } from './use-bets' -import { useRecentComments } from './use-comments' import { useContracts } from './use-contracts' import { useFollowedFolds } from './use-fold' import { useUserBetContracts } from './use-user-bets' @@ -39,9 +37,8 @@ export const useActiveContracts = ( }, user: User | undefined | null ) => { + const { recentBets, recentComments } = props const contracts = useContracts() ?? props.contracts - const recentBets = useRecentBets() ?? props.recentBets - const recentComments = useRecentComments() ?? props.recentComments const followedFoldIds = useFollowedFolds(user) @@ -82,8 +79,7 @@ export const useActiveContracts = ( const activeContracts = findActiveContracts( feedContracts, recentComments, - recentBets, - 365 + recentBets ) const betsByContract = _.groupBy(recentBets, (bet) => bet.contractId) diff --git a/web/hooks/use-bets.ts b/web/hooks/use-bets.ts index 5ea66e1c..4c530f5b 100644 --- a/web/hooks/use-bets.ts +++ b/web/hooks/use-bets.ts @@ -2,6 +2,7 @@ import { useEffect, useState } from 'react' import { Contract } from '../../common/contract' import { Bet, + getRecentBets, listenForBets, listenForRecentBets, withoutAnteBets, @@ -36,3 +37,11 @@ export const useRecentBets = () => { useEffect(() => listenForRecentBets(setRecentBets), []) return recentBets } + +export const useGetRecentBets = () => { + const [recentBets, setRecentBets] = useState() + useEffect(() => { + getRecentBets().then(setRecentBets) + }, []) + return recentBets +} diff --git a/web/pages/activity.tsx b/web/pages/activity.tsx index e7683298..c039ee4d 100644 --- a/web/pages/activity.tsx +++ b/web/pages/activity.tsx @@ -5,10 +5,8 @@ import { Contract } from '../lib/firebase/contracts' import { Comment } from '../lib/firebase/comments' import { Col } from '../components/layout/col' import { Bet } from '../../common/bet' -import { filterDefined } from '../../common/util/array' const MAX_ACTIVE_CONTRACTS = 75 -const MAX_HOT_MARKETS = 10 // This does NOT include comment times, since those aren't part of the contract atm. // TODO: Maybe store last activity time directly in the contract? @@ -29,8 +27,7 @@ function lastActivityTime(contract: Contract) { export function findActiveContracts( allContracts: Contract[], recentComments: Comment[], - recentBets: Bet[], - daysAgo = 3 + recentBets: Bet[] ) { const idToActivityTime = new Map() function record(contractId: string, time: number) { @@ -44,10 +41,8 @@ export function findActiveContracts( // Find contracts with activity in the last 3 days const DAY_IN_MS = 24 * 60 * 60 * 1000 for (const contract of allContracts || []) { - if (lastActivityTime(contract) > Date.now() - daysAgo * DAY_IN_MS) { - contracts.push(contract) - record(contract.id, lastActivityTime(contract)) - } + contracts.push(contract) + record(contract.id, lastActivityTime(contract)) } // Add every contract that had a recent comment, too @@ -60,29 +55,24 @@ export function findActiveContracts( } } - // Add recent top-trading contracts, ordered by last bet. + // Add contracts by last bet time. const contractBets = _.groupBy(recentBets, (bet) => bet.contractId) - const contractTotalBets = _.mapValues(contractBets, (bets) => - _.sumBy(bets, (bet) => bet.amount) + const contractMostRecentBet = _.mapValues( + contractBets, + (bets) => _.maxBy(bets, (bet) => bet.createdTime) as Bet ) - const sortedPairs = _.sortBy( - _.toPairs(contractTotalBets), - ([_, total]) => -1 * total - ) - const topTradedContracts = filterDefined( - sortedPairs.map(([id]) => contractsById.get(id)) - ).slice(0, MAX_HOT_MARKETS) - - for (const contract of topTradedContracts) { - const bet = recentBets.find((bet) => bet.contractId === contract.id) - if (bet) { + for (const bet of Object.values(contractMostRecentBet)) { + const contract = contractsById.get(bet.id) + if (contract) { contracts.push(contract) record(contract.id, bet.createdTime) } } contracts = _.uniqBy(contracts, (c) => c.id) - contracts = contracts.filter((contract) => contract.visibility === 'public') + contracts = contracts.filter( + (contract) => contract.visibility === 'public' && !contract.isResolved + ) contracts = _.sortBy(contracts, (c) => -(idToActivityTime.get(c.id) ?? 0)) return contracts.slice(0, MAX_ACTIVE_CONTRACTS) } diff --git a/web/pages/fold/[...slugs]/index.tsx b/web/pages/fold/[...slugs]/index.tsx index aa3742a4..39c0845b 100644 --- a/web/pages/fold/[...slugs]/index.tsx +++ b/web/pages/fold/[...slugs]/index.tsx @@ -67,8 +67,7 @@ export async function getStaticProps(props: { params: { slugs: string[] } }) { let activeContracts = findActiveContracts( contracts, _.flatten(contractComments), - _.flatten(contractRecentBets), - 365 + _.flatten(contractRecentBets) ) const [resolved, unresolved] = _.partition( activeContracts, diff --git a/web/pages/home.tsx b/web/pages/home.tsx index 4a91fd59..ec21e62f 100644 --- a/web/pages/home.tsx +++ b/web/pages/home.tsx @@ -19,6 +19,7 @@ import { getAllContractInfo, useActiveContracts, } from '../hooks/use-active-contracts' +import { useGetRecentBets } from '../hooks/use-bets' export async function getStaticProps() { const contractInfo = await getAllContractInfo() @@ -35,14 +36,20 @@ const Home = (props: { recentBets: Bet[] recentComments: Comment[] }) => { + const { contracts, folds, recentComments } = props const user = useUser() + const recentBets = useGetRecentBets() + const { activeContracts, activeBets, activeComments, initialFollowedFoldSlugs, - } = useActiveContracts(props, user) + } = useActiveContracts( + { contracts, folds, recentBets: recentBets ?? [], recentComments }, + user + ) if (user === null) { Router.replace('/') @@ -71,7 +78,7 @@ const Home = (props: { - {activeContracts ? ( + {activeContracts && recentBets ? (