2022-05-12 15:07:10 +00:00
|
|
|
import _, { Dictionary } from 'lodash'
|
2022-05-01 16:36:54 +00:00
|
|
|
import { useState, useEffect } from 'react'
|
2022-05-12 15:07:10 +00:00
|
|
|
import type { feed } from 'common/feed'
|
2022-04-21 06:00:08 +00:00
|
|
|
import { useTimeSinceFirstRender } from './use-time-since-first-render'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { trackLatency } from 'web/lib/firebase/tracking'
|
|
|
|
import { User } from 'common/user'
|
2022-05-12 15:07:10 +00:00
|
|
|
import { getCategoryFeeds, getUserFeed } from 'web/lib/firebase/users'
|
2022-05-01 16:36:54 +00:00
|
|
|
import {
|
|
|
|
getRecentBetsAndComments,
|
|
|
|
getTopWeeklyContracts,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/lib/firebase/contracts'
|
2022-04-09 23:10:58 +00:00
|
|
|
|
2022-05-01 16:36:54 +00:00
|
|
|
export const useAlgoFeed = (user: User | null | undefined) => {
|
|
|
|
const [feed, setFeed] = useState<feed>()
|
2022-05-12 15:07:10 +00:00
|
|
|
const [categoryFeeds, setCategoryFeeds] = useState<Dictionary<feed>>()
|
2022-04-09 23:10:58 +00:00
|
|
|
|
2022-04-21 06:00:08 +00:00
|
|
|
const getTime = useTimeSinceFirstRender()
|
|
|
|
|
2022-04-09 23:10:58 +00:00
|
|
|
useEffect(() => {
|
2022-05-01 16:36:54 +00:00
|
|
|
if (user) {
|
|
|
|
getUserFeed(user.id).then((feed) => {
|
|
|
|
if (feed.length === 0) {
|
|
|
|
getDefaultFeed().then((feed) => setFeed(feed))
|
|
|
|
} else setFeed(feed)
|
|
|
|
|
|
|
|
trackLatency('feed', getTime())
|
|
|
|
console.log('feed load time', getTime())
|
|
|
|
})
|
2022-05-12 15:07:10 +00:00
|
|
|
|
|
|
|
getCategoryFeeds(user.id).then((feeds) => {
|
|
|
|
setCategoryFeeds(feeds)
|
|
|
|
console.log('category feeds load time', getTime())
|
|
|
|
})
|
2022-04-09 23:10:58 +00:00
|
|
|
}
|
2022-05-12 15:07:10 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-05-01 16:36:54 +00:00
|
|
|
}, [user?.id])
|
2022-04-09 23:10:58 +00:00
|
|
|
|
2022-05-12 15:07:10 +00:00
|
|
|
const followedCategory = user?.followedCategories?.[0] ?? 'all'
|
2022-04-09 23:10:58 +00:00
|
|
|
|
2022-05-12 15:07:10 +00:00
|
|
|
const followedFeed =
|
|
|
|
followedCategory === 'all' ? feed : categoryFeeds?.[followedCategory]
|
2022-04-09 23:10:58 +00:00
|
|
|
|
2022-05-12 15:07:10 +00:00
|
|
|
return followedFeed
|
2022-04-09 23:10:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 16:36:54 +00:00
|
|
|
const getDefaultFeed = async () => {
|
|
|
|
const contracts = await getTopWeeklyContracts()
|
|
|
|
const feed = await Promise.all(
|
|
|
|
contracts.map((c) => getRecentBetsAndComments(c))
|
2022-04-09 23:10:58 +00:00
|
|
|
)
|
2022-05-01 16:36:54 +00:00
|
|
|
return feed
|
2022-04-09 23:10:58 +00:00
|
|
|
}
|