2022-02-20 00:02:40 +00:00
|
|
|
import _ from 'lodash'
|
2022-02-27 21:37:04 +00:00
|
|
|
import { useMemo, 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'
|
2022-03-05 00:06:11 +00:00
|
|
|
import { Bet } from '../lib/firebase/bets'
|
2022-02-20 00:02:40 +00:00
|
|
|
import { Comment, getRecentComments } from '../lib/firebase/comments'
|
2022-02-25 08:11:10 +00:00
|
|
|
import { Contract, getActiveContracts } from '../lib/firebase/contracts'
|
2022-02-20 00:02:40 +00:00
|
|
|
import { listAllFolds } from '../lib/firebase/folds'
|
2022-03-05 00:06:11 +00:00
|
|
|
import { findActiveContracts } from '../components/activity-feed'
|
2022-02-27 21:37:04 +00:00
|
|
|
import { useInactiveContracts } 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([
|
2022-02-25 08:11:10 +00:00
|
|
|
getActiveContracts().catch((_) => []),
|
2022-02-20 00:02:40 +00:00
|
|
|
listAllFolds().catch(() => []),
|
|
|
|
])
|
|
|
|
|
2022-03-05 00:06:11 +00:00
|
|
|
const recentComments = await getRecentComments()
|
2022-02-20 00:02:40 +00:00
|
|
|
|
2022-03-05 00:06:11 +00:00
|
|
|
return { contracts, recentComments, folds }
|
2022-02-20 00:02:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 21:37:04 +00:00
|
|
|
export const useFilterYourContracts = (
|
|
|
|
user: User | undefined | null,
|
|
|
|
folds: Fold[],
|
|
|
|
contracts: Contract[]
|
2022-02-20 00:02:40 +00:00
|
|
|
) => {
|
|
|
|
const followedFoldIds = useFollowedFolds(user)
|
|
|
|
|
|
|
|
const followedFolds = filterDefined(
|
2022-02-27 21:37:04 +00:00
|
|
|
(followedFoldIds ?? []).map((id) => folds.find((fold) => fold.id === id))
|
2022-02-20 00:02:40 +00:00
|
|
|
)
|
|
|
|
|
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.
|
2022-02-27 21:37:04 +00:00
|
|
|
let yourContracts: Contract[] = []
|
2022-02-20 00:02:40 +00:00
|
|
|
if (yourBetContracts && followedFoldIds) {
|
|
|
|
// Show all contracts if no folds are followed.
|
2022-02-27 21:37:04 +00:00
|
|
|
if (followedFoldIds.length === 0) yourContracts = contracts
|
2022-02-20 00:02:40 +00:00
|
|
|
else
|
2022-02-27 21:37:04 +00:00
|
|
|
yourContracts = contracts.filter(
|
2022-02-20 00:02:40 +00:00
|
|
|
(contract) =>
|
|
|
|
contract.lowercaseTags.some((tag) => tagSet.has(tag)) ||
|
|
|
|
yourBetContracts.has(contract.id)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-27 21:37:04 +00:00
|
|
|
return {
|
|
|
|
yourContracts,
|
|
|
|
initialFollowedFoldSlugs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useFindActiveContracts = (props: {
|
|
|
|
contracts: Contract[]
|
|
|
|
recentBets: Bet[]
|
|
|
|
recentComments: Comment[]
|
|
|
|
}) => {
|
|
|
|
const { contracts, recentBets, recentComments } = props
|
|
|
|
|
2022-02-20 00:02:40 +00:00
|
|
|
const activeContracts = findActiveContracts(
|
2022-02-27 21:37:04 +00:00
|
|
|
contracts,
|
2022-02-20 00:02:40 +00:00
|
|
|
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,
|
|
|
|
}
|
2022-02-20 00:02:40 +00:00
|
|
|
}
|
2022-02-27 21:37:04 +00:00
|
|
|
|
|
|
|
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]))
|
|
|
|
}
|