manifold/web/hooks/use-find-active-contracts.ts
James Grugett d7d8fb94c4
Trim activity feed (#60)
* Trim activity feed: show only items from last 24 hours, remove contract description, shorter width.

* Preserve feed ordering based on initial fetch of bets

* Add see more button, description to explore items
2022-03-04 18:06:11 -06:00

126 lines
3.6 KiB
TypeScript

import _ from 'lodash'
import { useMemo, useRef } from 'react'
import { Fold } from '../../common/fold'
import { User } from '../../common/user'
import { filterDefined } from '../../common/util/array'
import { Bet } from '../lib/firebase/bets'
import { Comment, getRecentComments } from '../lib/firebase/comments'
import { Contract, getActiveContracts } from '../lib/firebase/contracts'
import { listAllFolds } from '../lib/firebase/folds'
import { findActiveContracts } from '../components/activity-feed'
import { useInactiveContracts } from './use-contracts'
import { useFollowedFolds } from './use-fold'
import { useUserBetContracts } from './use-user-bets'
// used in static props
export const getAllContractInfo = async () => {
let [contracts, folds] = await Promise.all([
getActiveContracts().catch((_) => []),
listAllFolds().catch(() => []),
])
const recentComments = await getRecentComments()
return { contracts, recentComments, folds }
}
export const useFilterYourContracts = (
user: User | undefined | null,
folds: Fold[],
contracts: Contract[]
) => {
const followedFoldIds = useFollowedFolds(user)
const followedFolds = filterDefined(
(followedFoldIds ?? []).map((id) => folds.find((fold) => fold.id === id))
)
// Save the initial followed fold slugs.
const followedFoldSlugsRef = useRef<string[] | undefined>()
if (followedFoldIds && !followedFoldSlugsRef.current)
followedFoldSlugsRef.current = followedFolds.map((f) => f.slug)
const initialFollowedFoldSlugs = followedFoldSlugsRef.current
const tagSet = new Set(
_.flatten(followedFolds.map((fold) => fold.lowercaseTags))
)
const yourBetContractIds = useUserBetContracts(user?.id)
const yourBetContracts = yourBetContractIds
? new Set(yourBetContractIds)
: undefined
// Show no contracts before your info is loaded.
let yourContracts: Contract[] = []
if (yourBetContracts && followedFoldIds) {
// Show all contracts if no folds are followed.
if (followedFoldIds.length === 0) yourContracts = contracts
else
yourContracts = contracts.filter(
(contract) =>
contract.lowercaseTags.some((tag) => tagSet.has(tag)) ||
yourBetContracts.has(contract.id)
)
}
return {
yourContracts,
initialFollowedFoldSlugs,
}
}
export const useFindActiveContracts = (props: {
contracts: Contract[]
recentBets: Bet[]
recentComments: Comment[]
}) => {
const { contracts, recentBets, recentComments } = props
const activeContracts = findActiveContracts(
contracts,
recentComments,
recentBets
)
const betsByContract = _.groupBy(recentBets, (bet) => bet.contractId)
const activeBets = activeContracts.map(
(contract) => betsByContract[contract.id] ?? []
)
const commentsByContract = _.groupBy(
recentComments,
(comment) => comment.contractId
)
const activeComments = activeContracts.map(
(contract) => commentsByContract[contract.id] ?? []
)
return {
activeContracts,
activeBets,
activeComments,
}
}
export const useExploreContracts = (maxContracts = 75) => {
const inactiveContracts = useInactiveContracts()
const contractsDict = _.fromPairs(
(inactiveContracts ?? []).map((c) => [c.id, c])
)
// Preserve random ordering once inactiveContracts loaded.
const exploreContractIds = useMemo(
() => _.shuffle(Object.keys(contractsDict)),
// eslint-disable-next-line react-hooks/exhaustive-deps
[!!inactiveContracts]
).slice(0, maxContracts)
if (!inactiveContracts) return undefined
return filterDefined(exploreContractIds.map((id) => contractsDict[id]))
}