2022-01-16 03:09:15 +00:00
|
|
|
import { useState, useEffect } from 'react'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { PrivateUser, User } from 'common/user'
|
2022-06-08 04:42:42 +00:00
|
|
|
import { groupBy, sortBy, difference } from 'lodash'
|
|
|
|
import { getContractsOfUserBets } from 'web/lib/firebase/bets'
|
|
|
|
import { useFollows } from './use-follows'
|
2022-06-10 17:28:02 +00:00
|
|
|
import { useUser } from './use-user'
|
2022-07-20 23:31:18 +00:00
|
|
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
|
|
|
import { DocumentData } from 'firebase/firestore'
|
|
|
|
import { users, privateUsers } from 'web/lib/firebase/users'
|
2022-01-16 03:09:15 +00:00
|
|
|
|
|
|
|
export const useUsers = () => {
|
2022-07-20 23:31:18 +00:00
|
|
|
const result = useFirestoreQueryData<DocumentData, User[]>(['users'], users, {
|
|
|
|
subscribe: true,
|
|
|
|
includeMetadataChanges: true,
|
|
|
|
})
|
|
|
|
return result.data ?? []
|
2022-01-16 03:09:15 +00:00
|
|
|
}
|
2022-01-24 06:48:06 +00:00
|
|
|
|
|
|
|
export const usePrivateUsers = () => {
|
2022-07-20 23:31:18 +00:00
|
|
|
const result = useFirestoreQueryData<DocumentData, PrivateUser[]>(
|
|
|
|
['private users'],
|
|
|
|
privateUsers,
|
|
|
|
{ subscribe: true, includeMetadataChanges: true }
|
|
|
|
)
|
|
|
|
return result.data || []
|
2022-01-24 06:48:06 +00:00
|
|
|
}
|
2022-06-08 04:42:42 +00:00
|
|
|
|
2022-06-08 19:38:04 +00:00
|
|
|
export const useDiscoverUsers = (userId: string | null | undefined) => {
|
2022-06-08 04:42:42 +00:00
|
|
|
const [discoverUserIds, setDiscoverUserIds] = useState<string[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-08 19:38:04 +00:00
|
|
|
if (userId)
|
|
|
|
getContractsOfUserBets(userId).then((contracts) => {
|
2022-06-08 04:42:42 +00:00
|
|
|
const creatorCounts = Object.entries(
|
|
|
|
groupBy(contracts, 'creatorId')
|
|
|
|
).map(([id, contracts]) => [id, contracts.length] as const)
|
|
|
|
|
|
|
|
const topCreatorIds = sortBy(creatorCounts, ([_, i]) => i)
|
|
|
|
.map(([id]) => id)
|
|
|
|
.reverse()
|
|
|
|
|
|
|
|
setDiscoverUserIds(topCreatorIds)
|
|
|
|
})
|
2022-06-08 19:38:04 +00:00
|
|
|
}, [userId])
|
2022-06-08 04:42:42 +00:00
|
|
|
|
2022-06-10 17:28:02 +00:00
|
|
|
const user = useUser()
|
|
|
|
const followedUserIds = useFollows(user?.id)
|
|
|
|
const nonSuggestions = [user?.id ?? '', ...(followedUserIds ?? [])]
|
2022-06-08 04:42:42 +00:00
|
|
|
|
|
|
|
return difference(discoverUserIds, nonSuggestions).slice(0, 50)
|
|
|
|
}
|