Skip to content

Commit c9e3c8b

Browse files
authored
fix: dataset pagination state keeps resetting when filters changed (#15268)
1 parent 908a7b6 commit c9e3c8b

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

web/app/(commonLayout)/datasets/Datasets.tsx

+18-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { useEffect, useRef } from 'react'
3+
import { useCallback, useEffect, useRef } from 'react'
44
import useSWRInfinite from 'swr/infinite'
55
import { debounce } from 'lodash-es'
66
import { useTranslation } from 'react-i18next'
@@ -62,21 +62,28 @@ const Datasets = ({
6262
useEffect(() => {
6363
loadingStateRef.current = isLoading
6464
document.title = `${t('dataset.knowledge')} - Dify`
65-
}, [isLoading])
65+
}, [isLoading, t])
6666

67-
useEffect(() => {
68-
const onScroll = debounce(() => {
69-
if (!loadingStateRef.current) {
70-
const { scrollTop, clientHeight } = containerRef.current!
71-
const anchorOffset = anchorRef.current!.offsetTop
67+
const onScroll = useCallback(
68+
debounce(() => {
69+
if (!loadingStateRef.current && containerRef.current && anchorRef.current) {
70+
const { scrollTop, clientHeight } = containerRef.current
71+
const anchorOffset = anchorRef.current.offsetTop
7272
if (anchorOffset - scrollTop - clientHeight < 100)
7373
setSize(size => size + 1)
7474
}
75-
}, 50)
75+
}, 50),
76+
[setSize],
77+
)
7678

77-
containerRef.current?.addEventListener('scroll', onScroll)
78-
return () => containerRef.current?.removeEventListener('scroll', onScroll)
79-
}, [])
79+
useEffect(() => {
80+
const currentContainer = containerRef.current
81+
currentContainer?.addEventListener('scroll', onScroll)
82+
return () => {
83+
currentContainer?.removeEventListener('scroll', onScroll)
84+
onScroll.cancel()
85+
}
86+
}, [onScroll])
8087

8188
return (
8289
<nav className='grid content-start grid-cols-1 gap-4 px-12 pt-2 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 grow shrink-0'>

0 commit comments

Comments
 (0)