Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(acceptance): stop + Acceptance flicker while polling (#21039)

Rdmclin21b94dcd87003
diff --git a/src/features/Acceptance/hooks.test.ts b/src/features/Acceptance/hooks.test.ts
index 77c29125cd9..cb8dd581980 100644
--- a/src/features/Acceptance/hooks.test.ts
+++ b/src/features/Acceptance/hooks.test.ts
@@ -73,6 +73,40 @@ describe('Verify data hooks', () => {
expect(getAcceptanceBySubject).toHaveBeenCalledWith('task', 'T-231');
});
+ it('keeps a subject without an acceptance out of loading while it polls', async () => {
+ const getAcceptanceBySubject = vi
+ .spyOn(verifyService, 'getAcceptanceBySubject')
+ .mockResolvedValue(null as never);
+
+ const { result } = renderHook(() => useAcceptanceBySubject('task', 'T-864'), {
+ wrapper: createSWRWrapper(new Map()),
+ });
+
+ expect(result.current.isLoading).toBe(true);
+ await waitFor(() => expect(result.current.isLoading).toBe(false));
+
+ // A background poll of a subject known to have no acceptance must not flip
+ // the caller back to its loading placeholder on every tick.
+ let resolveNext!: (value: null) => void;
+ getAcceptanceBySubject.mockReturnValueOnce(
+ new Promise((resolve) => {
+ resolveNext = resolve;
+ }) as never,
+ );
+ let revalidation!: Promise<unknown>;
+ act(() => {
+ revalidation = result.current.mutate();
+ });
+ await waitFor(() => expect(result.current.isValidating).toBe(true));
+ expect(result.current.isLoading).toBe(false);
+
+ await act(async () => {
+ resolveNext(null);
+ await revalidation;
+ });
+ expect(result.current.isLoading).toBe(false);
+ });
+
it('forwards the project scope to the acceptance list service', async () => {
const listAcceptances = vi.spyOn(verifyService, 'listAcceptances').mockResolvedValue([]);
diff --git a/src/features/Acceptance/hooks.ts b/src/features/Acceptance/hooks.ts
index 06b15668d1a..7b8bfa77f5d 100644
--- a/src/features/Acceptance/hooks.ts
+++ b/src/features/Acceptance/hooks.ts
@@ -1,5 +1,5 @@
import type { AcceptanceSubjectType } from '@lobechat/types';
-import { useCallback, useEffect } from 'react';
+import { useCallback, useEffect, useRef, useState } from 'react';
import useSWRInfinite from 'swr/infinite';
import { useActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';
@@ -101,11 +101,30 @@ export const useAcceptanceBySubject = (
refreshInterval: getAcceptanceBySubjectRefreshInterval(data),
});
+ // A subject without an acceptance polls every 2s. Once a request for this key
+ // has settled, "no acceptance" is a known answer: later polls must not flip the
+ // caller back to its loading placeholder (that swapped the task's Acceptance
+ // trigger for a spinner on every tick and made the section jump).
+ const validatingKeyRef = useRef<string | undefined>(undefined);
+ const [settledKey, setSettledKey] = useState<string>();
+ useEffect(() => {
+ if (!key) return;
+ if (sync.isValidating) {
+ validatingKeyRef.current = key;
+ return;
+ }
+ if (validatingKeyRef.current === key) setSettledKey(key);
+ }, [key, sync.isValidating]);
+
return {
data,
error: sync.error,
// A read with no subject has nothing to load — never report it as loading.
- isLoading: !!subjectId && !data && !sync.error && (sync.isValidating || !sync.isHydrated),
+ isLoading:
+ !!subjectId &&
+ !data &&
+ !sync.error &&
+ (!sync.isHydrated || (sync.isValidating && settledKey !== key)),
isValidating: sync.isValidating,
mutate: async () => {
await sync.revalidate();