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-03-21 01:07:45 +00:00
|
|
|
import {
|
|
|
|
getUser,
|
|
|
|
listenForAllUsers,
|
|
|
|
listenForPrivateUsers,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/lib/firebase/users'
|
2022-06-08 04:42:42 +00:00
|
|
|
import { useUser } from './use-user'
|
|
|
|
import { groupBy, sortBy, difference } from 'lodash'
|
|
|
|
import { getContractsOfUserBets } from 'web/lib/firebase/bets'
|
|
|
|
import { useFollows } from './use-follows'
|
2022-01-16 03:09:15 +00:00
|
|
|
|
|
|
|
export const useUsers = () => {
|
|
|
|
const [users, setUsers] = useState<User[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
listenForAllUsers(setUsers)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return users
|
|
|
|
}
|
2022-01-24 06:48:06 +00:00
|
|
|
|
2022-03-21 22:19:42 +00:00
|
|
|
export const useUserById = (userId?: string) => {
|
2022-03-21 01:07:45 +00:00
|
|
|
const [user, setUser] = useState<User | undefined>(undefined)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-03-21 22:19:42 +00:00
|
|
|
if (userId) {
|
|
|
|
getUser(userId).then(setUser)
|
|
|
|
}
|
2022-03-21 01:07:45 +00:00
|
|
|
}, [userId])
|
|
|
|
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2022-01-24 06:48:06 +00:00
|
|
|
export const usePrivateUsers = () => {
|
|
|
|
const [users, setUsers] = useState<PrivateUser[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
listenForPrivateUsers(setUsers)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return users
|
|
|
|
}
|
2022-06-08 04:42:42 +00:00
|
|
|
|
|
|
|
export const useDiscoverUsers = () => {
|
|
|
|
const user = useUser()
|
|
|
|
|
|
|
|
const [discoverUserIds, setDiscoverUserIds] = useState<string[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (user)
|
|
|
|
getContractsOfUserBets(user.id).then((contracts) => {
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}, [user])
|
|
|
|
|
|
|
|
const followedUserIds = useFollows(user?.id)
|
|
|
|
const nonSuggestions = [user?.id ?? '', ...(followedUserIds ?? [])]
|
|
|
|
|
|
|
|
return difference(discoverUserIds, nonSuggestions).slice(0, 50)
|
|
|
|
}
|