Skip to content

Commit

Permalink
perf scroll components (labring#2676)
Browse files Browse the repository at this point in the history
* perf: add scroll list && virtualist (labring#2665)

* perf: chatHistorySlider add virtualList

* perf: dataCard add scroll

* fix: ts

* perf: scroll list components

* perf: hook refresh

---------

Co-authored-by: papapatrick <[email protected]>
  • Loading branch information
2 people authored and shilin66 committed Sep 12, 2024
1 parent 91fb2f7 commit 5de3692
Show file tree
Hide file tree
Showing 17 changed files with 409 additions and 331 deletions.
185 changes: 97 additions & 88 deletions packages/web/hooks/usePagination.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { useRef, useState, useCallback, useMemo, useEffect } from 'react';
import { IconButton, Flex, Box, Input } from '@chakra-ui/react';
import { useRef, useState, useCallback, useMemo } from 'react';
import { IconButton, Flex, Box, Input, BoxProps } from '@chakra-ui/react';
import { ArrowBackIcon, ArrowForwardIcon } from '@chakra-ui/icons';
import { useMutation } from '@tanstack/react-query';
import { useTranslation } from 'next-i18next';
import { throttle } from 'lodash';
import { useToast } from './useToast';
import { getErrText } from '@fastgpt/global/common/error/utils';

const thresholdVal = 100;
import {
useBoolean,
useLockFn,
useMemoizedFn,
useRequest,
useScroll,
useThrottleEffect
} from 'ahooks';

type PagingData<T> = {
pageNum: number;
Expand All @@ -23,54 +27,67 @@ export function usePagination<ResT = any>({
defaultRequest = true,
type = 'button',
onChange,
elementRef
refreshDeps
}: {
api: (data: any) => Promise<PagingData<ResT>>;
pageSize?: number;
params?: Record<string, any>;
defaultRequest?: boolean;
type?: 'button' | 'scroll';
onChange?: (pageNum: number) => void;
elementRef?: React.RefObject<HTMLDivElement>;
refreshDeps?: any[];
}) {
const { toast } = useToast();
const { t } = useTranslation();
const [pageNum, setPageNum] = useState(1);
const pageNumRef = useRef(pageNum);
pageNumRef.current = pageNum;

const ScrollContainerRef = useRef<HTMLDivElement>(null);

const noMore = useRef(false);

const [isLoading, { setTrue, setFalse }] = useBoolean(false);

const [total, setTotal] = useState(0);
const totalRef = useRef(total);
totalRef.current = total;
const [data, setData] = useState<ResT[]>([]);
const dataLengthRef = useRef(data.length);
dataLengthRef.current = data.length;

const maxPage = useMemo(() => Math.ceil(total / pageSize) || 1, [pageSize, total]);

const { mutate, isLoading } = useMutation({
mutationFn: async (num: number = pageNum) => {
try {
const res: PagingData<ResT> = await api({
pageNum: num,
pageSize,
...params
});
setPageNum(num);
res.total !== undefined && setTotal(res.total);
if (type === 'scroll') {
setData((prevData) => [...prevData, ...res.data]);
} else {
setData(res.data);
}
onChange && onChange(num);
} catch (error: any) {
toast({
title: getErrText(error, t('common:core.chat.error.data_error')),
status: 'error'
});
console.log(error);
const fetchData = useLockFn(async (num: number = pageNum) => {
if (noMore.current && num !== 1) return;
setTrue();

try {
const res: PagingData<ResT> = await api({
pageNum: num,
pageSize,
...params
});

// Check total and set
res.total !== undefined && setTotal(res.total);

if (res.total !== undefined && res.total <= data.length + res.data.length) {
noMore.current = true;
}

setPageNum(num);

if (type === 'scroll') {
setData((prevData) => (num === 1 ? res.data : [...prevData, ...res.data]));
} else {
setData(res.data);
}
return null;

onChange?.(num);
} catch (error: any) {
toast({
title: getErrText(error, t('common:core.chat.error.data_error')),
status: 'error'
});
console.log(error);
}

setFalse();
});

const Pagination = useCallback(() => {
Expand All @@ -82,7 +99,7 @@ export function usePagination<ResT = any>({
aria-label={'left'}
size={'smSquare'}
isLoading={isLoading}
onClick={() => mutate(pageNum - 1)}
onClick={() => fetchData(pageNum - 1)}
/>
<Flex mx={2} alignItems={'center'}>
<Input
Expand All @@ -97,11 +114,11 @@ export function usePagination<ResT = any>({
const val = +e.target.value;
if (val === pageNum) return;
if (val >= maxPage) {
mutate(maxPage);
fetchData(maxPage);
} else if (val < 1) {
mutate(1);
fetchData(1);
} else {
mutate(+e.target.value);
fetchData(+e.target.value);
}
}}
onKeyDown={(e) => {
Expand All @@ -110,11 +127,11 @@ export function usePagination<ResT = any>({
if (val && e.key === 'Enter') {
if (val === pageNum) return;
if (val >= maxPage) {
mutate(maxPage);
fetchData(maxPage);
} else if (val < 1) {
mutate(1);
fetchData(1);
} else {
mutate(val);
fetchData(val);
}
}
}}
Expand All @@ -130,22 +147,35 @@ export function usePagination<ResT = any>({
isLoading={isLoading}
w={'28px'}
h={'28px'}
onClick={() => mutate(pageNum + 1)}
onClick={() => fetchData(pageNum + 1)}
/>
</Flex>
);
}, [isLoading, maxPage, mutate, pageNum]);
}, [isLoading, maxPage, fetchData, pageNum]);

// Reload data
const { runAsync: refresh } = useRequest(
async () => {
setData([]);
defaultRequest && fetchData(1);
},
{
manual: false,
refreshDeps,
throttleWait: 100
}
);

const ScrollData = useCallback(
({ children, ...props }: { children: React.ReactNode }) => {
const loadText = useMemo(() => {
const ScrollData = useMemoizedFn(
({ children, ...props }: { children: React.ReactNode } & BoxProps) => {
const loadText = (() => {
if (isLoading) return t('common:common.is_requesting');
if (total <= data.length) return t('common:common.request_end');
return t('common:common.request_more');
}, []);
})();

return (
<Box {...props} ref={elementRef} overflow={'overlay'}>
<Box {...props} ref={ScrollContainerRef} overflow={'overlay'}>
{children}
<Box
mt={2}
Expand All @@ -155,51 +185,29 @@ export function usePagination<ResT = any>({
cursor={loadText === t('common:common.request_more') ? 'pointer' : 'default'}
onClick={() => {
if (loadText !== t('common:common.request_more')) return;
mutate(pageNum + 1);
fetchData(pageNum + 1);
}}
>
{loadText}
</Box>
</Box>
);
},
[data.length, isLoading, mutate, pageNum, total]
}
);

useEffect(() => {
if (!elementRef?.current || type !== 'scroll') return;

const scrolling = throttle((e: Event) => {
const element = e.target as HTMLDivElement;
if (!element) return;
// 当前滚动位置
const scrollTop = element.scrollTop;
// 可视高度
const clientHeight = element.clientHeight;
// 内容总高度
const scrollHeight = element.scrollHeight;
// 判断是否滚动到底部
if (
scrollTop + clientHeight + thresholdVal >= scrollHeight &&
dataLengthRef.current < totalRef.current
) {
mutate(pageNumRef.current + 1);
// Scroll check
const scroll = useScroll(ScrollContainerRef);
useThrottleEffect(
() => {
if (!ScrollContainerRef?.current || type !== 'scroll' || total === 0) return;
const { scrollTop, scrollHeight, clientHeight } = ScrollContainerRef.current;
if (scrollTop + clientHeight >= scrollHeight - 100) {
fetchData(pageNum + 1);
}
}, 100);

const handleScroll = (e: Event) => {
scrolling(e);
};

elementRef.current.addEventListener('scroll', handleScroll);
return () => {
elementRef.current?.removeEventListener('scroll', handleScroll);
};
}, [elementRef, mutate, pageNum, type, total, data.length]);

useEffect(() => {
defaultRequest && mutate(1);
}, []);
},
[scroll],
{ wait: 50 }
);

return {
pageNum,
Expand All @@ -210,6 +218,7 @@ export function usePagination<ResT = any>({
isLoading,
Pagination,
ScrollData,
getData: mutate
getData: fetchData,
refresh
};
}
Loading

0 comments on commit 5de3692

Please sign in to comment.