From a2a0dc61162d0d3070b23b60106d21de79a2fb85 Mon Sep 17 00:00:00 2001 From: balibabu Date: Thu, 25 Apr 2024 08:46:18 +0800 Subject: [PATCH] feat: upload file in FileManager #345 (#529) ### What problem does this PR solve? feat: upload file in FileManager #345 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --- web/src/hooks/fileManagerHooks.ts | 50 +++++- web/src/interfaces/database/file-manager.ts | 2 +- web/src/interfaces/request/file-manager.ts | 9 ++ web/src/locales/en.ts | 2 +- web/src/locales/zh-traditional.ts | 2 +- web/src/locales/zh.ts | 2 +- .../pages/file-manager/action-cell/index.tsx | 18 ++- .../connect-to-knowledge-modal/index.tsx | 58 +++++++ web/src/pages/file-manager/file-toolbar.tsx | 10 +- .../file-manager/file-upload-modal/index.tsx | 143 +++++++++++++----- web/src/pages/file-manager/hooks.ts | 88 +++++++++++ web/src/pages/file-manager/index.tsx | 34 +++++ web/src/pages/file-manager/model.ts | 27 ++++ .../user-setting/setting-model/index.tsx | 2 +- web/src/services/fileManagerService.ts | 5 + web/src/utils/api.ts | 1 + web/src/utils/fileUtil.ts | 9 ++ 17 files changed, 412 insertions(+), 50 deletions(-) create mode 100644 web/src/pages/file-manager/connect-to-knowledge-modal/index.tsx diff --git a/web/src/hooks/fileManagerHooks.ts b/web/src/hooks/fileManagerHooks.ts index 8410aa74c6..0c863e8a3f 100644 --- a/web/src/hooks/fileManagerHooks.ts +++ b/web/src/hooks/fileManagerHooks.ts @@ -1,4 +1,8 @@ -import { IFileListRequestBody } from '@/interfaces/request/file-manager'; +import { + IConnectRequestBody, + IFileListRequestBody, +} from '@/interfaces/request/file-manager'; +import { UploadFile } from 'antd'; import { useCallback } from 'react'; import { useDispatch, useSelector } from 'umi'; @@ -94,3 +98,47 @@ export const useSelectParentFolderList = () => { ); return parentFolderList.toReversed(); }; + +export const useUploadFile = () => { + const dispatch = useDispatch(); + + const uploadFile = useCallback( + (file: UploadFile, parentId: string, path: string) => { + try { + return dispatch({ + type: 'fileManager/uploadFile', + payload: { + file, + parentId, + path, + }, + }); + } catch (errorInfo) { + console.log('Failed:', errorInfo); + } + }, + [dispatch], + ); + + return uploadFile; +}; + +export const useConnectToKnowledge = () => { + const dispatch = useDispatch(); + + const uploadFile = useCallback( + (payload: IConnectRequestBody) => { + try { + return dispatch({ + type: 'fileManager/connectFileToKnowledge', + payload, + }); + } catch (errorInfo) { + console.log('Failed:', errorInfo); + } + }, + [dispatch], + ); + + return uploadFile; +}; diff --git a/web/src/interfaces/database/file-manager.ts b/web/src/interfaces/database/file-manager.ts index d22df392ce..8bbcf77fed 100644 --- a/web/src/interfaces/database/file-manager.ts +++ b/web/src/interfaces/database/file-manager.ts @@ -3,7 +3,7 @@ export interface IFile { create_time: number; created_by: string; id: string; - kb_ids: string[]; + kbs_info: { kb_id: string; kb_name: string }[]; location: string; name: string; parent_id: string; diff --git a/web/src/interfaces/request/file-manager.ts b/web/src/interfaces/request/file-manager.ts index 7e748ca9c7..0a0928bf8f 100644 --- a/web/src/interfaces/request/file-manager.ts +++ b/web/src/interfaces/request/file-manager.ts @@ -3,3 +3,12 @@ import { IPaginationRequestBody } from './base'; export interface IFileListRequestBody extends IPaginationRequestBody { parent_id?: string; // folder id } + +interface BaseRequestBody { + parentId: string; +} + +export interface IConnectRequestBody extends BaseRequestBody { + fileIds: string[]; + kbIds: string[]; +} diff --git a/web/src/locales/en.ts b/web/src/locales/en.ts index 18b94cd4e4..e23684613f 100644 --- a/web/src/locales/en.ts +++ b/web/src/locales/en.ts @@ -382,7 +382,7 @@ export default { passwordDescription: 'Please enter your current password to change your password.', model: 'Model Providers', - modelDescription: 'Manage your account settings and preferences here.', + modelDescription: 'Set the model parameter and API Key here.', team: 'Team', logout: 'Log out', username: 'Username', diff --git a/web/src/locales/zh-traditional.ts b/web/src/locales/zh-traditional.ts index 21ba2781dd..e6b500e1b5 100644 --- a/web/src/locales/zh-traditional.ts +++ b/web/src/locales/zh-traditional.ts @@ -352,7 +352,7 @@ export default { password: '密碼', passwordDescription: '請輸入您當前的密碼以更改您的密碼。', model: '模型提供商', - modelDescription: '在此管理您的帳戶設置和首選項。', + modelDescription: '在此設置模型參數和 API Key。', team: '團隊', logout: '登出', username: '使用者名稱', diff --git a/web/src/locales/zh.ts b/web/src/locales/zh.ts index 439be394ee..72595257d1 100644 --- a/web/src/locales/zh.ts +++ b/web/src/locales/zh.ts @@ -369,7 +369,7 @@ export default { password: '密码', passwordDescription: '请输入您当前的密码以更改您的密码。', model: '模型提供商', - modelDescription: '在此管理您的帐户设置和首选项。', + modelDescription: '在此设置模型参数和 API Key。', team: '团队', logout: '登出', username: '用户名', diff --git a/web/src/pages/file-manager/action-cell/index.tsx b/web/src/pages/file-manager/action-cell/index.tsx index 518a50763d..2cd390e760 100644 --- a/web/src/pages/file-manager/action-cell/index.tsx +++ b/web/src/pages/file-manager/action-cell/index.tsx @@ -17,9 +17,15 @@ interface IProps { record: IFile; setCurrentRecord: (record: any) => void; showRenameModal: (record: IFile) => void; + showConnectToKnowledgeModal: (ids: string[]) => void; } -const ActionCell = ({ record, setCurrentRecord, showRenameModal }: IProps) => { +const ActionCell = ({ + record, + setCurrentRecord, + showRenameModal, + showConnectToKnowledgeModal, +}: IProps) => { const documentId = record.id; const beingUsed = false; const { t } = useTranslate('knowledgeDetails'); @@ -41,9 +47,17 @@ const ActionCell = ({ record, setCurrentRecord, showRenameModal }: IProps) => { showRenameModal(record); }; + const onShowConnectToKnowledgeModal = () => { + showConnectToKnowledgeModal([documentId]); + }; + return ( - diff --git a/web/src/pages/file-manager/connect-to-knowledge-modal/index.tsx b/web/src/pages/file-manager/connect-to-knowledge-modal/index.tsx new file mode 100644 index 0000000000..4c10f9d558 --- /dev/null +++ b/web/src/pages/file-manager/connect-to-knowledge-modal/index.tsx @@ -0,0 +1,58 @@ +import { useFetchKnowledgeList } from '@/hooks/knowledgeHook'; +import { IModalProps } from '@/interfaces/common'; +import { Form, Modal, Select, SelectProps } from 'antd'; + +const ConnectToKnowledgeModal = ({ + visible, + hideModal, + onOk, +}: IModalProps) => { + const [form] = Form.useForm(); + const { list } = useFetchKnowledgeList(); + + const options: SelectProps['options'] = list?.map((item) => ({ + label: item.name, + value: item.id, + })); + + const handleOk = async () => { + const values = await form.getFieldsValue(); + const knowledgeIds = values.knowledgeIds ?? []; + if (knowledgeIds.length > 0) { + return onOk?.(knowledgeIds); + } + }; + + return ( + +
+ +