import { Authorization } from '@/constants/authorization'; import { useTranslate } from '@/hooks/common-hooks'; import { useRemoveNextDocument } from '@/hooks/document-hooks'; import { getAuthorization } from '@/utils/authorization-util'; import { getExtension } from '@/utils/document-util'; import { CloseCircleOutlined, LoadingOutlined, PlusOutlined, UploadOutlined, } from '@ant-design/icons'; import type { GetProp, UploadFile } from 'antd'; import { Button, Card, Flex, Input, List, Space, Spin, Typography, Upload, UploadProps, } from 'antd'; import get from 'lodash/get'; import { ChangeEventHandler, useCallback, useState } from 'react'; import FileIcon from '../file-icon'; import styles from './index.less'; type FileType = Parameters>[0]; const { Text } = Typography; const getFileId = (file: UploadFile) => get(file, 'response.data.0'); interface IProps { disabled: boolean; value: string; sendDisabled: boolean; sendLoading: boolean; onPressEnter(documentIds: string[]): void; onInputChange: ChangeEventHandler; conversationId: string; } const getBase64 = (file: FileType): Promise => new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file as any); reader.onload = () => resolve(reader.result as string); reader.onerror = (error) => reject(error); }); const MessageInput = ({ disabled, value, onPressEnter, sendDisabled, sendLoading, onInputChange, conversationId, }: IProps) => { const { t } = useTranslate('chat'); const { removeDocument } = useRemoveNextDocument(); const [fileList, setFileList] = useState([]); const handlePreview = async (file: UploadFile) => { if (!file.url && !file.preview) { file.preview = await getBase64(file.originFileObj as FileType); } }; const handleChange: UploadProps['onChange'] = ({ fileList: newFileList }) => { setFileList(newFileList); }; const isUploadingFile = fileList.some((x) => x.status === 'uploading'); const handlePressEnter = useCallback(async () => { if (isUploadingFile) return; const ids = fileList.reduce((pre, cur) => { return pre.concat(get(cur, 'response.data', [])); }, []); onPressEnter(ids); setFileList([]); }, [fileList, onPressEnter, isUploadingFile]); const handleRemove = useCallback( async (file: UploadFile) => { const ids = get(file, 'response.data', []); if (ids.length) { await removeDocument(ids[0]); setFileList((preList) => { return preList.filter((x) => getFileId(x) !== ids[0]); }); } }, [removeDocument], ); const uploadButton = ( ); return ( } onPressEnter={handlePressEnter} onChange={onInputChange} /> {/* {fileList.length >= 8 ? null : uploadButton} */} {fileList.length > 0 && ( { const fileExtension = getExtension(item.name); return ( <> {item.status === 'uploading' || !item.response ? ( } /> ) : ( )} {item.name} {item.percent !== 100 ? ( '上传中' ) : !item.response ? ( '解析中' ) : ( {fileExtension?.toUpperCase()}, )} {item.status !== 'uploading' && ( handleRemove(item)} /> )} ); }} /> )} ); }; export default MessageInput;