File size: 6,699 Bytes
2eeb8b1
 
d35be0d
2eeb8b1
fb3b3ab
 
 
 
 
 
 
2eeb8b1
fb3b3ab
 
 
 
 
 
 
 
 
 
 
 
2eeb8b1
 
fb3b3ab
 
 
2eeb8b1
 
fb3b3ab
 
 
2eeb8b1
 
 
 
 
 
d35be0d
2eeb8b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d35be0d
2eeb8b1
d35be0d
2eeb8b1
 
 
 
 
 
 
 
 
 
d35be0d
2eeb8b1
 
d35be0d
2eeb8b1
 
 
 
d35be0d
2eeb8b1
d35be0d
 
 
fb3b3ab
d35be0d
 
fb3b3ab
 
 
 
d35be0d
 
 
 
2eeb8b1
 
 
 
 
 
 
 
 
fb3b3ab
2eeb8b1
 
 
 
 
 
fb3b3ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2eeb8b1
 
 
 
fb3b3ab
2eeb8b1
 
 
 
 
 
 
 
 
d35be0d
2eeb8b1
 
fb3b3ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2eeb8b1
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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<GetProp<UploadProps, 'beforeUpload'>>[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<HTMLInputElement>;
  conversationId: string;
}

const getBase64 = (file: FileType): Promise<string> =>
  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<UploadFile[]>([]);

  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 = (
    <button style={{ border: 0, background: 'none' }} type="button">
      <PlusOutlined />
      <div style={{ marginTop: 8 }}>Upload</div>
    </button>
  );

  return (
    <Flex gap={20} vertical className={styles.messageInputWrapper}>
      <Input
        size="large"
        placeholder={t('sendPlaceholder')}
        value={value}
        disabled={disabled}
        suffix={
          <Space>
            <Upload
              action="/v1/document/upload_and_parse"
              // listType="picture-card"
              fileList={fileList}
              onPreview={handlePreview}
              onChange={handleChange}
              multiple
              headers={{ [Authorization]: getAuthorization() }}
              data={{ conversation_id: conversationId }}
              method="post"
              onRemove={handleRemove}
              showUploadList={false}
            >
              <Button icon={<UploadOutlined />}></Button>
            </Upload>
            <Button
              type="primary"
              onClick={handlePressEnter}
              loading={sendLoading}
              disabled={sendDisabled || isUploadingFile}
            >
              {t('send')}
            </Button>
          </Space>
        }
        onPressEnter={handlePressEnter}
        onChange={onInputChange}
      />
      {/* <Upload
        action="/v1/document/upload_and_parse"
        listType="picture-card"
        fileList={fileList}
        onPreview={handlePreview}
        onChange={handleChange}
        multiple
        headers={{ [Authorization]: getAuthorization() }}
        data={{ conversation_id: conversationId }}
        method="post"
        onRemove={handleRemove}
      >
        {fileList.length >= 8 ? null : uploadButton}
      </Upload> */}
      {fileList.length > 0 && (
        <List
          grid={{
            gutter: 16,
            xs: 1,
            sm: 2,
            md: 2,
            lg: 1,
            xl: 2,
            xxl: 4,
          }}
          dataSource={fileList}
          renderItem={(item) => {
            const fileExtension = getExtension(item.name);

            return (
              <List.Item>
                <Card className={styles.documentCard}>
                  <>
                    <Flex gap={10} align="center">
                      {item.status === 'uploading' || !item.response ? (
                        <Spin
                          indicator={
                            <LoadingOutlined style={{ fontSize: 24 }} spin />
                          }
                        />
                      ) : (
                        <FileIcon
                          id={getFileId(item)}
                          name={item.name}
                        ></FileIcon>
                      )}
                      <Flex vertical style={{ width: '90%' }}>
                        <Text
                          ellipsis={{ tooltip: item.name }}
                          className={styles.nameText}
                        >
                          <b> {item.name}</b>
                        </Text>
                        {item.percent !== 100 ? (
                          '上传中'
                        ) : !item.response ? (
                          '解析中'
                        ) : (
                          <Space>
                            <span>{fileExtension?.toUpperCase()},</span>
                          </Space>
                        )}
                      </Flex>
                    </Flex>
                  </>

                  {item.status !== 'uploading' && (
                    <CloseCircleOutlined
                      className={styles.deleteIcon}
                      onClick={() => handleRemove(item)}
                    />
                  )}
                </Card>
              </List.Item>
            );
          }}
        />
      )}
    </Flex>
  );
};

export default MessageInput;