From ee11a776b00e7c3bc7e371702f1a2235d74c0e98 Mon Sep 17 00:00:00 2001 From: Jvst Me Date: Tue, 27 Oct 2026 18:39:57 +0100 Subject: [PATCH] [UI]: Fix filters changed mid-load When filters changed while a list page was loading, the previous request could finish after the new one and overwrite the table with items matching the old filters. A pending "load more" request could likewise append outdated items to the new list, and the outdated request could reset the loading flag while the new request was still running. Track a generation counter in useInfiniteScroll, incremented on every list reset, and ignore responses to requests from earlier generations. --- frontend/src/hooks/useInfiniteScroll.ts | 41 ++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/frontend/src/hooks/useInfiniteScroll.ts b/frontend/src/hooks/useInfiniteScroll.ts index 5507cfb97e..39391e8418 100644 --- a/frontend/src/hooks/useInfiniteScroll.ts +++ b/frontend/src/hooks/useInfiniteScroll.ts @@ -37,25 +37,36 @@ export const useInfiniteScroll = ({ const lastRequestParams = useRef(undefined); const { limit, ...argsProp } = args; const lastArgsProps = useRef | null>(null); + // Incremented on every list reset, so that responses to outdated requests can be ignored + const requestGenerationRef = useRef(0); const [getItems, { isLoading, isFetching, error }] = useLazyQuery({ ...args } as Args); - const getDataRequest = async (params: Args) => { + const getDataRequest = async (params: Args, requestGeneration: number) => { const result = await getItems({ limit, ...params, } as Args).unwrap(); - lastRequestParams.current = { ...params }; + + if (requestGeneration === requestGenerationRef.current) { + lastRequestParams.current = { ...params }; + } + return result; }; const getEmptyList = () => { + const requestGeneration = ++requestGenerationRef.current; isLoadingRef.current = true; setData([]); - getDataRequest(argsProp as Args) + getDataRequest(argsProp as Args, requestGeneration) .then((result: LazyQueryResponse) => { + if (requestGeneration !== requestGenerationRef.current) { + return; + } + isDisabledMoreRef.current = false; if ('data' in result) { @@ -67,9 +78,17 @@ export const useInfiniteScroll = ({ } }) .catch(() => { + if (requestGeneration !== requestGenerationRef.current) { + return; + } + isDisabledMoreRef.current = true; }) .finally(() => { + if (requestGeneration !== requestGenerationRef.current) { + return; + } + isLoadingRef.current = false; }); }; @@ -92,10 +111,16 @@ export const useInfiniteScroll = ({ }; if (!isEqual(requestParams, lastRequestParams.current)) { + const requestGeneration = requestGenerationRef.current; + try { isLoadingRef.current = true; - const result = await getDataRequest(requestParams as Args); + const result = await getDataRequest(requestParams as Args, requestGeneration); + + if (requestGeneration !== requestGenerationRef.current) { + return; + } let listResponse: ListResponse; @@ -113,11 +138,17 @@ export const useInfiniteScroll = ({ isDisabledMoreRef.current = true; } } catch (e) { + if (requestGeneration !== requestGenerationRef.current) { + return; + } + console.log(e); } setTimeout(() => { - isLoadingRef.current = false; + if (requestGeneration === requestGenerationRef.current) { + isLoadingRef.current = false; + } }, 50); } };