import { addDocument, addYuque, apiInterceptors, uploadDocument } from '@/client/api'; import { StepChangeParams } from '@/types/knowledge'; import { InboxOutlined, MinusCircleOutlined, PlusOutlined } from '@ant-design/icons'; import { Button, Form, Input, Spin, Typography, Upload, message } from 'antd'; import { RcFile, UploadChangeParam } from 'antd/es/upload'; import { default as classNames, default as cls } from 'classnames'; import { useState } from 'react'; import { useTranslation } from 'react-i18next'; type FileParams = { file: RcFile; fileList: FileList; }; type IProps = { className: string; handleStepChange: (params: StepChangeParams) => void; spaceName: string; docType: string; }; type FieldType = { docName: string; textSource: string; originFileObj: FileParams; text: string; webPageUrl: string; questions: Record[]; doc_token?: string; }; const { Dragger } = Upload; const { TextArea } = Input; export default function DocUploadForm(props: IProps) { const { className, handleStepChange, spaceName, docType } = props; const { t } = useTranslation(); const [form] = Form.useForm(); const [spinning, setSpinning] = useState(false); const [files, setFiles] = useState([]); const upload = async (data: FieldType) => { const { docName, textSource, text, webPageUrl, doc_token, questions = [] } = data; let docId: any; setSpinning(true); switch (docType) { case 'URL': [, docId] = await apiInterceptors( addDocument(spaceName as string, { doc_name: docName, content: webPageUrl, doc_type: 'URL', questions: questions?.map(item => item.question), }), ); break; case 'TEXT': [, docId] = await apiInterceptors( addDocument(spaceName as string, { doc_name: docName, source: textSource, content: text, doc_type: 'TEXT', questions: questions.map(item => item.question), }), ); break; case 'YUQUEURL': [, docId] = await apiInterceptors( addYuque({ doc_name: docName, space_name: spaceName, content: webPageUrl, doc_type: 'YUQUEURL', doc_token: doc_token || '', questions: questions?.map(item => item.question), }), ); break; } setSpinning(false); if (docType === 'DOCUMENT' && files.length < 1) { return message.error('Upload failed, please re-upload.'); } else if (docType !== 'DOCUMENT' && !docId) { return message.error('Upload failed, please re-upload.'); } handleStepChange({ label: 'forward', files: docType === 'DOCUMENT' ? files : [ { name: docName, doc_id: docId || -1, }, ], }); }; const handleFileChange = ({ fileList }: UploadChangeParam) => { if (fileList.length === 0) { form.setFieldValue('originFileObj', null); } }; const renderText = () => { return ( <> label={`${t('Name')}:`} name='docName' rules={[{ required: true, message: t('Please_input_the_name') }]} > label={`${t('Text_Source')}:`} name='textSource' rules={[{ required: true, message: t('Please_input_the_text_source') }]} > label={`${t('Text')}:`} name='text' rules={[{ required: true, message: t('Please_input_the_description') }]} >