2022-02-20 00:02:40 +00:00
|
|
|
import _ from 'lodash'
|
2022-02-21 05:12:35 +00:00
|
|
|
import { useRef } from 'react'
|
2022-02-20 00:02:40 +00:00
|
|
|
|
|
|
|
import { Fold } from '../../common/fold'
|
|
|
|
import { User } from '../../common/user'
|
|
|
|
import { filterDefined } from '../../common/util/array'
|
|
|
|
import { Bet, getRecentBets } from '../lib/firebase/bets'
|
|
|
|
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'
|
2022-02-21 03:06:10 +00:00
|
|
|
import { useContracts } from './use-contracts'
|
2022-02-20 00:02:40 +00:00
|
|
|
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([
|
|
|
|
listAllContracts().catch((_) => []),
|
|
|
|
listAllFolds().catch(() => []),
|
|
|
|
])
|
|
|
|
|
|
|
|
const [recentBets, recentComments] = await Promise.all([
|
|
|
|
getRecentBets(),
|
|
|
|
getRecentComments(),
|
|
|
|
])
|
|
|
|
|
|
|
|
return { contracts, recentBets, recentComments, folds }
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useActiveContracts = (
|
|
|
|
props: {
|
|
|
|
contracts: Contract[]
|
|
|
|
folds: Fold[]
|
|
|
|
recentBets: Bet[]
|
|
|
|
recentComments: Comment[]
|
|
|
|
},
|
|
|
|
user: User | undefined | null
|
|
|
|
) => {
|
2022-02-25 07:59:53 +00:00
|
|
|
const { recentBets, recentComments } = props
|
2022-02-21 03:06:10 +00:00
|
|
|
const contracts = useContracts() ?? props.contracts
|
2022-02-20 00:02:40 +00:00
|
|
|
|
|
|
|
const followedFoldIds = useFollowedFolds(user)
|
|
|
|
|
|
|
|
const followedFolds = filterDefined(
|
|
|
|
(followedFoldIds ?? []).map((id) =>
|
|
|
|
props.folds.find((fold) => fold.id === id)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-02-21 05:12:35 +00:00
|
|
|
// 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
|
2022-02-20 00:02:40 +00:00
|
|
|
|
|
|
|
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 feedContracts: Contract[] = []
|
|
|
|
if (yourBetContracts && followedFoldIds) {
|
|
|
|
// Show all contracts if no folds are followed.
|
|
|
|
if (followedFoldIds.length === 0) feedContracts = contracts
|
|
|
|
else
|
|
|
|
feedContracts = contracts.filter(
|
|
|
|
(contract) =>
|
|
|
|
contract.lowercaseTags.some((tag) => tagSet.has(tag)) ||
|
|
|
|
yourBetContracts.has(contract.id)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const activeContracts = findActiveContracts(
|
|
|
|
feedContracts,
|
|
|
|
recentComments,
|
2022-02-25 07:59:53 +00:00
|
|
|
recentBets
|
2022-02-20 00:02:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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] ?? []
|
|
|
|
)
|
|
|
|
|
2022-02-21 05:12:35 +00:00
|
|
|
return {
|
|
|
|
activeContracts,
|
|
|
|
activeBets,
|
|
|
|
activeComments,
|
|
|
|
initialFollowedFoldSlugs,
|
|
|
|
}
|
2022-02-20 00:02:40 +00:00
|
|
|
}
|