Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
newfish-cmyk committed Jan 15, 2025
1 parent 8871c0e commit 8ad542b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 196 deletions.
27 changes: 12 additions & 15 deletions docSite/content/zh-cn/docs/guide/knowledge_base/api_dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type FileListItem = {
- parentId - 父级 id,可选,或者 null。
- searchKey - 检索词,可选
- pageSize - 每页显示的数据项的数量
- pageToken - 分页标记,第一次请求不填,表示从头开始遍历;分页查询结果还有更多项时会同时返回新的 pageToken,如果没有更多项则不返回
- offset - 偏移量
{{% /alert %}}
```bash
Expand All @@ -73,7 +73,7 @@ curl --location --request POST '{{baseURL}}/v1/file/list' \
"parentId": null,
"searchKey": "",
"pageSize": 15,
"pageToken": ""
"offset": 0
}'
```

Expand All @@ -88,19 +88,16 @@ curl --location --request POST '{{baseURL}}/v1/file/list' \
"code": 200,
"success": true,
"message": "",
"data": {
"files":[
{
"id": "xxxx",
"parentId": "xxxx",
"type": "file", // file | folder
"name":"test.json",
"updateTime":"2024-11-26T03:05:24.759Z",
"createTime":"2024-11-26T03:05:24.759Z"
}
],
"nextPageToken": "ljCp8KywpEEw5x-wqjCoELDrAfCrXrCpsOcw4wFM2"
}
"data":[
{
"id": "xxxx",
"parentId": "xxxx",
"type": "file", // file | folder
"name":"test.json",
"updateTime":"2024-11-26T03:05:24.759Z",
"createTime":"2024-11-26T03:05:24.759Z"
}
]
}
```

Expand Down
5 changes: 0 additions & 5 deletions packages/global/core/dataset/apiDataset.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ export type APIFileServer = {
authorization: string;
};

export type APIFileListResponse = {
files: APIFileItem[];
nextPageToken: string;
};

export type APIFileContentResponse = {
content?: string;
previewUrl?: string;
Expand Down
13 changes: 6 additions & 7 deletions packages/service/core/dataset/apiDataset/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
APIFileContentResponse,
APIFileListResponse,
APIFileItem,
APIFileReadResponse,
APIFileServer
} from '@fastgpt/global/core/dataset/apiDataset';
Expand Down Expand Up @@ -80,21 +80,21 @@ export const useApiDatasetRequest = ({ apiServer }: { apiServer: APIFileServer }
const listFiles = async ({
searchKey,
parentId,
pageToken = '',
offset,
pageSize
}: {
searchKey?: string;
parentId?: ParentIdType;
pageToken: string;
offset?: number;
pageSize: number;
}) => {
const { files, nextPageToken } = await request<APIFileListResponse>(
const files = await request<APIFileItem[]>(
`/v1/file/list`,
{
searchKey,
parentId,
pageSize,
pageToken
offset
},
'POST'
);
Expand All @@ -112,8 +112,7 @@ export const useApiDatasetRequest = ({ apiServer }: { apiServer: APIFileServer }
}));

return {
list: formattedFiles,
nextPageToken
list: formattedFiles
};
};

Expand Down
4 changes: 2 additions & 2 deletions packages/web/common/fetch/type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type PaginationResponse<T = {}> = {
};

export type TokenPaginationProps = {
pageToken: string;
pageToken?: string;
pageSize: number;
};

export type TokenPaginationResponse<T = any> = {
nextPageToken: string;
nextPageToken?: string;
list: T[];
};
187 changes: 30 additions & 157 deletions packages/web/hooks/useScrollPagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ export function useVirtualScrollPagination<
}

export function useScrollPagination<
TParams extends PaginationProps,
TData extends PaginationResponse
TParams extends PaginationProps | TokenPaginationProps,
TData extends PaginationResponse | TokenPaginationResponse
>(
api: (data: TParams) => Promise<TData>,
{
Expand All @@ -194,7 +194,8 @@ export function useScrollPagination<
pageSize = 10,
params = {},
EmptyTip,
showErrorToast = true
showErrorToast = true,
isTokenPagination = false
}: {
refreshDeps?: any[];
scrollLoadType?: 'top' | 'bottom';
Expand All @@ -203,36 +204,46 @@ export function useScrollPagination<
params?: Record<string, any>;
EmptyTip?: React.JSX.Element;
showErrorToast?: boolean;
isTokenPagination?: boolean;
}
) {
const { t } = useTranslation();
const { toast } = useToast();

const [data, setData] = useState<TData['list']>([]);
const [data, setData] = useState<any[]>([]);
const [total, setTotal] = useState(0);
const [pageToken, setPageToken] = useState<string>('');
const [isLoading, { setTrue, setFalse }] = useBoolean(false);
const isEmpty = total === 0 && !isLoading;

const noMore = data.length >= total;
const isEmpty = isTokenPagination ? data.length === 0 : total === 0 && !isLoading;
const noMore = isTokenPagination ? !pageToken : data.length >= total;

const loadData = useLockFn(
async (init = false, ScrollContainerRef?: RefObject<HTMLDivElement>) => {
if (noMore && !init) return;

const offset = init ? 0 : data.length;

setTrue();

try {
const res = await api({
offset,
pageSize,
...params
} as TParams);

setTotal(res.total);
let res;
if (isTokenPagination) {
res = await api({
pageToken: init ? '' : pageToken,
pageSize,
...params
} as TParams);
setPageToken((res as TokenPaginationResponse).nextPageToken || '');
} else {
const offset = init ? 0 : data.length;
res = await api({
offset,
pageSize,
...params
} as TParams);
setTotal((res as PaginationResponse).total);
}

if (scrollLoadType === 'top') {
if (scrollLoadType === 'top' && !isTokenPagination) {
const prevHeight = ScrollContainerRef?.current?.scrollHeight || 0;
const prevScrollTop = ScrollContainerRef?.current?.scrollTop || 0;
// 使用 requestAnimationFrame 来调整滚动位置
Expand All @@ -250,10 +261,10 @@ export function useScrollPagination<
);
}

setData((prevData) => (offset === 0 ? res.list : [...res.list, ...prevData]));
setData((prevData) => (init ? res.list : [...res.list, ...prevData]));
adjustScrollPosition();
} else {
setData((prevData) => (offset === 0 ? res.list : [...prevData, ...res.list]));
setData((prevData) => (init ? res.list : [...prevData, ...res.list]));
}
} catch (error: any) {
if (showErrorToast) {
Expand Down Expand Up @@ -355,145 +366,7 @@ export function useScrollPagination<
return {
ScrollData,
isLoading,
total: Math.max(total, data.length),
data,
setData,
fetchData: loadData,
refreshList
};
}

export function useTokenScrollPagination<
TParams extends TokenPaginationProps,
TData extends TokenPaginationResponse
>(
api: (data: TParams) => Promise<TData>,
{
refreshDeps,

pageSize = 10,
params = {},
EmptyTip,
showErrorToast = true
}: {
refreshDeps?: any[];

pageSize?: number;
params?: Record<string, any>;
EmptyTip?: React.JSX.Element;
showErrorToast?: boolean;
}
) {
const { t } = useTranslation();
const { toast } = useToast();

const [data, setData] = useState<TData['list']>([]);
const [isLoading, { setTrue, setFalse }] = useBoolean(false);

const [pageToken, setPageToken] = useState<string>('');
const noMore = !pageToken;
const isEmpty = data.length === 0;

const loadData = useLockFn(async (init = false) => {
if (noMore && !init) return;

setTrue();

try {
const res = await api({
pageToken: init ? '' : pageToken,
pageSize,
...params
} as TParams);

setPageToken(res.nextPageToken);
setData((prevData) => (init ? res.list : [...prevData, ...res.list]));
} catch (error: any) {
if (showErrorToast) {
toast({
title: getErrText(error, t('common:core.chat.error.data_error')),
status: 'error'
});
}
console.log(error);
}

setFalse();
});

let ScrollRef = useRef<HTMLDivElement>(null);
const ScrollData = useMemoizedFn(
({
children,
...props
}: {
children: ReactNode;
} & BoxProps) => {
const ref = ScrollRef;
const loadText = useMemo(() => {
if (isLoading) return t('common:common.is_requesting');
if (noMore) return t('common:common.request_end');
return t('common:common.request_more');
}, [isLoading, noMore]);

const scroll = useScroll(ref);

// Watch scroll position
useThrottleEffect(
() => {
if (!ref?.current || noMore) return;
const { scrollTop, scrollHeight, clientHeight } = ref.current;

if (scrollTop + clientHeight >= scrollHeight - thresholdVal) {
loadData(false);
}
},
[scroll],
{ wait: 50 }
);

return (
<Box {...props} ref={ref}>
{children}
{!isEmpty && (
<Box
mt={2}
fontSize={'xs'}
color={'blackAlpha.500'}
textAlign={'center'}
cursor={loadText === t('common:common.request_more') ? 'pointer' : 'default'}
onClick={() => {
if (loadText !== t('common:common.request_more')) return;
loadData(false);
}}
>
{loadText}
</Box>
)}
{isEmpty && EmptyTip}
</Box>
);
}
);

// Reload data
useRequest(
async () => {
loadData(true);
},
{
manual: false,
refreshDeps
}
);

const refreshList = useMemoizedFn(() => {
loadData(true);
});

return {
ScrollData,
isLoading,
total: isTokenPagination ? undefined : Math.max(total, data.length),
data,
setData,
fetchData: loadData,
Expand Down
Loading

0 comments on commit 8ad542b

Please sign in to comment.