|
1 | 1 | 'use client'
|
2 | 2 |
|
3 |
| -import { useEffect, useRef } from 'react' |
| 3 | +import { useCallback, useEffect, useRef } from 'react' |
4 | 4 | import useSWRInfinite from 'swr/infinite'
|
5 | 5 | import { debounce } from 'lodash-es'
|
6 | 6 | import { useTranslation } from 'react-i18next'
|
@@ -62,21 +62,28 @@ const Datasets = ({
|
62 | 62 | useEffect(() => {
|
63 | 63 | loadingStateRef.current = isLoading
|
64 | 64 | document.title = `${t('dataset.knowledge')} - Dify`
|
65 |
| - }, [isLoading]) |
| 65 | + }, [isLoading, t]) |
66 | 66 |
|
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 |
72 | 72 | if (anchorOffset - scrollTop - clientHeight < 100)
|
73 | 73 | setSize(size => size + 1)
|
74 | 74 | }
|
75 |
| - }, 50) |
| 75 | + }, 50), |
| 76 | + [setSize], |
| 77 | + ) |
76 | 78 |
|
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]) |
80 | 87 |
|
81 | 88 | return (
|
82 | 89 | <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