manifold/web/hooks/use-algo-feed.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

import _ from 'lodash'
2022-04-28 17:28:52 +00:00
import { useState, useEffect } from 'react'
import { Bet } from '../../common/bet'
import { Comment } from '../../common/comment'
import { Contract } from '../../common/contract'
import { useTimeSinceFirstRender } from './use-time-since-first-render'
import { trackLatency } from '../lib/firebase/tracking'
import { User } from '../../common/user'
import { getUserFeed } from '../lib/firebase/users'
2022-04-30 23:02:39 +00:00
import { useUpdatedContracts } from './use-contracts'
type feed = {
contract: Contract
recentBets: Bet[]
recentComments: Comment[]
}[]
export const useAlgoFeed = (user: User | null | undefined) => {
2022-04-30 23:02:39 +00:00
const [feed, setFeed] = useState<feed>()
const getTime = useTimeSinceFirstRender()
useEffect(() => {
if (user) {
getUserFeed(user.id).then((feed) => {
setFeed(feed)
trackLatency('feed', getTime())
console.log('feed load time', getTime())
})
}
2022-04-30 23:02:39 +00:00
}, [user?.id])
return useUpdateFeed(feed)
}
const useUpdateFeed = (feed: feed | undefined) => {
const contracts = useUpdatedContracts(feed?.map((item) => item.contract))
2022-04-30 23:02:39 +00:00
return feed && contracts
? feed.map(({ contract, ...other }, i) => ({
...other,
contract: contracts[i],
}))
: undefined
}