mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-08-12 20:39:03 +08:00
### What problem does this PR solve? fix: fetch file list by react-query #1306 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
parent
ac7a0d4fbf
commit
bf2ea04d02
@ -1,10 +1,21 @@
|
|||||||
|
import { ResponseType } from '@/interfaces/database/base';
|
||||||
import {
|
import {
|
||||||
IConnectRequestBody,
|
IConnectRequestBody,
|
||||||
IFileListRequestBody,
|
IFileListRequestBody,
|
||||||
} from '@/interfaces/request/file-manager';
|
} from '@/interfaces/request/file-manager';
|
||||||
import { UploadFile } from 'antd';
|
import fileManagerService from '@/services/file-manager-service';
|
||||||
import { useCallback } from 'react';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { useDispatch, useSelector } from 'umi';
|
import { PaginationProps, UploadFile } from 'antd';
|
||||||
|
import React, { useCallback } from 'react';
|
||||||
|
import { useDispatch, useSearchParams, useSelector } from 'umi';
|
||||||
|
import { useGetNextPagination, useHandleSearchChange } from './logic-hooks';
|
||||||
|
|
||||||
|
export const useGetFolderId = () => {
|
||||||
|
const [searchParams] = useSearchParams();
|
||||||
|
const id = searchParams.get('folderId') as string;
|
||||||
|
|
||||||
|
return id ?? '';
|
||||||
|
};
|
||||||
|
|
||||||
export const useFetchFileList = () => {
|
export const useFetchFileList = () => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
@ -22,6 +33,56 @@ export const useFetchFileList = () => {
|
|||||||
return fetchFileList;
|
return fetchFileList;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export interface IListResult {
|
||||||
|
searchString: string;
|
||||||
|
handleInputChange: React.ChangeEventHandler<HTMLInputElement>;
|
||||||
|
pagination: PaginationProps;
|
||||||
|
setPagination: (pagination: { page: number; pageSize: number }) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useFetchNextFileList = (): ResponseType<any> & IListResult => {
|
||||||
|
const { searchString, handleInputChange } = useHandleSearchChange();
|
||||||
|
const { pagination, setPagination } = useGetNextPagination();
|
||||||
|
const id = useGetFolderId();
|
||||||
|
|
||||||
|
const { data } = useQuery({
|
||||||
|
queryKey: [
|
||||||
|
'fetchFileList',
|
||||||
|
id,
|
||||||
|
pagination.current,
|
||||||
|
pagination.pageSize,
|
||||||
|
searchString,
|
||||||
|
],
|
||||||
|
initialData: {},
|
||||||
|
queryFn: async () => {
|
||||||
|
const { data } = await fileManagerService.listFile({
|
||||||
|
parent_id: id,
|
||||||
|
keywords: searchString,
|
||||||
|
page_size: pagination.pageSize,
|
||||||
|
page: pagination.current,
|
||||||
|
});
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const onInputChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(
|
||||||
|
(e) => {
|
||||||
|
setPagination({ page: 1 });
|
||||||
|
handleInputChange(e);
|
||||||
|
},
|
||||||
|
[handleInputChange, setPagination],
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...data,
|
||||||
|
searchString,
|
||||||
|
handleInputChange: onInputChange,
|
||||||
|
pagination: { ...pagination, total: data?.data?.total },
|
||||||
|
setPagination,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const useRemoveFile = () => {
|
export const useRemoveFile = () => {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
|
@ -71,6 +71,20 @@ export const useSetSelectedRecord = <T = IKnowledgeFile>() => {
|
|||||||
return { currentRecord, setRecord };
|
return { currentRecord, setRecord };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useHandleSearchChange = () => {
|
||||||
|
const [searchString, setSearchString] = useState('');
|
||||||
|
|
||||||
|
const handleInputChange = useCallback(
|
||||||
|
(e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
||||||
|
const value = e.target.value;
|
||||||
|
setSearchString(value);
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
return { handleInputChange, searchString };
|
||||||
|
};
|
||||||
|
|
||||||
export const useChangeLanguage = () => {
|
export const useChangeLanguage = () => {
|
||||||
const { i18n } = useTranslation();
|
const { i18n } = useTranslation();
|
||||||
const saveSetting = useSaveSetting();
|
const saveSetting = useSaveSetting();
|
||||||
@ -85,6 +99,46 @@ export const useChangeLanguage = () => {
|
|||||||
return changeLanguage;
|
return changeLanguage;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useGetNextPagination = () => {
|
||||||
|
const { t } = useTranslate('common');
|
||||||
|
const [{ page, pageSize }, setPagination] = useState({
|
||||||
|
page: 1,
|
||||||
|
pageSize: 10,
|
||||||
|
});
|
||||||
|
|
||||||
|
const onPageChange: PaginationProps['onChange'] = useCallback(
|
||||||
|
(pageNumber: number, pageSize: number) => {
|
||||||
|
setPagination({ page: pageNumber, pageSize });
|
||||||
|
},
|
||||||
|
[setPagination],
|
||||||
|
);
|
||||||
|
|
||||||
|
const setCurrentPagination = useCallback(
|
||||||
|
(pagination: { page: number; pageSize?: number }) => {
|
||||||
|
setPagination((p) => ({ ...p, ...pagination }));
|
||||||
|
},
|
||||||
|
[setPagination],
|
||||||
|
);
|
||||||
|
|
||||||
|
const pagination: PaginationProps = useMemo(() => {
|
||||||
|
return {
|
||||||
|
showQuickJumper: true,
|
||||||
|
total: 0,
|
||||||
|
showSizeChanger: true,
|
||||||
|
current: page,
|
||||||
|
pageSize: pageSize,
|
||||||
|
pageSizeOptions: [1, 2, 10, 20, 50, 100],
|
||||||
|
onChange: onPageChange,
|
||||||
|
showTotal: (total) => `${t('total')} ${total}`,
|
||||||
|
};
|
||||||
|
}, [t, onPageChange, page, pageSize]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
pagination,
|
||||||
|
setPagination: setCurrentPagination,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const useGetPagination = (
|
export const useGetPagination = (
|
||||||
total: number,
|
total: number,
|
||||||
page: number,
|
page: number,
|
||||||
|
@ -19,17 +19,19 @@ import {
|
|||||||
} from 'antd';
|
} from 'antd';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import {
|
import {
|
||||||
useFetchDocumentListOnMount,
|
|
||||||
useHandleBreadcrumbClick,
|
useHandleBreadcrumbClick,
|
||||||
useHandleDeleteFile,
|
useHandleDeleteFile,
|
||||||
useHandleSearchChange,
|
|
||||||
useSelectBreadcrumbItems,
|
useSelectBreadcrumbItems,
|
||||||
} from './hooks';
|
} from './hooks';
|
||||||
|
|
||||||
import { useSelectParentFolderList } from '@/hooks/file-manager-hooks';
|
import {
|
||||||
|
IListResult,
|
||||||
|
useSelectParentFolderList,
|
||||||
|
} from '@/hooks/file-manager-hooks';
|
||||||
import styles from './index.less';
|
import styles from './index.less';
|
||||||
|
|
||||||
interface IProps {
|
interface IProps
|
||||||
|
extends Pick<IListResult, 'searchString' | 'handleInputChange'> {
|
||||||
selectedRowKeys: string[];
|
selectedRowKeys: string[];
|
||||||
showFolderCreateModal: () => void;
|
showFolderCreateModal: () => void;
|
||||||
showFileUploadModal: () => void;
|
showFileUploadModal: () => void;
|
||||||
@ -41,10 +43,10 @@ const FileToolbar = ({
|
|||||||
showFolderCreateModal,
|
showFolderCreateModal,
|
||||||
showFileUploadModal,
|
showFileUploadModal,
|
||||||
setSelectedRowKeys,
|
setSelectedRowKeys,
|
||||||
|
searchString,
|
||||||
|
handleInputChange,
|
||||||
}: IProps) => {
|
}: IProps) => {
|
||||||
const { t } = useTranslate('knowledgeDetails');
|
const { t } = useTranslate('knowledgeDetails');
|
||||||
useFetchDocumentListOnMount();
|
|
||||||
const { handleInputChange, searchString } = useHandleSearchChange();
|
|
||||||
const breadcrumbItems = useSelectBreadcrumbItems();
|
const breadcrumbItems = useSelectBreadcrumbItems();
|
||||||
const { handleBreadcrumbClick } = useHandleBreadcrumbClick();
|
const { handleBreadcrumbClick } = useHandleBreadcrumbClick();
|
||||||
const parentFolderList = useSelectParentFolderList();
|
const parentFolderList = useSelectParentFolderList();
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { useSelectFileList } from '@/hooks/file-manager-hooks';
|
import { useFetchNextFileList } from '@/hooks/file-manager-hooks';
|
||||||
import { IFile } from '@/interfaces/database/file-manager';
|
import { IFile } from '@/interfaces/database/file-manager';
|
||||||
import { formatDate } from '@/utils/date';
|
import { formatDate } from '@/utils/date';
|
||||||
import { Button, Flex, Space, Table, Tag, Typography } from 'antd';
|
import { Button, Flex, Space, Table, Tag, Typography } from 'antd';
|
||||||
@ -6,7 +6,6 @@ import { ColumnsType } from 'antd/es/table';
|
|||||||
import ActionCell from './action-cell';
|
import ActionCell from './action-cell';
|
||||||
import FileToolbar from './file-toolbar';
|
import FileToolbar from './file-toolbar';
|
||||||
import {
|
import {
|
||||||
useGetFilesPagination,
|
|
||||||
useGetRowSelection,
|
useGetRowSelection,
|
||||||
useHandleConnectToKnowledge,
|
useHandleConnectToKnowledge,
|
||||||
useHandleCreateFolder,
|
useHandleCreateFolder,
|
||||||
@ -30,7 +29,7 @@ const { Text } = Typography;
|
|||||||
|
|
||||||
const FileManager = () => {
|
const FileManager = () => {
|
||||||
const { t } = useTranslate('fileManager');
|
const { t } = useTranslate('fileManager');
|
||||||
const fileList = useSelectFileList();
|
// const fileList = useSelectFileList();
|
||||||
const { rowSelection, setSelectedRowKeys } = useGetRowSelection();
|
const { rowSelection, setSelectedRowKeys } = useGetRowSelection();
|
||||||
const loading = useSelectFileListLoading();
|
const loading = useSelectFileListLoading();
|
||||||
const navigateToOtherFolder = useNavigateToOtherFolder();
|
const navigateToOtherFolder = useNavigateToOtherFolder();
|
||||||
@ -64,8 +63,9 @@ const FileManager = () => {
|
|||||||
initialValue,
|
initialValue,
|
||||||
connectToKnowledgeLoading,
|
connectToKnowledgeLoading,
|
||||||
} = useHandleConnectToKnowledge();
|
} = useHandleConnectToKnowledge();
|
||||||
const { pagination } = useGetFilesPagination();
|
// const { pagination } = useGetFilesPagination();
|
||||||
|
const { pagination, data, searchString, handleInputChange } =
|
||||||
|
useFetchNextFileList();
|
||||||
const columns: ColumnsType<IFile> = [
|
const columns: ColumnsType<IFile> = [
|
||||||
{
|
{
|
||||||
title: t('name'),
|
title: t('name'),
|
||||||
@ -151,13 +151,15 @@ const FileManager = () => {
|
|||||||
return (
|
return (
|
||||||
<section className={styles.fileManagerWrapper}>
|
<section className={styles.fileManagerWrapper}>
|
||||||
<FileToolbar
|
<FileToolbar
|
||||||
|
searchString={searchString}
|
||||||
|
handleInputChange={handleInputChange}
|
||||||
selectedRowKeys={rowSelection.selectedRowKeys as string[]}
|
selectedRowKeys={rowSelection.selectedRowKeys as string[]}
|
||||||
showFolderCreateModal={showFolderCreateModal}
|
showFolderCreateModal={showFolderCreateModal}
|
||||||
showFileUploadModal={showFileUploadModal}
|
showFileUploadModal={showFileUploadModal}
|
||||||
setSelectedRowKeys={setSelectedRowKeys}
|
setSelectedRowKeys={setSelectedRowKeys}
|
||||||
></FileToolbar>
|
></FileToolbar>
|
||||||
<Table
|
<Table
|
||||||
dataSource={fileList}
|
dataSource={data?.files}
|
||||||
columns={columns}
|
columns={columns}
|
||||||
rowKey={'id'}
|
rowKey={'id'}
|
||||||
rowSelection={rowSelection}
|
rowSelection={rowSelection}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user