2022-06-22 16:35:50 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { Group } from 'common/group'
|
|
|
|
import { User } from 'common/user'
|
|
|
|
import {
|
|
|
|
listenForGroup,
|
|
|
|
listenForGroups,
|
|
|
|
listenForMemberGroups,
|
2022-08-02 03:15:09 +00:00
|
|
|
listenForOpenGroups,
|
2022-07-22 17:34:10 +00:00
|
|
|
listGroups,
|
2022-06-22 16:35:50 +00:00
|
|
|
} from 'web/lib/firebase/groups'
|
2022-07-14 15:09:12 +00:00
|
|
|
import { getUser, getUsers } 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'
|
|
|
|
import { uniq } from 'lodash'
|
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-07-13 21:11:22 +00:00
|
|
|
export const useMemberGroups = (
|
|
|
|
userId: string | null | undefined,
|
2022-07-15 12:52:08 +00:00
|
|
|
options?: { withChatEnabled: boolean },
|
|
|
|
sort?: { by: 'mostRecentChatActivityTime' | 'mostRecentContractAddedTime' }
|
2022-07-13 21:11:22 +00:00
|
|
|
) => {
|
2022-06-22 16:35:50 +00:00
|
|
|
const [memberGroups, setMemberGroups] = useState<Group[] | undefined>()
|
|
|
|
useEffect(() => {
|
2022-07-13 21:11:22 +00:00
|
|
|
if (userId)
|
2022-07-15 12:52:08 +00:00
|
|
|
return listenForMemberGroups(
|
|
|
|
userId,
|
|
|
|
(groups) => {
|
|
|
|
if (options?.withChatEnabled)
|
|
|
|
return setMemberGroups(
|
|
|
|
filterDefined(
|
|
|
|
groups.filter((group) => group.chatDisabled !== true)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return setMemberGroups(groups)
|
|
|
|
},
|
|
|
|
sort
|
|
|
|
)
|
2022-07-15 13:28:04 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [options?.withChatEnabled, sort?.by, userId])
|
2022-06-22 16:35:50 +00:00
|
|
|
return memberGroups
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: We cache member group ids in localstorage to speed up the initial load
|
|
|
|
export const useMemberGroupIds = (user: User | null | undefined) => {
|
|
|
|
const [memberGroupIds, setMemberGroupIds] = useState<string[] | undefined>(
|
|
|
|
undefined
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (user) {
|
|
|
|
const key = `member-groups-${user.id}`
|
|
|
|
const memberGroupJson = localStorage.getItem(key)
|
|
|
|
if (memberGroupJson) {
|
|
|
|
setMemberGroupIds(JSON.parse(memberGroupJson))
|
|
|
|
}
|
|
|
|
|
|
|
|
return listenForMemberGroups(user.id, (Groups) => {
|
|
|
|
const groupIds = Groups.map((group) => group.id)
|
|
|
|
setMemberGroupIds(groupIds)
|
|
|
|
localStorage.setItem(key, JSON.stringify(groupIds))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [user])
|
|
|
|
|
|
|
|
return memberGroupIds
|
|
|
|
}
|
|
|
|
|
2022-07-13 22:20:56 +00:00
|
|
|
export function useMembers(group: Group, max?: number) {
|
2022-06-22 16:35:50 +00:00
|
|
|
const [members, setMembers] = useState<User[]>([])
|
|
|
|
useEffect(() => {
|
2022-06-24 23:38:39 +00:00
|
|
|
const { memberIds } = group
|
|
|
|
if (memberIds.length > 0) {
|
2022-07-13 22:20:56 +00:00
|
|
|
listMembers(group, max).then((members) => setMembers(members))
|
2022-06-24 23:38:39 +00:00
|
|
|
}
|
2022-07-13 22:20:56 +00:00
|
|
|
}, [group, max])
|
2022-06-22 16:35:50 +00:00
|
|
|
return members
|
|
|
|
}
|
2022-06-22 22:19:17 +00:00
|
|
|
|
2022-07-13 22:20:56 +00:00
|
|
|
export async function listMembers(group: Group, max?: number) {
|
2022-07-14 15:09:12 +00:00
|
|
|
const { memberIds } = group
|
|
|
|
const numToRetrieve = max ?? memberIds.length
|
|
|
|
if (memberIds.length === 0) return []
|
2022-07-15 12:52:08 +00:00
|
|
|
if (numToRetrieve > 100)
|
2022-07-14 15:09:12 +00:00
|
|
|
return (await getUsers()).filter((user) =>
|
|
|
|
group.memberIds.includes(user.id)
|
|
|
|
)
|
|
|
|
return await Promise.all(group.memberIds.slice(0, numToRetrieve).map(getUser))
|
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
|
|
|
|
}
|