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-16 02:57:50 +00:00
|
|
|
export const useAlgoFeed = (
|
|
|
|
user: User | null | undefined,
|
|
|
|
category: string
|
|
|
|
) => {
|
|
|
|
const [allFeed, setAllFeed] = useState<feed>()
|
2022-05-22 08:36:05 +00:00
|
|
|
const [categoryFeeds, setCategoryFeeds] = useState<{ [x: string]: 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) {
|
2022-05-16 02:57:50 +00:00
|
|
|
getDefaultFeed().then((feed) => setAllFeed(feed))
|
|
|
|
} else setAllFeed(feed)
|
2022-05-01 16:36:54 +00:00
|
|
|
|
|
|
|
trackLatency('feed', getTime())
|
2022-05-16 02:57:50 +00:00
|
|
|
console.log('"all" feed load time', getTime())
|
2022-05-01 16:36:54 +00:00
|
|
|
})
|
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-16 02:57:50 +00:00
|
|
|
const feed = category === 'all' ? allFeed : categoryFeeds?.[category]
|
2022-04-09 23:10:58 +00:00
|
|
|
|
2022-05-16 02:57:50 +00:00
|
|
|
return feed
|
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
|
|
|
}
|