Fix problem with useUpdatedContracts.
This commit is contained in:
parent
7758ec1d24
commit
8b60f56083
|
@ -1,12 +1,11 @@
|
||||||
import _ from 'lodash'
|
import _ from 'lodash'
|
||||||
import { useState, useEffect } from 'react'
|
import { useState, useEffect, useMemo } from 'react'
|
||||||
import { Bet } from '../../common/bet'
|
import { Bet } from '../../common/bet'
|
||||||
import { Comment } from '../../common/comment'
|
import { Comment } from '../../common/comment'
|
||||||
import { Contract } from '../../common/contract'
|
import { Contract } from '../../common/contract'
|
||||||
import { User } from '../../common/user'
|
import { User } from '../../common/user'
|
||||||
import { logInterpolation } from '../../common/util/math'
|
import { logInterpolation } from '../../common/util/math'
|
||||||
import { getRecommendedContracts } from '../../common/recommended-contracts'
|
import { getRecommendedContracts } from '../../common/recommended-contracts'
|
||||||
import { useUpdatedContracts } from './use-contracts'
|
|
||||||
import { useSeenContracts } from './use-seen-contracts'
|
import { useSeenContracts } from './use-seen-contracts'
|
||||||
import { useGetUserBetContractIds, useUserBetContracts } from './use-user-bets'
|
import { useGetUserBetContractIds, useUserBetContracts } from './use-user-bets'
|
||||||
|
|
||||||
|
@ -18,6 +17,10 @@ export const useAlgoFeed = (
|
||||||
recentBets: Bet[] | undefined,
|
recentBets: Bet[] | undefined,
|
||||||
recentComments: Comment[] | undefined
|
recentComments: Comment[] | undefined
|
||||||
) => {
|
) => {
|
||||||
|
const initialContracts = useMemo(() => contracts, [!!contracts])
|
||||||
|
const initialBets = useMemo(() => recentBets, [!!recentBets])
|
||||||
|
const initialComments = useMemo(() => recentComments, [!!recentComments])
|
||||||
|
|
||||||
const yourBetContractIds = useGetUserBetContractIds(user?.id)
|
const yourBetContractIds = useGetUserBetContractIds(user?.id)
|
||||||
// Update user bet contracts in local storage.
|
// Update user bet contracts in local storage.
|
||||||
useUserBetContracts(user?.id)
|
useUserBetContracts(user?.id)
|
||||||
|
@ -27,24 +30,28 @@ export const useAlgoFeed = (
|
||||||
const [algoFeed, setAlgoFeed] = useState<Contract[]>([])
|
const [algoFeed, setAlgoFeed] = useState<Contract[]>([])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (contracts && recentBets && recentComments) {
|
if (initialContracts && initialBets && initialComments) {
|
||||||
const eligibleContracts = contracts.filter(
|
const eligibleContracts = initialContracts.filter(
|
||||||
(c) => !c.isResolved && (c.closeTime ?? Infinity) > Date.now()
|
(c) => !c.isResolved && (c.closeTime ?? Infinity) > Date.now()
|
||||||
)
|
)
|
||||||
|
const contracts = getAlgoFeed(
|
||||||
setAlgoFeed(
|
eligibleContracts,
|
||||||
getAlgoFeed(
|
initialBets,
|
||||||
eligibleContracts,
|
initialComments,
|
||||||
recentBets,
|
yourBetContractIds,
|
||||||
recentComments,
|
seenContracts
|
||||||
yourBetContractIds,
|
|
||||||
seenContracts
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
setAlgoFeed(contracts)
|
||||||
}
|
}
|
||||||
}, [contracts, recentBets, recentComments, seenContracts, yourBetContractIds])
|
}, [
|
||||||
|
initialBets,
|
||||||
|
initialComments,
|
||||||
|
initialContracts,
|
||||||
|
seenContracts,
|
||||||
|
yourBetContractIds,
|
||||||
|
])
|
||||||
|
|
||||||
return useUpdatedContracts(algoFeed)
|
return algoFeed
|
||||||
}
|
}
|
||||||
|
|
||||||
const getAlgoFeed = (
|
const getAlgoFeed = (
|
||||||
|
|
|
@ -2,7 +2,6 @@ import { useEffect, useState } from 'react'
|
||||||
import { Contract } from '../../common/contract'
|
import { Contract } from '../../common/contract'
|
||||||
import {
|
import {
|
||||||
Bet,
|
Bet,
|
||||||
getRecentBets,
|
|
||||||
listenForBets,
|
listenForBets,
|
||||||
listenForRecentBets,
|
listenForRecentBets,
|
||||||
withoutAnteBets,
|
withoutAnteBets,
|
||||||
|
@ -37,11 +36,3 @@ export const useRecentBets = () => {
|
||||||
useEffect(() => listenForRecentBets(setRecentBets), [])
|
useEffect(() => listenForRecentBets(setRecentBets), [])
|
||||||
return recentBets
|
return recentBets
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useGetRecentBets = () => {
|
|
||||||
const [recentBets, setRecentBets] = useState<Bet[] | undefined>()
|
|
||||||
useEffect(() => {
|
|
||||||
getRecentBets().then(setRecentBets)
|
|
||||||
}, [])
|
|
||||||
return recentBets
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import {
|
import {
|
||||||
Comment,
|
Comment,
|
||||||
getRecentComments,
|
|
||||||
listenForComments,
|
listenForComments,
|
||||||
listenForRecentComments,
|
listenForRecentComments,
|
||||||
} from '../lib/firebase/comments'
|
} from '../lib/firebase/comments'
|
||||||
|
@ -21,11 +20,3 @@ export const useRecentComments = () => {
|
||||||
useEffect(() => listenForRecentComments(setRecentComments), [])
|
useEffect(() => listenForRecentComments(setRecentComments), [])
|
||||||
return recentComments
|
return recentComments
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useGetRecentComments = () => {
|
|
||||||
const [recentComments, setRecentComments] = useState<Comment[] | undefined>()
|
|
||||||
useEffect(() => {
|
|
||||||
getRecentComments().then(setRecentComments)
|
|
||||||
}, [])
|
|
||||||
return recentComments
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ import _ from 'lodash'
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import {
|
import {
|
||||||
Contract,
|
Contract,
|
||||||
getActiveContracts,
|
|
||||||
listenForActiveContracts,
|
listenForActiveContracts,
|
||||||
listenForContracts,
|
listenForContracts,
|
||||||
listenForHotContracts,
|
listenForHotContracts,
|
||||||
|
@ -30,16 +29,6 @@ export const useActiveContracts = () => {
|
||||||
return contracts
|
return contracts
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useGetActiveContracts = () => {
|
|
||||||
const [contracts, setContracts] = useState<Contract[] | undefined>()
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
getActiveContracts().then(setContracts)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
return contracts
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useInactiveContracts = () => {
|
export const useInactiveContracts = () => {
|
||||||
const [contracts, setContracts] = useState<Contract[] | undefined>()
|
const [contracts, setContracts] = useState<Contract[] | undefined>()
|
||||||
|
|
||||||
|
@ -50,19 +39,6 @@ export const useInactiveContracts = () => {
|
||||||
return contracts
|
return contracts
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useUpdatedContracts = (initialContracts: Contract[]) => {
|
|
||||||
const [contracts, setContracts] = useState(initialContracts)
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
return listenForContracts((newContracts) => {
|
|
||||||
const contractMap = _.fromPairs(newContracts.map((c) => [c.id, c]))
|
|
||||||
setContracts(initialContracts.map((c) => contractMap[c.id]))
|
|
||||||
})
|
|
||||||
}, [initialContracts])
|
|
||||||
|
|
||||||
return contracts
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useTaggedContracts = (tags: string[] | undefined) => {
|
export const useTaggedContracts = (tags: string[] | undefined) => {
|
||||||
const [contracts, setContracts] = useState<Contract[] | undefined>(
|
const [contracts, setContracts] = useState<Contract[] | undefined>(
|
||||||
tags && tags.length === 0 ? [] : undefined
|
tags && tags.length === 0 ? [] : undefined
|
||||||
|
|
|
@ -9,27 +9,24 @@ import { Spacer } from '../components/layout/spacer'
|
||||||
import { Col } from '../components/layout/col'
|
import { Col } from '../components/layout/col'
|
||||||
import { useUser } from '../hooks/use-user'
|
import { useUser } from '../hooks/use-user'
|
||||||
import { LoadingIndicator } from '../components/loading-indicator'
|
import { LoadingIndicator } from '../components/loading-indicator'
|
||||||
import { useGetRecentBets, useRecentBets } from '../hooks/use-bets'
|
import { useRecentBets } from '../hooks/use-bets'
|
||||||
import { useGetActiveContracts } from '../hooks/use-contracts'
|
import { useActiveContracts } from '../hooks/use-contracts'
|
||||||
import { useGetRecentComments, useRecentComments } from '../hooks/use-comments'
|
import { useRecentComments } from '../hooks/use-comments'
|
||||||
import { useAlgoFeed } from '../hooks/use-algo-feed'
|
import { useAlgoFeed } from '../hooks/use-algo-feed'
|
||||||
|
|
||||||
const Home = () => {
|
const Home = () => {
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
|
|
||||||
const initialContracts = useGetActiveContracts()
|
const contracts = useActiveContracts()
|
||||||
|
const contractsDict = _.keyBy(contracts, 'id')
|
||||||
|
|
||||||
const initialRecentBets = useGetRecentBets()
|
const recentBets = useRecentBets()
|
||||||
const recentBets = useRecentBets() ?? initialRecentBets
|
const recentComments = useRecentComments()
|
||||||
|
|
||||||
const initialRecentComments = useGetRecentComments()
|
const feedContracts = useAlgoFeed(user, contracts, recentBets, recentComments)
|
||||||
const recentComments = useRecentComments() ?? initialRecentComments
|
|
||||||
|
|
||||||
const feedContracts = useAlgoFeed(
|
const updatedContracts = feedContracts.map(
|
||||||
user,
|
(contract) => contractsDict[contract.id] ?? contract
|
||||||
initialContracts,
|
|
||||||
initialRecentBets,
|
|
||||||
initialRecentComments
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (user === null) {
|
if (user === null) {
|
||||||
|
@ -38,9 +35,9 @@ const Home = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const activityContent =
|
const activityContent =
|
||||||
initialContracts && recentBets && recentComments ? (
|
contracts && recentBets && recentComments ? (
|
||||||
<ActivityFeed
|
<ActivityFeed
|
||||||
contracts={feedContracts}
|
contracts={updatedContracts}
|
||||||
recentBets={recentBets}
|
recentBets={recentBets}
|
||||||
recentComments={recentComments}
|
recentComments={recentComments}
|
||||||
mode="only-recent"
|
mode="only-recent"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user