-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathMessageBar.tsx
More file actions
550 lines (516 loc) · 19.8 KB
/
MessageBar.tsx
File metadata and controls
550 lines (516 loc) · 19.8 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import type { ChangeEvent, FunctionComponent, KeyboardEvent as ReactKeyboardEvent, Ref } from 'react';
import { forwardRef, useCallback, useEffect, useRef, useState } from 'react';
import { Accept, DropEvent, DropzoneOptions, FileError, FileRejection } from 'react-dropzone';
import {
ButtonProps,
MenuSearchInputProps,
MenuSearchProps,
SearchInputProps,
TextArea,
TextAreaProps,
TooltipProps
} from '@patternfly/react-core';
import { css } from '@patternfly/react-styles';
// Import Chatbot components
import SendButton from './SendButton';
import MicrophoneButton from './MicrophoneButton';
import { AttachButton, AttachButtonProps } from './AttachButton';
import AttachMenu from '../AttachMenu';
import StopButton from './StopButton';
import { ChatbotDisplayMode } from '../Chatbot';
export interface MessageBarWithAttachMenuProps {
/** Flag to enable whether attach menu is open */
isAttachMenuOpen: boolean;
/** Callback to close attach menu */
setIsAttachMenuOpen: (isOpen: boolean) => void;
/** Items in menu */
attachMenuItems: React.ReactNode;
/** A callback for when the attachment menu toggle is clicked */
onAttachMenuToggleClick: () => void;
/** A callback for when the input value in the menu changes. */
onAttachMenuInputChange?: (value: string) => void;
/** Function callback called when user selects item in menu. */
onAttachMenuSelect?: (event?: React.MouseEvent<Element, MouseEvent>, value?: string | number) => void;
/** Placeholder for search input */
attachMenuInputPlaceholder?: string;
/** Keys that trigger onOpenChange, defaults to tab and escape. It is highly recommended to include Escape in the array, while Tab may be omitted if the menu contains non-menu items that are focusable. */
onAttachMenuOnOpenChangeKeys?: string[];
/** Callback to change the open state of the menu. Triggered by clicking outside of the menu. */
onAttachMenuOpenChange?: (isOpen: boolean) => void;
/** Additional props passed to MenuSearch component in attach menu */
menuSearchProps?: Omit<MenuSearchProps, 'ref'>;
/** Additional props passed to MenuSearchInput component in attach menu */
menuSearchInputProps?: Omit<MenuSearchInputProps, 'ref'>;
/** Additional props passed to SearchInput component in attach menu */
searchInputProps?: SearchInputProps;
}
export interface MessageBarProps extends Omit<TextAreaProps, 'innerRef'> {
/** Callback to get the value of input message by user */
onSendMessage: (message: string | number) => void;
/** Class Name for the MessageBar component */
className?: string;
/** Flag to always to show the send button. By default send button is shown when there is a message in the input field */
alwayShowSendButton?: boolean;
/** Placeholder for message input */
placeholder?: string;
/** Flag to disable/enable the Attach button */
hasAttachButton?: boolean;
/** Whether the attach button is rendered before or after the message input. */
attachButtonPosition?: 'start' | 'end';
/** Flag to enable the Microphone button */
hasMicrophoneButton?: boolean;
/** Placeholder text when listening */
listeningText?: string;
/** Flag to enable the Stop button, used for streaming content */
hasStopButton?: boolean;
/** Callback function for when stop button is clicked */
handleStopButton?: (event: React.MouseEvent<HTMLButtonElement>) => void;
/** Callback function for when attach button is used to upload a file */
handleAttach?: (data: File[], event: DropEvent) => void;
/** Specifies the file types accepted by the attachment upload component.
* Files that don't match the accepted types will be disabled in the file picker.
* For example,
* allowedFileTypes: { 'application/json': ['.json'], 'text/plain': ['.txt'] }
**/
allowedFileTypes?: Accept;
/** Minimum file size allowed */
minSize?: number;
/** Max file size allowed */
maxSize?: number;
/** Max number of files allowed */
maxFiles?: number;
/** Whether attachments are disabled */
isAttachmentDisabled?: boolean;
/** Callback when file(s) are attached */
onAttach?: <T extends File>(acceptedFiles: T[], fileRejections: FileRejection[], event: DropEvent) => void;
/** Callback function for AttachButton when an attachment fails */
onAttachRejected?: (fileRejections: FileRejection[], event: DropEvent) => void;
/** Validator for files; see https://react-dropzone.js.org/#!/Custom%20validation for more information */
validator?: <T extends File>(file: T) => FileError | readonly FileError[] | null;
/** Additional props passed to react-dropzone */
dropzoneProps?: DropzoneOptions;
/** Props to enable a menu that opens when the Attach button is clicked, instead of the attachment window */
attachMenuProps?: MessageBarWithAttachMenuProps;
/** Flag to provide manual control over whether send button is disabled */
isSendButtonDisabled?: boolean;
/** Prop to allow passage of additional props to buttons */
buttonProps?: {
attach?: AttachButtonProps & { props?: ButtonProps };
stop?: { tooltipContent?: string; props?: ButtonProps; tooltipProps?: Omit<TooltipProps, 'content'> };
send?: { tooltipContent?: string; props?: ButtonProps; tooltipProps?: Omit<TooltipProps, 'content'> };
microphone?: {
tooltipContent?: { active?: string; inactive?: string };
language?: string;
props?: ButtonProps;
tooltipProps?: Omit<TooltipProps, 'content'>;
};
};
/** A callback for when the text area value changes. */
onChange?: (event: React.ChangeEvent<HTMLTextAreaElement>, value: string | number) => void;
/** Display mode of chatbot, if you want to message bar to resize when the display mode changes */
displayMode?: ChatbotDisplayMode;
/** Whether message bar is compact */
isCompact?: boolean;
/** Ref applied to message bar textarea, for use with focus or other custom behaviors */
innerRef?: React.Ref<HTMLTextAreaElement>;
/** Sets background color to primary */
isPrimary?: boolean;
/** Additional actions to render for the message bar. This will force a multiline layout, and the actions will render at the start of the container. */
additionalActions?: React.ReactNode;
/** Flag indicating whether a multiline layout for the message input and actions should be forced. This can be used to always render actions below the message input. */
forceMultilineLayout?: boolean;
}
export const MessageBarBase: FunctionComponent<MessageBarProps> = ({
onSendMessage,
className,
alwayShowSendButton,
placeholder = 'Send a message...',
hasAttachButton = true,
attachButtonPosition = 'end',
hasMicrophoneButton,
listeningText = 'Listening',
handleAttach,
attachMenuProps,
isSendButtonDisabled,
handleStopButton,
hasStopButton,
buttonProps,
onChange,
displayMode,
value,
isCompact = false,
allowedFileTypes,
minSize,
maxSize,
maxFiles,
isAttachmentDisabled,
onAttach,
onAttachRejected,
validator,
dropzoneProps,
innerRef,
isPrimary,
additionalActions,
forceMultilineLayout = false,
...props
}: MessageBarProps) => {
// Text Input
// --------------------------------------------------------------------------
const [message, setMessage] = useState<string | number>(value ?? '');
const [isListeningMessage, setIsListeningMessage] = useState<boolean>(false);
const [hasSentMessage, setHasSentMessage] = useState(false);
const [isComposing, setIsComposing] = useState(false);
const shouldForceMultiline = forceMultilineLayout || additionalActions;
const [isMultiline, setIsMultiline] = useState(shouldForceMultiline);
const inputRef = useRef<HTMLTextAreaElement>(null);
const textareaRef = (innerRef as React.RefObject<HTMLTextAreaElement>) ?? inputRef;
const attachButtonRef = useRef<HTMLButtonElement>(null);
const topMargin = '1rem';
useEffect(() => {
if (value !== undefined && value !== message) {
setMessage(value);
}
}, [value, message]);
const setInitialLineHeight = (field: HTMLTextAreaElement) => {
field.style.setProperty('line-height', '1rem');
const parent = field.parentElement;
if (parent) {
parent.style.setProperty('margin-top', topMargin);
parent.style.setProperty('margin-bottom', `0rem`);
parent.style.setProperty('height', 'inherit');
const grandparent = parent.parentElement;
if (grandparent) {
grandparent.style.setProperty('flex-basis', shouldForceMultiline ? '100%' : 'auto');
}
}
};
const setAutoHeight = (field: HTMLTextAreaElement) => {
const parent = field.parentElement;
if (parent) {
parent.style.setProperty('height', 'inherit');
const computed = window.getComputedStyle(field);
// Calculate the height
const height =
parseInt(computed.getPropertyValue('border-top-width')) +
parseInt(computed.getPropertyValue('padding-top')) +
field.scrollHeight +
parseInt(computed.getPropertyValue('padding-bottom')) +
parseInt(computed.getPropertyValue('border-bottom-width'));
parent.style.setProperty('height', `${height}px`);
if (height > 32 || window.innerWidth <= 507) {
parent.style.setProperty('margin-bottom', topMargin);
parent.style.setProperty('margin-top', topMargin);
}
}
};
const textIsLongerThan2Lines = (field: HTMLTextAreaElement) => {
const lineHeight = parseFloat(window.getComputedStyle(field).lineHeight);
const lines = field.scrollHeight / lineHeight;
return lines > 2;
};
const checkIfMultiline = useCallback(
(field: HTMLTextAreaElement) => {
const parent = field.parentElement;
const grandparent = parent?.parentElement;
if (grandparent) {
const containerHeight = grandparent.offsetHeight;
const threshold = isCompact ? 56 : 70;
setIsMultiline(containerHeight > threshold);
}
},
[isCompact]
);
const setAutoWidth = useCallback((field: HTMLTextAreaElement) => {
const parent = field.parentElement;
if (parent) {
const grandparent = parent.parentElement;
if ((textIsLongerThan2Lines(field) || shouldForceMultiline) && grandparent) {
grandparent.style.setProperty('flex-basis', `100%`);
}
}
}, []);
const handleNewLine = (field: HTMLTextAreaElement) => {
const parent = field.parentElement;
if (parent) {
parent.style.setProperty('margin-bottom', topMargin);
parent.style.setProperty('margin-top', topMargin);
}
};
useEffect(() => {
const field = textareaRef.current;
if (field) {
if (field.value === '') {
if (window.innerWidth > 507) {
setInitialLineHeight(field);
}
} else {
setAutoHeight(field);
setAutoWidth(field);
}
}
const resetHeight = () => {
if (field) {
if (field.value === '') {
if (window.innerWidth > 507) {
setInitialLineHeight(field);
}
} else {
setAutoHeight(field);
setAutoWidth(field);
}
}
};
window.addEventListener('resize', resetHeight);
return () => {
window.removeEventListener('resize', resetHeight);
};
}, [setAutoWidth]);
useEffect(() => {
const field = textareaRef.current;
if (field) {
if (field.value === '') {
setInitialLineHeight(field);
!shouldForceMultiline && setIsMultiline(false);
} else {
setAutoHeight(field);
setAutoWidth(field);
!shouldForceMultiline && checkIfMultiline(field);
}
}
}, [displayMode, message, setAutoWidth, shouldForceMultiline, checkIfMultiline]);
useEffect(() => {
const field = textareaRef.current;
if (field) {
setInitialLineHeight(field);
setHasSentMessage(false);
}
}, [hasSentMessage]);
const handleChange = useCallback(
(event) => {
onChange && onChange(event, event.target.value);
if (textareaRef.current) {
if (event.target.value === '') {
setInitialLineHeight(textareaRef.current);
!shouldForceMultiline && setIsMultiline(false);
} else {
setAutoHeight(textareaRef.current);
!shouldForceMultiline && checkIfMultiline(textareaRef.current);
}
}
setMessage(event.target.value);
},
[onChange, checkIfMultiline]
);
// Handle sending message
const handleSend = useCallback(
(newMessage: string | number) => {
onSendMessage(newMessage);
setHasSentMessage(true);
setMessage('');
},
[onSendMessage]
);
const handleKeyDown = useCallback(
(event: ReactKeyboardEvent) => {
// Japanese and other languages may use IME for character input.
// In these cases, enter is used to select the final input, so we need to check for composition end instead.
// See more info at https://www.stum.de/2016/06/24/handling-ime-events-in-javascript/
// Chrome, Edge, and Firefox seem to work well with just the compose event.
// Safari is a little bit special. We need to handle 229 as well in this case.
const nativeEvent = event.nativeEvent as KeyboardEvent;
const isCompositionKey = nativeEvent.which === 229;
const isCurrentlyComposing = isComposing || isCompositionKey;
if (event.key === 'Enter' && !isCurrentlyComposing && !event.shiftKey) {
event.preventDefault();
if (!isSendButtonDisabled && !hasStopButton) {
handleSend(message);
}
}
if (event.key === 'Enter' && !isCurrentlyComposing && event.shiftKey) {
if (textareaRef.current) {
handleNewLine(textareaRef.current);
}
}
},
[isSendButtonDisabled, hasStopButton, handleSend, message, isComposing]
);
const handleCompositionStart = useCallback(() => {
setIsComposing(true);
}, []);
const handleCompositionEnd = useCallback(() => {
setIsComposing(false);
}, []);
const handleAttachMenuToggle = () => {
attachMenuProps?.setIsAttachMenuOpen && attachMenuProps?.setIsAttachMenuOpen(!attachMenuProps?.isAttachMenuOpen);
attachMenuProps?.onAttachMenuToggleClick();
};
const handleSpeechRecognition = (message) => {
setMessage(message);
onChange && onChange({} as ChangeEvent<HTMLTextAreaElement>, message);
};
const renderAttachButton = () => {
if (!attachMenuProps && hasAttachButton) {
return (
<AttachButton
onAttachAccepted={handleAttach}
isDisabled={isListeningMessage}
tooltipContent={buttonProps?.attach?.tooltipContent}
inputTestId={buttonProps?.attach?.inputTestId}
isCompact={isCompact}
tooltipProps={buttonProps?.attach?.tooltipProps}
allowedFileTypes={allowedFileTypes}
minSize={minSize}
maxSize={maxSize}
maxFiles={maxFiles}
isAttachmentDisabled={isAttachmentDisabled}
onAttach={onAttach}
onAttachRejected={onAttachRejected}
validator={validator}
dropzoneProps={dropzoneProps}
{...buttonProps?.attach}
{...buttonProps?.attach?.props}
/>
);
}
if (attachMenuProps) {
return (
<AttachButton
ref={attachButtonRef}
onClick={handleAttachMenuToggle}
isDisabled={isListeningMessage}
tooltipContent={buttonProps?.attach?.tooltipContent}
isCompact={isCompact}
tooltipProps={buttonProps?.attach?.tooltipProps}
allowedFileTypes={allowedFileTypes}
minSize={minSize}
maxSize={maxSize}
maxFiles={maxFiles}
isAttachmentDisabled={isAttachmentDisabled}
onAttach={onAttach}
onAttachRejected={onAttachRejected}
validator={validator}
dropzoneProps={dropzoneProps}
{...buttonProps?.attach}
/>
);
}
};
const isAttachButtonAtStart = attachButtonPosition === 'start';
const renderButtons = () => {
if (hasStopButton && handleStopButton) {
return (
<StopButton
onClick={handleStopButton}
tooltipContent={buttonProps?.stop?.tooltipContent}
isCompact={isCompact}
tooltipProps={buttonProps?.stop?.tooltipProps}
{...buttonProps?.stop?.props}
/>
);
}
return (
<>
{!isAttachButtonAtStart && renderAttachButton()}
{hasMicrophoneButton && (
<MicrophoneButton
isListening={isListeningMessage}
onIsListeningChange={setIsListeningMessage}
onSpeechRecognition={handleSpeechRecognition}
tooltipContent={buttonProps?.microphone?.tooltipContent}
language={buttonProps?.microphone?.language}
isCompact={isCompact}
tooltipProps={buttonProps?.microphone?.tooltipProps}
{...buttonProps?.microphone?.props}
/>
)}
{(alwayShowSendButton || message) && (
<SendButton
value={message}
onClick={() => handleSend(message)}
isDisabled={isSendButtonDisabled}
tooltipContent={buttonProps?.send?.tooltipContent}
isCompact={isCompact}
tooltipProps={buttonProps?.send?.tooltipProps}
{...buttonProps?.send?.props}
/>
)}
</>
);
};
const hasGroupedActions = additionalActions || (isAttachButtonAtStart && isMultiline);
const messageBarContents = (
<>
{isAttachButtonAtStart && !isMultiline && (
<div className="pf-chatbot__message-bar-actions test">{renderAttachButton()}</div>
)}
<div className={`pf-chatbot__message-bar-input ${isCompact ? 'pf-m-compact' : ''}`}>
<TextArea
className="pf-chatbot__message-textarea"
value={message}
onChange={handleChange}
aria-label={isListeningMessage ? listeningText : placeholder}
placeholder={isListeningMessage ? listeningText : placeholder}
ref={textareaRef}
onKeyDown={handleKeyDown}
onCompositionStart={handleCompositionStart}
onCompositionEnd={handleCompositionEnd}
{...props}
/>
</div>
<div className={css('pf-chatbot__message-bar-actions', hasGroupedActions && 'pf-m-grouped')}>
{hasGroupedActions ? (
<>
<div className={css('pf-chatbot__message-bar-actions-group')}>
{isAttachButtonAtStart && renderAttachButton()}
{additionalActions}
</div>
<div className={css('pf-chatbot__message-bar-actions-group')}>{renderButtons()}</div>
</>
) : (
renderButtons()
)}
</div>
</>
);
if (attachMenuProps) {
return (
<AttachMenu
toggle={(toggleRef) => (
<div ref={toggleRef} className={css('pf-chatbot__message-bar', isMultiline && 'pf-m-multiline', className)}>
{messageBarContents}
</div>
)}
filteredItems={attachMenuProps?.attachMenuItems}
{...(attachMenuProps && { isOpen: attachMenuProps.isAttachMenuOpen })}
onOpenChange={(isAttachMenuOpen) => {
attachButtonRef.current?.focus();
attachMenuProps?.setIsAttachMenuOpen(isAttachMenuOpen);
attachMenuProps?.onAttachMenuOpenChange && attachMenuProps?.onAttachMenuOpenChange(isAttachMenuOpen);
}}
onOpenChangeKeys={attachMenuProps?.onAttachMenuOnOpenChangeKeys}
onSelect={attachMenuProps?.onAttachMenuSelect}
{...(attachMenuProps && { handleTextInputChange: attachMenuProps.onAttachMenuInputChange })}
popperProps={{ direction: 'up', distance: 8 }}
searchInputPlaceholder={attachMenuProps?.attachMenuInputPlaceholder}
{...attachMenuProps}
/>
);
}
return (
<div
className={css(
'pf-chatbot__message-bar',
isPrimary && 'pf-m-primary',
isMultiline && 'pf-m-multiline',
className
)}
>
{messageBarContents}
</div>
);
};
const MessageBar = forwardRef((props: MessageBarProps, ref: Ref<HTMLTextAreaElement>) => (
<MessageBarBase innerRef={ref} {...props} />
));
export { MessageBar };
export default MessageBar;