diff --git a/web/src/components/message-input/index.tsx b/web/src/components/message-input/index.tsx index 1f692a8ed..56e8d6afd 100644 --- a/web/src/components/message-input/index.tsx +++ b/web/src/components/message-input/index.tsx @@ -1,11 +1,10 @@ -import { Authorization } from '@/constants/authorization'; import { useTranslate } from '@/hooks/common-hooks'; import { useDeleteDocument, useFetchDocumentInfosByIds, useRemoveNextDocument, + useUploadAndParseDocument, } from '@/hooks/document-hooks'; -import { getAuthorization } from '@/utils/authorization-util'; import { getExtension } from '@/utils/document-util'; import { formatBytes } from '@/utils/file-util'; import { @@ -28,7 +27,14 @@ import { } from 'antd'; import classNames from 'classnames'; import get from 'lodash/get'; -import { ChangeEventHandler, useCallback, useEffect, useState } from 'react'; +import { + ChangeEventHandler, + memo, + useCallback, + useEffect, + useRef, + useState, +} from 'react'; import FileIcon from '../file-icon'; import SvgIcon from '../svg-icon'; import styles from './index.less'; @@ -64,9 +70,10 @@ interface IProps { onPressEnter(documentIds: string[]): void; onInputChange: ChangeEventHandler; conversationId: string; - uploadUrl?: string; + uploadMethod?: string; isShared?: boolean; showUploadIcon?: boolean; + createConversationBeforeUploadDocument?(message: string): Promise; } const getBase64 = (file: FileType): Promise => @@ -87,12 +94,15 @@ const MessageInput = ({ onInputChange, conversationId, showUploadIcon = true, - uploadUrl = '/v1/document/upload_and_parse', + createConversationBeforeUploadDocument, + uploadMethod = 'upload_and_parse', }: IProps) => { const { t } = useTranslate('chat'); const { removeDocument } = useRemoveNextDocument(); const { deleteDocument } = useDeleteDocument(); const { data: documentInfos, setDocumentIds } = useFetchDocumentInfosByIds(); + const { uploadAndParseDocument } = useUploadAndParseDocument(uploadMethod); + const conversationIdRef = useRef(conversationId); const [fileList, setFileList] = useState([]); @@ -102,9 +112,44 @@ const MessageInput = ({ } }; - const handleChange: UploadProps['onChange'] = ({ fileList: newFileList }) => { - setFileList(newFileList); + const handleChange: UploadProps['onChange'] = async ({ + // fileList: newFileList, + file, + }) => { + let nextConversationId: string = conversationId; + if (createConversationBeforeUploadDocument && !conversationId) { + const creatingRet = await createConversationBeforeUploadDocument( + file.name, + ); + if (creatingRet.retcode === 0) { + nextConversationId = creatingRet.data.id; + } + } + setFileList((list) => { + list.push({ + ...file, + status: 'uploading', + originFileObj: file as any, + }); + return [...list]; + }); + const ret = await uploadAndParseDocument({ + conversationId: nextConversationId, + fileList: [file], + }); + setFileList((list) => { + const nextList = list.filter((x) => x.uid !== file.uid); + nextList.push({ + ...file, + originFileObj: file as any, + response: ret, + percent: 100, + status: ret?.retcode === 0 ? 'done' : 'error', + }); + return nextList; + }); }; + const isUploadingFile = fileList.some((x) => x.status === 'uploading'); const handlePressEnter = useCallback(async () => { @@ -150,6 +195,16 @@ const MessageInput = ({ setDocumentIds(ids); }, [fileList, setDocumentIds]); + useEffect(() => { + if ( + conversationIdRef.current && + conversationId !== conversationIdRef.current + ) { + setFileList([]); + } + conversationIdRef.current = conversationId; + }, [conversationId, setFileList]); + return ( - {conversationId && showUploadIcon && ( + {showUploadIcon && ( { + console.log('🚀 ~ beforeUpload:', fileList); + return false; + }} >