-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathChatMessageComponentAsEditBox.tsx
More file actions
258 lines (238 loc) · 9.2 KB
/
ChatMessageComponentAsEditBox.tsx
File metadata and controls
258 lines (238 loc) · 9.2 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { concatStyleSets, Icon, ITextField, mergeStyles, Stack } from '@fluentui/react';
import { ChatMyMessage } from '@fluentui-contrib/react-chat';
import { mergeClasses } from '@fluentui/react-components';
import { _formatString } from '@internal/acs-ui-common';
import { useTheme } from '../../theming/FluentThemeProvider';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { editBoxStyle, inputBoxIcon, editingButtonStyle, editBoxStyleSet } from '../styles/EditBox.styles';
import { InputBoxComponent } from '../InputBoxComponent';
import { InputBoxButton } from '../InputBoxButton';
import { MessageThreadStrings } from '../MessageThread';
import { useChatMyMessageStyles } from '../styles/MessageThread.styles';
import { ChatMessage } from '../../types';
import { _FileUploadCards } from '../FileUploadCards';
/* @conditional-compile-remove(file-sharing) */
import { AttachmentMetadata } from '../FileDownloadCards';
import {
chatMessageFailedTagStyle,
editChatMessageFailedTagStyle,
chatMessageFailedTagStackItemStyle,
editChatMessageButtonsStackStyle,
useChatMessageEditContainerStyles
} from '../styles/ChatMessageComponent.styles';
/* @conditional-compile-remove(mention) */
import { MentionLookupOptions } from '../MentionPopover';
const MAXIMUM_LENGTH_OF_MESSAGE = 8000;
const onRenderCancelIcon = (color: string): JSX.Element => {
const className = mergeStyles(inputBoxIcon, { color });
return <Icon iconName={'EditBoxCancel'} className={className} />;
};
const onRenderSubmitIcon = (color: string): JSX.Element => {
const className = mergeStyles(inputBoxIcon, { color });
return <Icon iconName={'EditBoxSubmit'} className={className} />;
};
/** @private */
export type ChatMessageComponentAsEditBoxProps = {
onCancel?: (messageId: string) => void;
onSubmit: (
text: string,
metadata?: Record<string, string>,
options?: {
/* @conditional-compile-remove(file-sharing) */
attachmentMetadata?: AttachmentMetadata[];
}
) => void;
message: ChatMessage;
strings: MessageThreadStrings;
/* @conditional-compile-remove(mention) */
mentionLookupOptions?: MentionLookupOptions;
};
type MessageState = 'OK' | 'too short' | 'too long';
/**
* @private
*/
export const ChatMessageComponentAsEditBox = (props: ChatMessageComponentAsEditBoxProps): JSX.Element => {
const { onCancel, onSubmit, strings, message } = props;
/* @conditional-compile-remove(mention) */
const { mentionLookupOptions } = props;
const [textValue, setTextValue] = useState<string>(message.content || '');
/* @conditional-compile-remove(file-sharing) */
const [attachmentMetadata, setAttachedFilesMetadata] = React.useState(getMessageAttachedFilesMetadata(message));
const editTextFieldRef = React.useRef<ITextField>(null);
const theme = useTheme();
const messageState = getMessageState(
textValue,
/* @conditional-compile-remove(file-sharing) */ attachmentMetadata ?? []
);
const submitEnabled = messageState === 'OK';
const editContainerStyles = useChatMessageEditContainerStyles();
const chatMyMessageStyles = useChatMyMessageStyles();
useEffect(() => {
editTextFieldRef.current?.focus();
}, []);
const setText = (event?: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue?: string): void => {
setTextValue(newValue ?? '');
};
const textTooLongMessage =
messageState === 'too long'
? _formatString(strings.editBoxTextLimit, { limitNumber: `${MAXIMUM_LENGTH_OF_MESSAGE}` })
: undefined;
const onRenderThemedCancelIcon = useCallback(
(isHover: boolean) => onRenderCancelIcon(isHover ? theme.palette.accent : theme.palette.neutralSecondary),
[theme.palette.neutralSecondary, theme.palette.accent]
);
const onRenderThemedSubmitIcon = useCallback(
(isHover: boolean) => onRenderSubmitIcon(isHover ? theme.palette.accent : theme.palette.neutralSecondary),
[theme.palette.neutralSecondary, theme.palette.accent]
);
const editBoxStyles = useMemo(() => {
return concatStyleSets(editBoxStyleSet, { textField: { borderColor: theme.palette.themePrimary } });
}, [theme.palette.themePrimary]);
/* @conditional-compile-remove(file-sharing) */
const onRenderFileUploads = useCallback(() => {
return (
!!attachmentMetadata &&
attachmentMetadata.length > 0 && (
<div style={{ margin: '0.25rem' }}>
<_FileUploadCards
activeFileUploads={attachmentMetadata?.map((file) => ({
id: file.name,
filename: file.name,
progress: 1
}))}
onCancelFileUpload={(fileId) => {
setAttachedFilesMetadata(attachmentMetadata?.filter((file) => file.name !== fileId));
}}
/>
</div>
)
);
}, [attachmentMetadata]);
const getContent = (): JSX.Element => {
return (
<>
<InputBoxComponent
data-ui-id="edit-box"
textFieldRef={editTextFieldRef}
inputClassName={editBoxStyle}
placeholderText={strings.editBoxPlaceholderText}
textValue={textValue}
onChange={setText}
onKeyDown={(ev) => {
if (ev.key === 'ArrowUp' || ev.key === 'ArrowDown') {
ev.stopPropagation();
}
}}
onEnterKeyDown={() => {
submitEnabled &&
onSubmit(
textValue,
message.metadata,
/* @conditional-compile-remove(file-sharing) */ {
attachmentMetadata
}
);
}}
supportNewline={false}
maxLength={MAXIMUM_LENGTH_OF_MESSAGE}
errorMessage={textTooLongMessage}
styles={editBoxStyles}
/* @conditional-compile-remove(mention) */
mentionLookupOptions={mentionLookupOptions}
></InputBoxComponent>
<Stack
horizontal
horizontalAlign="end"
className={editChatMessageButtonsStackStyle}
tokens={{ childrenGap: '0.25rem' }}
>
{message.failureReason && (
<Stack.Item grow align="stretch" className={chatMessageFailedTagStackItemStyle}>
<div className={mergeStyles(chatMessageFailedTagStyle(theme), editChatMessageFailedTagStyle)}>
{message.failureReason}
</div>
</Stack.Item>
)}
<Stack.Item align="end">
<InputBoxButton
className={editingButtonStyle}
ariaLabel={strings.editBoxCancelButton}
tooltipContent={strings.editBoxCancelButton}
onRenderIcon={onRenderThemedCancelIcon}
onClick={() => {
onCancel && onCancel(message.messageId);
}}
id={'dismissIconWrapper'}
/>
</Stack.Item>
<Stack.Item align="end">
<InputBoxButton
className={editingButtonStyle}
ariaLabel={strings.editBoxSubmitButton}
tooltipContent={strings.editBoxSubmitButton}
onRenderIcon={onRenderThemedSubmitIcon}
onClick={(e) => {
submitEnabled &&
onSubmit(
textValue,
message.metadata,
/* @conditional-compile-remove(file-sharing) */ {
attachmentMetadata
}
);
e.stopPropagation();
}}
id={'submitIconWrapper'}
/>
</Stack.Item>
</Stack>
{/* @conditional-compile-remove(file-sharing) */ onRenderFileUploads()}
</>
);
};
const attached = message.attached === true ? 'center' : message.attached === 'bottom' ? 'bottom' : 'top';
return (
<ChatMyMessage
attached={attached}
root={{
className: chatMyMessageStyles.root
}}
body={{
className: mergeClasses(
editContainerStyles.body,
message.failureReason !== undefined ? editContainerStyles.bodyError : editContainerStyles.bodyDefault,
attached !== 'top' ? editContainerStyles.bodyAttached : undefined
)
}}
>
{getContent()}
</ChatMyMessage>
);
};
const isMessageTooLong = (messageText: string): boolean => messageText.length > MAXIMUM_LENGTH_OF_MESSAGE;
function isMessageEmpty(
messageText: string,
/* @conditional-compile-remove(file-sharing) */
attachmentMetadata?: AttachmentMetadata[]
): boolean {
/* @conditional-compile-remove(file-sharing) */
return messageText.trim().length === 0 && attachmentMetadata?.length === 0;
return messageText.trim().length === 0;
}
function getMessageState(
messageText: string,
/* @conditional-compile-remove(file-sharing) */ attachmentMetadata: AttachmentMetadata[]
): MessageState {
return isMessageEmpty(messageText, /* @conditional-compile-remove(file-sharing) */ attachmentMetadata)
? 'too short'
: isMessageTooLong(messageText)
? 'too long'
: 'OK';
}
/* @conditional-compile-remove(file-sharing) */
// @TODO: Remove when file-sharing feature becomes stable.
const getMessageAttachedFilesMetadata = (message: ChatMessage): AttachmentMetadata[] | undefined => {
return message.files;
};