Skip to content

Commit

Permalink
feat: upload file in FileManager #345 (#529)
Browse files Browse the repository at this point in the history
### What problem does this PR solve?

feat: upload file in FileManager #345 

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
  • Loading branch information
cike8899 authored Apr 25, 2024
1 parent b06d639 commit 51e7697
Show file tree
Hide file tree
Showing 17 changed files with 412 additions and 50 deletions.
50 changes: 49 additions & 1 deletion web/src/hooks/fileManagerHooks.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<any>({
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<any>({
type: 'fileManager/connectFileToKnowledge',
payload,
});
} catch (errorInfo) {
console.log('Failed:', errorInfo);
}
},
[dispatch],
);

return uploadFile;
};
2 changes: 1 addition & 1 deletion web/src/interfaces/database/file-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions web/src/interfaces/request/file-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}
2 changes: 1 addition & 1 deletion web/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion web/src/locales/zh-traditional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default {
password: '密碼',
passwordDescription: '請輸入您當前的密碼以更改您的密碼。',
model: '模型提供商',
modelDescription: '在此管理您的帳戶設置和首選項。',
modelDescription: '在此設置模型參數和 API Key。',
team: '團隊',
logout: '登出',
username: '使用者名稱',
Expand Down
2 changes: 1 addition & 1 deletion web/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export default {
password: '密码',
passwordDescription: '请输入您当前的密码以更改您的密码。',
model: '模型提供商',
modelDescription: '在此管理您的帐户设置和首选项。',
modelDescription: '在此设置模型参数和 API Key。',
team: '团队',
logout: '登出',
username: '用户名',
Expand Down
18 changes: 16 additions & 2 deletions web/src/pages/file-manager/action-cell/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -41,9 +47,17 @@ const ActionCell = ({ record, setCurrentRecord, showRenameModal }: IProps) => {
showRenameModal(record);
};

const onShowConnectToKnowledgeModal = () => {
showConnectToKnowledgeModal([documentId]);
};

return (
<Space size={0}>
<Button type="text" className={styles.iconButton}>
<Button
type="text"
className={styles.iconButton}
onClick={onShowConnectToKnowledgeModal}
>
<ToolOutlined size={20} />
</Button>

Expand Down
58 changes: 58 additions & 0 deletions web/src/pages/file-manager/connect-to-knowledge-modal/index.tsx
Original file line number Diff line number Diff line change
@@ -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<string[]>) => {
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 (
<Modal
title="Add to Knowledge Base"
open={visible}
onOk={handleOk}
onCancel={hideModal}
>
<Form form={form}>
<Form.Item
name="knowledgeIds"
noStyle
rules={[
{
required: true,
message: 'Please select your favourite colors!',
type: 'array',
},
]}
>
<Select
mode="multiple"
allowClear
style={{ width: '100%' }}
placeholder="Please select"
options={options}
/>
</Form.Item>
</Form>
</Modal>
);
};

export default ConnectToKnowledgeModal;
10 changes: 8 additions & 2 deletions web/src/pages/file-manager/file-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import styles from './index.less';
interface IProps {
selectedRowKeys: string[];
showFolderCreateModal: () => void;
showFileUploadModal: () => void;
}

const itemRender: BreadcrumbProps['itemRender'] = (
Expand All @@ -48,7 +49,11 @@ const itemRender: BreadcrumbProps['itemRender'] = (
);
};

const FileToolbar = ({ selectedRowKeys, showFolderCreateModal }: IProps) => {
const FileToolbar = ({
selectedRowKeys,
showFolderCreateModal,
showFileUploadModal,
}: IProps) => {
const { t } = useTranslate('knowledgeDetails');
const { fetchDocumentList } = useFetchDocumentListOnMount();
const { setPagination, searchString } = useGetPagination(fetchDocumentList);
Expand All @@ -59,6 +64,7 @@ const FileToolbar = ({ selectedRowKeys, showFolderCreateModal }: IProps) => {
return [
{
key: '1',
onClick: showFileUploadModal,
label: (
<div>
<Button type="link">
Expand All @@ -85,7 +91,7 @@ const FileToolbar = ({ selectedRowKeys, showFolderCreateModal }: IProps) => {
// disabled: true,
},
];
}, [t, showFolderCreateModal]);
}, [t, showFolderCreateModal, showFileUploadModal]);

const { handleRemoveFile } = useHandleDeleteFile(selectedRowKeys);

Expand Down
Loading

0 comments on commit 51e7697

Please sign in to comment.