import { ReactComponent as SelectFilesEndIcon } from '@/assets/svg/select-files-end.svg'; import { ReactComponent as SelectFilesStartIcon } from '@/assets/svg/select-files-start.svg'; import { KnowledgeRouteKey } from '@/constants/knowledge'; import { useDeleteDocumentById, useFetchKnowledgeDetail, useGetDocumentDefaultParser, useKnowledgeBaseId, } from '@/hooks/knowledgeHook'; import { useFetchTenantInfo, useSelectParserList, } from '@/hooks/userSettingHook'; import uploadService from '@/services/uploadService'; import { isFileUploadDone } from '@/utils/documentUtils'; import { ArrowLeftOutlined, CloudUploadOutlined, DeleteOutlined, EditOutlined, FileDoneOutlined, } from '@ant-design/icons'; import { Button, Card, Flex, Popover, Progress, Radio, RadioChangeEvent, Space, Upload, UploadFile, UploadProps, } from 'antd'; import classNames from 'classnames'; import { ReactElement, useCallback, useEffect, useMemo, useRef, useState, } from 'react'; import { Link, useDispatch, useNavigate } from 'umi'; import { useSetDocumentParser } from '@/hooks/documentHooks'; import styles from './index.less'; const { Dragger } = Upload; type UploadRequestOption = Parameters< NonNullable >[0]; const UploaderItem = ({ file, isUpload, remove, }: { isUpload: boolean; originNode: ReactElement; file: UploadFile; fileList: object[]; remove: (id: string) => void; }) => { const { parserConfig, defaultParserId } = useGetDocumentDefaultParser(); const { removeDocument } = useDeleteDocumentById(); const [value, setValue] = useState(defaultParserId); const setDocumentParser = useSetDocumentParser(); const documentId = file?.response?.id; const parserList = useSelectParserList(); const saveParser = (parserId: string) => { setDocumentParser(parserId, documentId, parserConfig as any); }; const onChange = (e: RadioChangeEvent) => { const val = e.target.value; setValue(val); saveParser(val); }; const content = ( {parserList.map( ( x, // value is lowercase, key is uppercase ) => ( {x.label} ), )} ); const handleRemove = async () => { if (file.status === 'error') { remove(documentId); } else { const ret: any = await removeDocument(documentId); if (ret === 0) { remove(documentId); } } }; useEffect(() => { setValue(defaultParserId); }, [defaultParserId]); return (
{file.name}
{file.size}
{isUpload ? ( ) : ( )}
100%
); }; const KnowledgeUploadFile = () => { const knowledgeBaseId = useKnowledgeBaseId(); const [isUpload, setIsUpload] = useState(true); const dispatch = useDispatch(); const [uploadedFileIds, setUploadedFileIds] = useState([]); const fileListRef = useRef([]); const navigate = useNavigate(); const enabled = useMemo(() => { if (isUpload) { return ( uploadedFileIds.length > 0 && fileListRef.current.filter((x) => isFileUploadDone(x)).length === uploadedFileIds.length ); } return true; }, [uploadedFileIds, isUpload]); const createRequest: (props: UploadRequestOption) => void = async function ({ file, onSuccess, onError, // onProgress, }) { const ret = await uploadService.uploadFile(file, knowledgeBaseId); const data = ret?.data; if (data?.retcode === 0) { setUploadedFileIds((pre) => { return pre.concat(data.data.id); }); if (onSuccess) { onSuccess(data.data); } } else { if (onError) { onError(data?.data); } } }; const removeIdFromUploadedIds = useCallback((id: string) => { setUploadedFileIds((pre) => { return pre.filter((x) => x !== id); }); }, []); const props: UploadProps = { name: 'file', multiple: true, itemRender(originNode, file, fileList, actions) { fileListRef.current = fileList; const remove = (id: string) => { if (isFileUploadDone(file)) { removeIdFromUploadedIds(id); } actions.remove(); }; return ( ); }, customRequest: createRequest, onDrop(e) { console.log('Dropped files', e.dataTransfer.files); }, }; const runSelectedDocument = () => { const ids = fileListRef.current.map((x) => x.response.id); dispatch({ type: 'kFModel/document_run', payload: { doc_ids: ids, run: 1 }, }); }; const handleNextClick = () => { if (!isUpload) { runSelectedDocument(); navigate(`/knowledge/${KnowledgeRouteKey.Dataset}?id=${knowledgeBaseId}`); } else { setIsUpload(false); } }; useFetchTenantInfo(); useFetchKnowledgeDetail(); return (
Back to select files

Select files

Change specific category

Click or drag file to this area to upload

Support for a single or bulk upload. Strictly prohibited from uploading company data or other banned files.

); }; export default KnowledgeUploadFile;