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-06-22 22:19:17 +00:00
|
|
|
getGroupsWithContractId,
|
2022-06-22 16:35:50 +00:00
|
|
|
listenForGroup,
|
|
|
|
listenForGroups,
|
|
|
|
listenForMemberGroups,
|
|
|
|
} from 'web/lib/firebase/groups'
|
|
|
|
import { getUser } from 'web/lib/firebase/users'
|
|
|
|
|
|
|
|
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-06-29 16:00:43 +00:00
|
|
|
export const useMemberGroups = (userId: string | null | undefined) => {
|
2022-06-22 16:35:50 +00:00
|
|
|
const [memberGroups, setMemberGroups] = useState<Group[] | undefined>()
|
|
|
|
useEffect(() => {
|
2022-06-29 16:00:43 +00:00
|
|
|
if (userId) return listenForMemberGroups(userId, setMemberGroups)
|
|
|
|
}, [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
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useMembers(group: Group) {
|
|
|
|
const [members, setMembers] = useState<User[]>([])
|
|
|
|
useEffect(() => {
|
2022-06-24 23:38:39 +00:00
|
|
|
const { memberIds } = group
|
|
|
|
if (memberIds.length > 0) {
|
|
|
|
listMembers(group).then((members) => setMembers(members))
|
|
|
|
}
|
2022-06-22 16:35:50 +00:00
|
|
|
}, [group])
|
|
|
|
return members
|
|
|
|
}
|
2022-06-22 22:19:17 +00:00
|
|
|
|
2022-06-24 23:38:39 +00:00
|
|
|
export async function listMembers(group: Group) {
|
2022-07-01 22:37:30 +00:00
|
|
|
return await Promise.all(group.memberIds.map(getUser))
|
2022-06-24 23:38:39 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 22:19:17 +00:00
|
|
|
export const useGroupsWithContract = (contractId: string | undefined) => {
|
|
|
|
const [groups, setGroups] = useState<Group[] | null | undefined>()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (contractId) getGroupsWithContractId(contractId, setGroups)
|
|
|
|
}, [contractId])
|
|
|
|
|
|
|
|
return groups
|
|
|
|
}
|