From 17453e56181a524f7be5165a889e808c98dbf389 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Sun, 18 Sep 2022 16:57:20 -0500 Subject: [PATCH] Improve hook that was spamming in dev --- web/hooks/use-group.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/web/hooks/use-group.ts b/web/hooks/use-group.ts index 59b36b27..9bcb59cd 100644 --- a/web/hooks/use-group.ts +++ b/web/hooks/use-group.ts @@ -105,16 +105,19 @@ export const useMemberGroupIds = (user: User | null | undefined) => { export function useMemberGroupsSubscription(user: User | null | undefined) { const cachedGroups = useMemberGroups(user?.id) ?? [] - const groupIds = useMemberGroupIds(user) const [groups, setGroups] = useState(cachedGroups) + const userId = user?.id useEffect(() => { - if (groupIds) { - Promise.all(groupIds.map((id) => getGroup(id))).then((groups) => - setGroups(filterDefined(groups)) - ) + if (userId) { + return listenForMemberGroupIds(userId, (groupIds) => { + Promise.all(groupIds.map((id) => getGroup(id))).then((groups) => + setGroups(filterDefined(groups)) + ) + }) } - }, [groupIds]) + }, [userId]) + return groups }