From 2eab7b39f75ddd6e219fc88b44b9de6848a18d90 Mon Sep 17 00:00:00 2001 From: ~latter-bolden Date: Wed, 15 Nov 2023 13:30:30 -0500 Subject: [PATCH 1/2] groups: only invalidate groups on gang updates if a gang was removed --- ui/src/logic/useReactQuerySubscription.tsx | 6 ++-- ui/src/state/groups/groups.ts | 37 ++++++++++++++++++---- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/ui/src/logic/useReactQuerySubscription.tsx b/ui/src/logic/useReactQuerySubscription.tsx index 0b75c21362..be0a2dd3a0 100644 --- a/ui/src/logic/useReactQuerySubscription.tsx +++ b/ui/src/logic/useReactQuerySubscription.tsx @@ -16,6 +16,7 @@ export default function useReactQuerySubscription({ scry, scryApp = app, priority = 3, + onEvent, options, }: { queryKey: QueryKey; @@ -24,6 +25,7 @@ export default function useReactQuerySubscription({ scry: string; scryApp?: string; priority?: number; + onEvent?: (data: Event) => void; options?: UseQueryOptions; }) { const queryClient = useQueryClient(); @@ -50,9 +52,9 @@ export default function useReactQuerySubscription({ api.subscribe({ app, path, - event: invalidate.current, + event: onEvent ? onEvent : invalidate.current, }); - }, [app, path, queryClient, queryKey]); + }, [app, path, queryClient, queryKey, onEvent]); return useQuery(queryKey, fetchData, { staleTime: 60 * 1000, diff --git a/ui/src/state/groups/groups.ts b/ui/src/state/groups/groups.ts index 958eaa23b2..0005a7c5bd 100644 --- a/ui/src/state/groups/groups.ts +++ b/ui/src/state/groups/groups.ts @@ -1,6 +1,6 @@ import _ from 'lodash'; import { useParams } from 'react-router'; -import { useCallback, useEffect, useMemo } from 'react'; +import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import create from 'zustand'; import { MutationFunction, @@ -248,18 +248,41 @@ const defGang = { export function useGangs() { const queryClient = useQueryClient(); + const queryKey = ['gangs']; + + const invalidate = useRef( + _.debounce( + () => { + queryClient.invalidateQueries(queryKey); + }, + 300, + { leading: true, trailing: true } + ) + ); + const { data, ...rest } = useReactQuerySubscription({ - queryKey: ['gangs'], + queryKey, app: 'groups', path: `/gangs/updates`, scry: `/gangs`, + onEvent: (event) => { + // right now for long group joins, the gang is initially removed but no fact about the corresponding created + // group is emitted until the join completes. This is a blunt hack to ensure our view of existing groups remains up to date. + // Once this is fixed, we should remove this and use the default useReactQuerySubscription event handler + const currGangCount = Object.keys( + queryClient.getQueryData(queryKey) || {} + ).length; + const newGangCount = Object.keys(event || {}).length; + const gangWasRemoved = + currGangCount && newGangCount && newGangCount < currGangCount; + if (gangWasRemoved) { + queryClient.invalidateQueries(['groups']); + } + + invalidate.current(); + }, options: { refetchOnMount: false, - onSuccess: () => { - // TEMPORARY: right now for long group joins, the gang is initially removed but no fact about the corresponding created - // group is emitted until the join completes. This is a blunt hack to ensure our view of existing groups remains up to date - queryClient.invalidateQueries(['groups']); - }, }, }); From 03acd96b89d2ea5d07d2f01a07ec5715985ba730 Mon Sep 17 00:00:00 2001 From: ~latter-bolden Date: Wed, 15 Nov 2023 13:40:40 -0500 Subject: [PATCH 2/2] groups: remove unneeded dep --- ui/src/state/groups/groups.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/src/state/groups/groups.ts b/ui/src/state/groups/groups.ts index 0005a7c5bd..37ecb8c7db 100644 --- a/ui/src/state/groups/groups.ts +++ b/ui/src/state/groups/groups.ts @@ -1,6 +1,6 @@ import _ from 'lodash'; import { useParams } from 'react-router'; -import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { useCallback, useEffect, useMemo, useRef } from 'react'; import create from 'zustand'; import { MutationFunction,