2022-06-22 16:35:50 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { Group } from 'common/group'
|
|
|
|
import { User } from 'common/user'
|
|
|
|
import {
|
2022-09-17 23:30:27 +00:00
|
|
|
getGroup,
|
2022-09-08 06:36:34 +00:00
|
|
|
getMemberGroups,
|
2022-09-03 00:06:48 +00:00
|
|
|
GroupMemberDoc,
|
|
|
|
groupMembers,
|
2022-06-22 16:35:50 +00:00
|
|
|
listenForGroup,
|
2022-09-03 00:06:48 +00:00
|
|
|
listenForGroupContractDocs,
|
2022-06-22 16:35:50 +00:00
|
|
|
listenForGroups,
|
2022-09-03 00:06:48 +00:00
|
|
|
listenForMemberGroupIds,
|
2022-08-02 03:15:09 +00:00
|
|
|
listenForOpenGroups,
|
2022-07-22 17:34:10 +00:00
|
|
|
listGroups,
|
2022-09-16 21:12:24 +00:00
|
|
|
topFollowedGroupsQuery,
|
2022-06-22 16:35:50 +00:00
|
|
|
} from 'web/lib/firebase/groups'
|
2022-09-03 00:06:48 +00:00
|
|
|
import { getUser } from 'web/lib/firebase/users'
|
2022-07-13 21:11:22 +00:00
|
|
|
import { filterDefined } from 'common/util/array'
|
2022-07-22 17:34:10 +00:00
|
|
|
import { Contract } from 'common/contract'
|
2022-09-16 21:12:24 +00:00
|
|
|
import { keyBy, uniq, uniqBy } from 'lodash'
|
2022-09-03 00:06:48 +00:00
|
|
|
import { listenForValues } from 'web/lib/firebase/utils'
|
2022-09-08 06:36:34 +00:00
|
|
|
import { useQuery } from 'react-query'
|
2022-09-16 21:12:24 +00:00
|
|
|
import { useFirestoreQueryData } from '@react-query-firebase/firestore'
|
|
|
|
import { limit, query } from 'firebase/firestore'
|
|
|
|
import { useTrendingContracts } from './use-contracts'
|
2022-06-22 16:35:50 +00:00
|
|
|
|
|
|
|
export const useGroup = (groupId: string | undefined) => {
|
|
|
|
const [group, setGroup] = useState<Group | null | undefined>()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (groupId) return listenForGroup(groupId, setGroup)
|
|
|
|
}, [groupId])
|
|
|
|
|
|
|
|
return group
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useGroups = () => {
|
|
|
|
const [groups, setGroups] = useState<Group[] | undefined>()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return listenForGroups(setGroups)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return groups
|
|
|
|
}
|
|
|
|
|
2022-08-02 03:15:09 +00:00
|
|
|
export const useOpenGroups = () => {
|
|
|
|
const [groups, setGroups] = useState<Group[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return listenForOpenGroups(setGroups)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return groups
|
|
|
|
}
|
|
|
|
|
2022-09-16 21:12:24 +00:00
|
|
|
export const useTopFollowedGroups = (count: number) => {
|
|
|
|
const result = useFirestoreQueryData(
|
|
|
|
['top-followed-contracts', count],
|
|
|
|
query(topFollowedGroupsQuery, limit(count))
|
|
|
|
)
|
|
|
|
return result.data
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useTrendingGroups = () => {
|
|
|
|
const topGroups = useTopFollowedGroups(200)
|
|
|
|
const groupsById = keyBy(topGroups, 'id')
|
|
|
|
|
|
|
|
const trendingContracts = useTrendingContracts(200)
|
|
|
|
|
|
|
|
const groupLinks = uniqBy(
|
|
|
|
(trendingContracts ?? []).map((c) => c.groupLinks ?? []).flat(),
|
|
|
|
(link) => link.groupId
|
|
|
|
)
|
|
|
|
|
|
|
|
return filterDefined(
|
|
|
|
groupLinks.map((link) => groupsById[link.groupId])
|
|
|
|
).filter((group) => group.totalMembers >= 3)
|
|
|
|
}
|
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
export const useMemberGroups = (userId: string | null | undefined) => {
|
2022-09-08 06:36:34 +00:00
|
|
|
const result = useQuery(['member-groups', userId ?? ''], () =>
|
|
|
|
getMemberGroups(userId ?? '')
|
|
|
|
)
|
|
|
|
return result.data
|
2022-06-22 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useMemberGroupIds = (user: User | null | undefined) => {
|
2022-09-16 21:12:24 +00:00
|
|
|
const cachedGroups = useMemberGroups(user?.id)
|
|
|
|
|
2022-06-22 16:35:50 +00:00
|
|
|
const [memberGroupIds, setMemberGroupIds] = useState<string[] | undefined>(
|
2022-09-16 21:12:24 +00:00
|
|
|
cachedGroups?.map((g) => g.id)
|
2022-06-22 16:35:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (user) {
|
2022-09-03 00:06:48 +00:00
|
|
|
return listenForMemberGroupIds(user.id, (groupIds) => {
|
2022-06-22 16:35:50 +00:00
|
|
|
setMemberGroupIds(groupIds)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [user])
|
|
|
|
|
|
|
|
return memberGroupIds
|
|
|
|
}
|
|
|
|
|
2022-09-17 23:30:27 +00:00
|
|
|
export function useMemberGroupsSubscription(user: User | null | undefined) {
|
|
|
|
const cachedGroups = useMemberGroups(user?.id) ?? []
|
|
|
|
const [groups, setGroups] = useState(cachedGroups)
|
|
|
|
|
2022-09-18 21:57:20 +00:00
|
|
|
const userId = user?.id
|
2022-09-17 23:30:27 +00:00
|
|
|
useEffect(() => {
|
2022-09-18 21:57:20 +00:00
|
|
|
if (userId) {
|
|
|
|
return listenForMemberGroupIds(userId, (groupIds) => {
|
|
|
|
Promise.all(groupIds.map((id) => getGroup(id))).then((groups) =>
|
|
|
|
setGroups(filterDefined(groups))
|
|
|
|
)
|
|
|
|
})
|
2022-09-17 23:30:27 +00:00
|
|
|
}
|
2022-09-18 21:57:20 +00:00
|
|
|
}, [userId])
|
|
|
|
|
2022-09-17 23:30:27 +00:00
|
|
|
return groups
|
|
|
|
}
|
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
export function useMembers(groupId: string | undefined) {
|
2022-06-22 16:35:50 +00:00
|
|
|
const [members, setMembers] = useState<User[]>([])
|
|
|
|
useEffect(() => {
|
2022-09-03 00:06:48 +00:00
|
|
|
if (groupId)
|
|
|
|
listenForValues<GroupMemberDoc>(groupMembers(groupId), (memDocs) => {
|
|
|
|
const memberIds = memDocs.map((memDoc) => memDoc.userId)
|
|
|
|
Promise.all(memberIds.map((id) => getUser(id))).then((users) => {
|
|
|
|
setMembers(users)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}, [groupId])
|
2022-06-22 16:35:50 +00:00
|
|
|
return members
|
|
|
|
}
|
2022-06-22 22:19:17 +00:00
|
|
|
|
2022-09-03 00:06:48 +00:00
|
|
|
export function useMemberIds(groupId: string | null) {
|
|
|
|
const [memberIds, setMemberIds] = useState<string[]>([])
|
|
|
|
useEffect(() => {
|
|
|
|
if (groupId)
|
|
|
|
return listenForValues<GroupMemberDoc>(groupMembers(groupId), (docs) => {
|
|
|
|
setMemberIds(docs.map((doc) => doc.userId))
|
|
|
|
})
|
|
|
|
}, [groupId])
|
|
|
|
return memberIds
|
2022-06-24 23:38:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 17:34:10 +00:00
|
|
|
export const useGroupsWithContract = (contract: Contract) => {
|
|
|
|
const [groups, setGroups] = useState<Group[]>([])
|
2022-06-22 22:19:17 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-07-22 17:34:10 +00:00
|
|
|
if (contract.groupSlugs)
|
|
|
|
listGroups(uniq(contract.groupSlugs)).then((groups) =>
|
|
|
|
setGroups(filterDefined(groups))
|
|
|
|
)
|
|
|
|
}, [contract.groupSlugs])
|
2022-06-22 22:19:17 +00:00
|
|
|
|
|
|
|
return groups
|
|
|
|
}
|
2022-09-03 00:06:48 +00:00
|
|
|
|
|
|
|
export function useGroupContractIds(groupId: string) {
|
|
|
|
const [contractIds, setContractIds] = useState<string[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (groupId)
|
|
|
|
return listenForGroupContractDocs(groupId, (docs) =>
|
|
|
|
setContractIds(docs.map((doc) => doc.contractId))
|
|
|
|
)
|
|
|
|
}, [groupId])
|
|
|
|
|
|
|
|
return contractIds
|
|
|
|
}
|