From 8322cbbd9f66aed224736f98e662c3fd3e274641 Mon Sep 17 00:00:00 2001 From: Nguyen Huu Tho Date: Sat, 8 Mar 2025 14:38:35 +0700 Subject: [PATCH 1/2] fix: dataset pagination state keeps resetting when filters changed --- web/app/(commonLayout)/datasets/Datasets.tsx | 29 ++++++++++++-------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/web/app/(commonLayout)/datasets/Datasets.tsx b/web/app/(commonLayout)/datasets/Datasets.tsx index ea918a2b17d763..f889963e66f78a 100644 --- a/web/app/(commonLayout)/datasets/Datasets.tsx +++ b/web/app/(commonLayout)/datasets/Datasets.tsx @@ -1,6 +1,6 @@ 'use client' -import { useEffect, useRef } from 'react' +import { useEffect, useRef, useCallback } from 'react' import useSWRInfinite from 'swr/infinite' import { debounce } from 'lodash-es' import { useTranslation } from 'react-i18next' @@ -62,21 +62,28 @@ const Datasets = ({ useEffect(() => { loadingStateRef.current = isLoading document.title = `${t('dataset.knowledge')} - Dify` - }, [isLoading]) + }, [isLoading, t]) - useEffect(() => { - const onScroll = debounce(() => { - if (!loadingStateRef.current) { - const { scrollTop, clientHeight } = containerRef.current! - const anchorOffset = anchorRef.current!.offsetTop + const onScroll = useCallback( + debounce(() => { + if (!loadingStateRef.current && containerRef.current && anchorRef.current) { + const { scrollTop, clientHeight } = containerRef.current + const anchorOffset = anchorRef.current.offsetTop if (anchorOffset - scrollTop - clientHeight < 100) setSize(size => size + 1) } - }, 50) + }, 50), + [setSize] + ) - containerRef.current?.addEventListener('scroll', onScroll) - return () => containerRef.current?.removeEventListener('scroll', onScroll) - }, []) + useEffect(() => { + const currentContainer = containerRef.current + currentContainer?.addEventListener('scroll', onScroll) + return () => { + currentContainer?.removeEventListener('scroll', onScroll) + onScroll.cancel() + } + }, [onScroll]) return (