Feed: Send only contracts with bets in last 24 hours. Instead of all contracts: 2.2MB => 0.8MB

This commit is contained in:
James Grugett 2022-02-25 02:11:10 -06:00
parent cd590031e7
commit ab5ea0181d
5 changed files with 36 additions and 8 deletions

View File

@ -747,7 +747,7 @@ export function ContractFeed(props: {
<div className="flow-root pr-2 md:pr-0"> <div className="flow-root pr-2 md:pr-0">
<div className={clsx(tradingAllowed(contract) ? '' : '-mb-8')}> <div className={clsx(tradingAllowed(contract) ? '' : '-mb-8')}>
{items.map((activityItem, activityItemIdx) => ( {items.map((activityItem, activityItemIdx) => (
<div className="relative pb-8"> <div key={activityItem.id} className="relative pb-8">
{activityItemIdx !== items.length - 1 ? ( {activityItemIdx !== items.length - 1 ? (
<span <span
className="absolute top-5 left-5 -ml-px h-[calc(100%-2rem)] w-0.5 bg-gray-200" className="absolute top-5 left-5 -ml-px h-[calc(100%-2rem)] w-0.5 bg-gray-200"

View File

@ -6,17 +6,17 @@ import { User } from '../../common/user'
import { filterDefined } from '../../common/util/array' import { filterDefined } from '../../common/util/array'
import { Bet, getRecentBets } from '../lib/firebase/bets' import { Bet, getRecentBets } from '../lib/firebase/bets'
import { Comment, getRecentComments } from '../lib/firebase/comments' import { Comment, getRecentComments } from '../lib/firebase/comments'
import { Contract, listAllContracts } from '../lib/firebase/contracts' import { Contract, getActiveContracts } from '../lib/firebase/contracts'
import { listAllFolds } from '../lib/firebase/folds' import { listAllFolds } from '../lib/firebase/folds'
import { findActiveContracts } from '../pages/activity' import { findActiveContracts } from '../pages/activity'
import { useContracts } from './use-contracts' import { useActiveContracts } from './use-contracts'
import { useFollowedFolds } from './use-fold' import { useFollowedFolds } from './use-fold'
import { useUserBetContracts } from './use-user-bets' import { useUserBetContracts } from './use-user-bets'
// used in static props // used in static props
export const getAllContractInfo = async () => { export const getAllContractInfo = async () => {
let [contracts, folds] = await Promise.all([ let [contracts, folds] = await Promise.all([
listAllContracts().catch((_) => []), getActiveContracts().catch((_) => []),
listAllFolds().catch(() => []), listAllFolds().catch(() => []),
]) ])
@ -28,7 +28,7 @@ export const getAllContractInfo = async () => {
return { contracts, recentBets, recentComments, folds } return { contracts, recentBets, recentComments, folds }
} }
export const useActiveContracts = ( export const useFindActiveContracts = (
props: { props: {
contracts: Contract[] contracts: Contract[]
folds: Fold[] folds: Fold[]
@ -38,7 +38,7 @@ export const useActiveContracts = (
user: User | undefined | null user: User | undefined | null
) => { ) => {
const { recentBets, recentComments } = props const { recentBets, recentComments } = props
const contracts = useContracts() ?? props.contracts const contracts = useActiveContracts() ?? props.contracts
const followedFoldIds = useFollowedFolds(user) const followedFoldIds = useFollowedFolds(user)

View File

@ -2,6 +2,7 @@ import _ from 'lodash'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { import {
Contract, Contract,
listenForActiveContracts,
listenForContracts, listenForContracts,
listenForHotContracts, listenForHotContracts,
} from '../lib/firebase/contracts' } from '../lib/firebase/contracts'
@ -17,6 +18,16 @@ export const useContracts = () => {
return contracts return contracts
} }
export const useActiveContracts = () => {
const [contracts, setContracts] = useState<Contract[] | undefined>()
useEffect(() => {
return listenForActiveContracts(setContracts)
}, [])
return contracts
}
export const useUpdatedContracts = (initialContracts: Contract[]) => { export const useUpdatedContracts = (initialContracts: Contract[]) => {
const [contracts, setContracts] = useState(initialContracts) const [contracts, setContracts] = useState(initialContracts)

View File

@ -115,6 +115,23 @@ export function listenForContracts(
return listenForValues<Contract>(q, setContracts) return listenForValues<Contract>(q, setContracts)
} }
const activeContracts = query(
contractCollection,
where('isResolved', '==', false),
where('visibility', '==', 'public'),
where('volume24Hours', '>', 0)
)
export function getActiveContracts() {
return getValues<Contract>(activeContracts)
}
export function listenForActiveContracts(
setContracts: (contracts: Contract[]) => void
) {
return listenForValues<Contract>(activeContracts, setContracts)
}
export function listenForContract( export function listenForContract(
contractId: string, contractId: string,
setContract: (contract: Contract | null) => void setContract: (contract: Contract | null) => void

View File

@ -17,7 +17,7 @@ import { SparklesIcon } from '@heroicons/react/solid'
import { FastFoldFollowing } from '../components/fast-fold-following' import { FastFoldFollowing } from '../components/fast-fold-following'
import { import {
getAllContractInfo, getAllContractInfo,
useActiveContracts, useFindActiveContracts,
} from '../hooks/use-active-contracts' } from '../hooks/use-active-contracts'
import { useGetRecentBets } from '../hooks/use-bets' import { useGetRecentBets } from '../hooks/use-bets'
@ -46,7 +46,7 @@ const Home = (props: {
activeBets, activeBets,
activeComments, activeComments,
initialFollowedFoldSlugs, initialFollowedFoldSlugs,
} = useActiveContracts( } = useFindActiveContracts(
{ contracts, folds, recentBets: recentBets ?? [], recentComments }, { contracts, folds, recentBets: recentBets ?? [], recentComments },
user user
) )