-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChatInput.jsx
More file actions
596 lines (562 loc) · 18 KB
/
Copy pathChatInput.jsx
File metadata and controls
596 lines (562 loc) · 18 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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import React, {
useEffect,
useRef,
useState,
forwardRef,
useImperativeHandle,
} from "react";
import {
ChatBubble,
Notes,
Attachment,
Forward,
BookOpen,
CannedResponses,
Down,
SendPlane,
} from "@bigbinary/neeto-icons-rn";
import { parse } from "node-html-parser";
import PropTypes from "prop-types";
import ExpandSVG from "@assets/icons/expand.svg";
import MinimizeSVG from "@assets/icons/minimize.svg";
import { moderateScale } from "@utils/scale";
import { AttachmentsView } from "./AttachmentsView";
import { EmailFields } from "./EmailFields";
import { IconButton } from "./IconButton";
import { MentionsInputWrapper } from "./MentionsInputWrapper";
import { theme } from "../../theme";
import { Button } from "../Button";
import { Container } from "../Container";
import { LineLoader } from "../LineLoader";
import { Popover } from "../Popover";
// eslint-disable-next-line @bigbinary/neeto/no-dangling-constants
const OPTION_TYPES = {
REPLY: "REPLY",
NOTE: "NOTE",
FORWARD: "FORWARD",
};
const placeholders = {
[OPTION_TYPES.REPLY]: "Type here to reply…",
[OPTION_TYPES.NOTE]: "Add note here…",
[OPTION_TYPES.FORWARD]: "Type here to forward…",
};
const labels = {
[OPTION_TYPES.NOTE]: "Add note",
[OPTION_TYPES.FORWARD]: "Forward",
};
/**
* ChatInput component supports various options like `REPLY`, `NOTE` and `FORWARD`.
* This component supports below props categories from [styled-system ](/styled-system).
* <ul>
* <li>flexbox</li>
* <li>space</li>
* <li>border</li>
* <li>buttonStyle</li>
* <li>brandLeft</li>
* <li>typography</li>
* </ul>
*
* <div class="screenshots">
* <img src="screenshots/chatInput/chatInput.png" />
* </div>
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Typography, ChatInput, Container } from '@bigbinary/neetoui-rn';
*
* export default function Main() {
* const [value, setValue] = React.useState(
* "Hey Oliver, We are working on this issue. We will keep you update");
*
* return (
* <Container flex={1}>
* <Container alignItems="center" flex={1} justifyContent="center">
* <Typography> Desk Example</Typography>
* </Container>
* <Container align-self="end">
* <ChatInput
* shouldShowEmailFields
* attachmentsCount={2}
* toEmails="oliver@example.com"
* value={value}
* Attachments={
* <Container alignItems="flex-start">
* <AnimatedImage
* imageHeight={30}
* imageUrl="https://picsum.photos/255/139"
* imageWidth={30}
* resizeMode="cover"
* />
* </Container>
* }
* onChangeText={setValue}
* onCannedResponse={() => {
* alert("On Canned Response");
* }}
* onForward={() => {
* alert("On Forward");
* }}
* />
* </Container>
* </Container>
* );
* }
* ```
*/
const convertMentionsToHTMLAndPlainText = ({ suggestions, value }) => {
const mentionsRegex = new RegExp(/@\[[^)]*\)/g);
let html = `<p>${value.replace(/\n/g, "<br>")}</p>`; // Replace newlines with <br> for HTML output
let plainText = value;
const allMentions = html.matchAll(mentionsRegex);
for (const mentionObject of allMentions) {
const mention = mentionObject[0];
const id = /\((.*)\)/.exec(mention)[1];
const suggestion = suggestions.find(suggestion => suggestion.id === id);
const rootNode = parse("<span></span>");
const spanNode = rootNode.querySelector("span");
spanNode.setAttribute("data-type", "mention");
spanNode.setAttribute("data-id", id);
spanNode.setAttribute("data-label", suggestion.name);
spanNode.set_content(`@${suggestion.name}`);
const rootNodeString = rootNode.toString();
html = html.replace(mention, rootNodeString);
const plainTextMention = `@${suggestion.name}`;
plainText = plainText.replace(mention, plainTextMention);
}
return { html, plainText };
};
export const ChatInput = forwardRef(
(
{
shouldShowEmailFields,
showReplyMenuOptions,
value = "",
onChangeText = () => {},
onForward,
onCannedResponse,
onArticles,
showArticlesFor = [OPTION_TYPES.REPLY, OPTION_TYPES.NOTE],
toEmails: initialToEmails,
onReply = () => {},
onAddNote = () => {},
onAttachment = () => {},
attachmentsCount,
Attachments,
showCannedResponsesFor = [OPTION_TYPES.REPLY],
showSuggestionsFor = [OPTION_TYPES.NOTE],
disabled,
isLoading = true,
onOptionChange = () => {},
initialSelectedOption = OPTION_TYPES.REPLY,
suggestions,
...rest
},
ref
) => {
const inputRef = useRef();
const [selectedOption, setSelectedOption] = useState(initialSelectedOption);
const [isEmailFieldsVisible, setIsEmailFieldsVisible] = useState(false);
const [isAttachmentsVisible, setIsAttachmentsVisible] = useState(false);
const isReplyOptionSelected = selectedOption === OPTION_TYPES.REPLY;
const isNoteOptionSelected = selectedOption === OPTION_TYPES.NOTE;
const isForwardOptionSelected = selectedOption === OPTION_TYPES.FORWARD;
const [toEmails, setToEmails] = useState(initialToEmails ?? []);
const [toEmailsForForward, setToEmailsForForward] = useState([]);
const [ccEmails, setCcEmails] = useState([]);
const [bccEmails, setBccEmails] = useState([]);
const moreReplyOptions = [
{
label: "Send and set as closed",
Icon: () => null,
onPress: () => {
onReply({
toEmails,
ccEmails,
bccEmails,
status: "closed",
...convertMentionsToHTMLAndPlainText({ suggestions, value }),
});
},
},
];
useImperativeHandle(
ref,
() => ({
clearEmailFields: () => {
setToEmails(initialToEmails ?? []);
setToEmailsForForward([]);
setCcEmails([]);
setBccEmails([]);
inputRef.current.blur();
},
}),
[initialToEmails, isReplyOptionSelected]
);
useEffect(() => {
onOptionChange(selectedOption);
}, [selectedOption]);
useEffect(() => {
setSelectedOption(initialSelectedOption);
}, [initialSelectedOption]);
useEffect(() => {
setToEmails(initialToEmails ?? []);
}, [initialToEmails]);
const showEmailFieldsAndAttachments = () => {
setIsAttachmentsVisible(true);
!isNoteOptionSelected && setIsEmailFieldsVisible(true);
};
const hideEmailFieldsAndAttachments = () => {
setIsAttachmentsVisible(false);
setIsEmailFieldsVisible(false);
};
const onReplyClickHandler = () => {
hideEmailFieldsAndAttachments();
inputRef.current.focus();
setSelectedOption(OPTION_TYPES.REPLY);
};
const onAddNoteClickHandler = () => {
hideEmailFieldsAndAttachments();
inputRef.current.focus();
setIsEmailFieldsVisible(false);
setSelectedOption(OPTION_TYPES.NOTE);
};
const onAddForwardClickHandler = () => {
inputRef.current.focus();
setIsEmailFieldsVisible(true);
setIsAttachmentsVisible(false);
setSelectedOption(OPTION_TYPES.FORWARD);
};
const onAddAttachmentsClickHandler = () => {
if (!isAttachmentsVisible && attachmentsCount > 0) {
// When attachments are not visible but there are few attachments then we no need to show the upload modal.
} else {
onAttachment();
}
setIsAttachmentsVisible(true);
};
const onActionHandler = () => {
({
[OPTION_TYPES.REPLY]: () => {
onReply({
toEmails,
ccEmails,
bccEmails,
...convertMentionsToHTMLAndPlainText({ suggestions, value }),
});
},
[OPTION_TYPES.NOTE]: () => {
onAddNote({
toEmails,
ccEmails,
bccEmails,
...convertMentionsToHTMLAndPlainText({ suggestions, value }),
});
},
[OPTION_TYPES.FORWARD]: () => {
onForward({
toEmails: toEmailsForForward,
ccEmails,
bccEmails,
...convertMentionsToHTMLAndPlainText({ suggestions, value }),
});
},
})[selectedOption]();
};
const shouldShowExpandAndMinimizeButton =
attachmentsCount > 0 || (toEmails.length > 0 && !isNoteOptionSelected);
const shouldDisableWhenForwardAndToFieldMissing =
isForwardOptionSelected && toEmailsForForward.length === 0;
return (
<Container>
<LineLoader
backgroundColor={theme.colors.background.grey400}
height={moderateScale(1)}
isLoading={isLoading}
/>
<Container
bg={isNoteOptionSelected ? "background.oldLace" : "transparent"}
p={moderateScale(5)}
pt={0}
px={moderateScale(16)}
>
<Container pt={moderateScale(8)}>
<EmailFields
bccEmails={bccEmails}
ccEmails={ccEmails}
isEmailFieldsVisible={isEmailFieldsVisible}
isForwardOptionSelected={isForwardOptionSelected}
isNoteOptionSelected={isNoteOptionSelected}
isReplyOptionSelected={isReplyOptionSelected}
setBccEmails={setBccEmails}
setCcEmails={setCcEmails}
setIsEmailFieldsVisible={setIsEmailFieldsVisible}
setToEmails={setToEmails}
setToEmailsForForward={setToEmailsForForward}
shouldShowEmailFields={shouldShowEmailFields}
toEmails={toEmails}
toEmailsForForward={toEmailsForForward}
/>
<Container flexDirection="row" justifyContent="space-between">
<MentionsInputWrapper
placeholder={placeholders[selectedOption]}
ref={inputRef}
suggestions={suggestions}
value={value}
shouldShowSuggestions={showSuggestionsFor.includes(
selectedOption
)}
onChange={onChangeText}
{...rest}
/>
{shouldShowExpandAndMinimizeButton && (
<Container alignSelf="center">
{isEmailFieldsVisible || isAttachmentsVisible ? (
<IconButton
Icon={MinimizeSVG}
height={moderateScale(30)}
pt={moderateScale(8)}
width={moderateScale(30)}
onPress={hideEmailFieldsAndAttachments}
/>
) : (
<IconButton
Icon={ExpandSVG}
height={moderateScale(30)}
pt={moderateScale(8)}
width={moderateScale(30)}
onPress={showEmailFieldsAndAttachments}
/>
)}
</Container>
)}
</Container>
<AttachmentsView
Attachments={Attachments}
attachmentsCount={attachmentsCount}
isAttachmentsVisible={isAttachmentsVisible}
isNoteOptionSelected={isNoteOptionSelected}
setIsAttachmentsVisible={setIsAttachmentsVisible}
/>
<Container
alignItems="center"
flexDirection="row"
justifyContent="space-between"
mt={moderateScale(10)}
>
<Container flexDirection="row" justifyContent="space-between">
<IconButton
Icon={ChatBubble}
opacity={isReplyOptionSelected ? 1 : 0.5}
pl={moderateScale(10)}
iconProps={{
viewBox: "0 0 37 37",
size: moderateScale(30),
color: isReplyOptionSelected
? theme.colors.font.grey700
: theme.colors.font.grey500,
}}
onPress={onReplyClickHandler}
/>
<IconButton
Icon={Notes}
opacity={isNoteOptionSelected ? 1 : 0.5}
onPress={onAddNoteClickHandler}
/>
{onForward && (
<IconButton
Icon={Forward}
opacity={isForwardOptionSelected ? 1 : 0.5}
onPress={onAddForwardClickHandler}
/>
)}
<Container
bg="background.grey400"
mx={moderateScale(5)}
p={moderateScale(1)}
/>
{onCannedResponse &&
showCannedResponsesFor.includes(selectedOption) && (
<IconButton
Icon={CannedResponses}
opacity={0.5}
iconProps={{
viewBox: "0 0 38 38",
size: moderateScale(30),
}}
onPress={onCannedResponse}
/>
)}
{onArticles && showArticlesFor.includes(selectedOption) && (
<IconButton
Icon={BookOpen}
opacity={0.5}
iconProps={{
size: moderateScale(30),
}}
onPress={onArticles}
/>
)}
<IconButton
{...(isLoading && {
iconProps: { color: "grey" },
})}
Icon={Attachment}
disabled={isLoading}
onPress={onAddAttachmentsClickHandler}
/>
</Container>
<Container alignItems="center" flexDirection="row">
<Button
height={moderateScale(30)}
label={labels[selectedOption] ?? ""}
pr={0}
variant="text"
RightIcon={
selectedOption === OPTION_TYPES.REPLY
? () => (
<SendPlane
color={theme.colors.font.primary}
size={moderateScale(22)}
/>
)
: null
}
disabled={
shouldDisableWhenForwardAndToFieldMissing || disabled
}
labelStyle={{
mx: moderateScale(0),
}}
onPress={() => {
onActionHandler();
hideEmailFieldsAndAttachments();
}}
/>
{showReplyMenuOptions && isReplyOptionSelected && (
<Popover
data={moreReplyOptions}
from={
<Button
disabled={disabled}
height={moderateScale(30)}
label=""
p={0}
variant="text"
RightIcon={() => (
<Down
color={theme.colors.background.grey800}
size={moderateScale(22)}
/>
)}
/>
}
/>
)}
</Container>
</Container>
</Container>
</Container>
</Container>
);
}
);
ChatInput.displayName = "ChatInput";
ChatInput.propTypes = {
/**
* If true, Shows loader
*/
isLoading: PropTypes.bool,
/**
* If true, Shows to, cc and bcc email inputs.
*/
shouldShowEmailFields: PropTypes.bool,
/**
* If true, Shows reply menu options
*/
showReplyMenuOptions: PropTypes.bool,
/**
* Value to make input component controllable.
*/
value: PropTypes.string,
/**
* To set the initial selected option
*/
initialSelectedOption: PropTypes.oneOf(Object.values(OPTION_TYPES)),
/**
* Callback to be called when user selection option.
*/
onOptionChange: PropTypes.func,
/**
* Callback to be called when the input text changes.
*/
onChangeText: PropTypes.func,
/**
* Callback to be called on click of forward icon.
*/
onForward: PropTypes.func,
/**
* Callback to be called on click of canned responses icon.
*/
onCannedResponse: PropTypes.func,
/**
* Callback to be called on click of articles icon. Icon is shown only when this is passed.
*/
onArticles: PropTypes.func,
/**
* Array for options to show the articles icon. Example: ["REPLY","NOTE"]
*/
showArticlesFor: PropTypes.arrayOf(
PropTypes.oneOf(Object.values(OPTION_TYPES))
),
/**
* Email list separated by comma.
*/
toEmails: PropTypes.arrayOf(PropTypes.string),
/**
* Callback to be called on click of reply icon.
*/
onReply: PropTypes.func,
/**
* Callback to be called on click of add note icon.
*/
onAddNote: PropTypes.func,
/**
* Callback to be called on click of attachment icon.
*/
onAttachment: PropTypes.func,
/**
* Count of attachments.
*/
attachmentsCount: PropTypes.number,
/**
* Component to render attachments.
*/
Attachments: PropTypes.any,
/**
* Array for options to show canned responses. Example: ["REPLY","NOTES","FORWARD"]
*/
showCannedResponsesFor: PropTypes.arrayOf(
PropTypes.oneOf(Object.values(OPTION_TYPES))
),
/**
* Array for options to show suggestions. Example: ["REPLY","NOTES","FORWARD"]
*/
showSuggestionsFor: PropTypes.arrayOf(
PropTypes.oneOf(Object.values(OPTION_TYPES))
),
/**
* If true, Disables the reply, forward and add note button.
*/
disabled: PropTypes.bool,
/**
* List of mentions
*/
suggestions: PropTypes.arrayOf(
PropTypes.shape({
name: PropTypes.string,
imageUrl: PropTypes.string,
id: PropTypes.string,
})
),
};