-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomponent.utils.tsx
More file actions
384 lines (347 loc) · 10.8 KB
/
Copy pathcomponent.utils.tsx
File metadata and controls
384 lines (347 loc) · 10.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
import { CSSProperties } from 'react';
import {
ArrowLeftOutlined,
AudioOutlined,
FileImageOutlined,
FileOutlined,
GlobalOutlined,
PhoneOutlined,
UserOutlined,
VideoCameraOutlined,
} from '@ant-design/icons';
import { Button, Col, Row, Space, Typography, Image, Flex } from 'antd';
import OTP from 'antd/es/input/OTP';
import parse from 'html-react-parser';
import { TextFormatter } from './text-formatter';
import DoubleTickIcon from 'assets/double-tick.svg?react';
import TickIcon from 'assets/tick.svg?react';
import {
GetTemplateMessageLayoutOptions,
LanguageLiteral,
Renderable,
StatusMessage,
TypeMessage,
} from 'types';
const INTERACTIVE_BUTTON_ICONS = {
PHONE_NUMBER: <PhoneOutlined style={{ fontSize: 18 }} />,
COPY_CODE: <FileOutlined style={{ fontSize: 18 }} />,
URL: <GlobalOutlined style={{ fontSize: 18 }} />,
QUICK_REPLY: <ArrowLeftOutlined style={{ fontSize: 18 }} />,
OTP: <OTP style={{ fontSize: 18 }} />,
} as const;
export function getOutgoingStatusMessageIcon(
statusMessage?: StatusMessage,
styles?: CSSProperties
) {
switch (statusMessage) {
case 'sent':
return <TickIcon style={{ ...styles, color: '#8696a0' }} />;
case 'delivered':
return <DoubleTickIcon style={{ ...styles, color: '#8696a0' }} />;
case 'read':
return <DoubleTickIcon style={{ ...styles, color: 'var(--light-primary-color)' }} />;
default:
return null;
}
}
export function getMessageTypeIcon(typeMessage: TypeMessage, downloadUrl?: string) {
let messageTypeIcon: JSX.Element | null = null;
switch (typeMessage) {
case 'imageMessage':
messageTypeIcon = <FileImageOutlined />;
break;
case 'audioMessage':
messageTypeIcon = <AudioOutlined />;
break;
case 'videoMessage':
messageTypeIcon = <VideoCameraOutlined />;
break;
case 'documentMessage':
messageTypeIcon = <FileOutlined />;
break;
case 'locationMessage':
messageTypeIcon = <FileOutlined />;
break;
case 'contactMessage':
messageTypeIcon = <UserOutlined />;
break;
default:
break;
}
if (!messageTypeIcon) {
return null;
}
if (!downloadUrl) {
return messageTypeIcon;
}
return (
<a href={downloadUrl} target="_blank" rel="noreferrer">
{messageTypeIcon}
</a>
);
}
export function getFormattedMessage(
textMessage: string,
options: { enableMarkdownLinks?: boolean; compact?: boolean } = {}
): Renderable {
const formattedText = TextFormatter(textMessage, options);
if (!formattedText) {
return textMessage;
}
return (
<span className="text-overflow" style={{ width: 300 }}>
{parse(formattedText)}
</span>
);
}
export function fillString(string: string, data: string[]) {
return string.replaceAll(/{(\d)}/g, (_, index) => data[+index]);
}
export function fillJsxString(string: string, data: (JSX.Element | string)[]) {
const substringArray = string.split(/{\d}/);
data = data.filter((_, index) => string.includes(`{${index}`));
return (
<span>
{substringArray.map((substring, index) => {
return (
<span key={`${substring}${index}`}>
{substring} {data[index] || ''}{' '}
</span>
);
})}
</span>
);
}
export function fillTemplateString(string: string, data: string[]) {
const substringArray = string.split(/{{\d}}/);
data = data.filter((_, index) => string.includes(`{{${index + 1}}}`));
return substringArray.map((substring, index) => `${substring}${data[index] || ''}`).join('');
}
export function numWord(
count: number,
words: Record<LanguageLiteral, string[]>,
resolvedLanguage: LanguageLiteral
) {
const val = Math.abs(count) % 100;
const num = val % 10;
if (val > 10 && val < 20) return words[resolvedLanguage][2];
if (num > 1 && num < 5) return words[resolvedLanguage][1];
if (num == 1) return words[resolvedLanguage][0];
return words[resolvedLanguage][2];
}
export function getTemplateMessageLayout(options: GetTemplateMessageLayoutOptions) {
const { containerClassName, header, content, footer, mediaUrl, buttons, symbol, type, time } =
options;
if (!containerClassName) {
return (
<>
<Space direction="vertical">
{header && (
<Typography.Paragraph
style={{ fontSize: 16, margin: 0 }}
className={`${type === 'outgoing' ? 'outgoing' : 'incoming'} full`}
>
{header}
</Typography.Paragraph>
)}
{mediaUrl && <Image width={300} loading="lazy" src={mediaUrl} alt="media" />}
<Typography.Paragraph
style={{ fontSize: 14, margin: 0 }}
ellipsis={{ rows: 6, expandable: true, symbol: symbol }}
className={`${type === 'outgoing' ? 'outgoing' : 'incoming'} full`}
>
{content}
</Typography.Paragraph>
</Space>
<Row wrap={false} gutter={[15, 15]} className="margin-top">
{footer && (
<Col>
<Typography.Paragraph
className={`${type === 'outgoing' ? 'outgoing' : 'incoming'} full`}
style={{ fontSize: 13, margin: 0, fontStyle: 'italic' }}
>
{footer}
</Typography.Paragraph>
</Col>
)}
</Row>
{buttons && buttons.length > 0 && (
<Space direction="vertical">
{buttons.map((button, idx) => (
<Button
key={`${button.value}-${idx}`}
className="w-100"
style={{ textWrap: 'wrap', padding: 4, height: 'auto' }}
>
{button.text}
</Button>
))}
</Space>
)}
</>
);
}
return (
<div
style={{
maxWidth: 300,
}}
className={containerClassName}
>
<Space direction="vertical">
{header && (
<Typography.Paragraph style={{ fontSize: 16, margin: 0 }}>{header}</Typography.Paragraph>
)}
{mediaUrl && <Image src={mediaUrl} loading="lazy" alt="media" />}
<Typography.Paragraph
style={{ fontSize: 14, margin: 0 }}
ellipsis={{ rows: 6, expandable: true, symbol: symbol }}
>
{content}
</Typography.Paragraph>
</Space>
<Row wrap={false} gutter={[15, 15]} className="margin-top">
{footer && (
<Col>
<Typography.Paragraph style={{ fontSize: 13, margin: 0, fontStyle: 'italic' }}>
{footer}
</Typography.Paragraph>
</Col>
)}
{time && (
<Col className="margin-left-auto margin-top-auto">
<Space>
<span style={{ fontSize: 12 }}>{time}</span>
</Space>
</Col>
)}
</Row>
{buttons && buttons.length > 0 && (
<Space direction="vertical">
{buttons.map((button, idx) => (
<Button key={`${button.value}-${idx}`} className="w-100">
<div>{button.text}</div>
</Button>
))}
</Space>
)}
</div>
);
}
export function getInteractiveButtonsMessageLayout(options: GetTemplateMessageLayoutOptions) {
const { containerClassName, header, content, footer, mediaUrl, buttons, symbol, type, time } =
options;
if (!containerClassName) {
return (
<div
style={{
maxWidth: 300,
}}
>
<>
{header && (
<Typography.Paragraph
style={{ fontSize: 16, margin: 0 }}
className={`${type === 'outgoing' ? 'outgoing' : 'incoming'} full w-100 interactiveButtonsBody `}
>
{header}
</Typography.Paragraph>
)}
{mediaUrl && <Image width={300} loading="lazy" src={mediaUrl} alt="media" />}
<Typography.Paragraph
style={{ fontSize: 14, margin: 0 }}
ellipsis={{ rows: 6, expandable: true, symbol: symbol }}
className={`${type === 'outgoing' ? 'outgoing' : 'incoming'} full w-100 interactiveButtonsBody`}
>
{content}
</Typography.Paragraph>
</>
<Row wrap={false} gutter={[5, 5]} className="interactiveButtonsBody">
{footer && (
<Col>
<Typography.Paragraph
className={`${type === 'outgoing' ? 'outgoing' : 'incoming'} full`}
style={{ fontSize: 13, margin: 0, fontStyle: 'italic' }}
>
{footer}
</Typography.Paragraph>
</Col>
)}
</Row>
{buttons && buttons.length > 0 && (
<Flex vertical gap={0}>
{buttons.map((button, idx) => (
<Button
variant="outlined"
key={`${button.value}-${idx}`}
className="w-100"
style={{
padding: 14,
borderRadius: 0,
height: 50,
width: '300px',
whiteSpace: 'nowrap',
backgroundColor: 'transparent',
borderRight: '0px',
borderLeft: '0px',
borderTop: idx === 0 ? undefined : '0px',
}}
>
{INTERACTIVE_BUTTON_ICONS[button.type] || null}
<div style={{ width: '100%', textOverflow: 'ellipsis', overflow: 'hidden' }}>
{button.text}
</div>
</Button>
))}
</Flex>
)}
</div>
);
}
return (
<div
style={{
maxWidth: 300,
}}
className={containerClassName}
>
<Space direction="vertical">
{header && (
<Typography.Paragraph style={{ fontSize: 16, margin: 0 }}>{header}</Typography.Paragraph>
)}
{mediaUrl && <Image src={mediaUrl} loading="lazy" alt="media" />}
<Typography.Paragraph
style={{ fontSize: 18, margin: 0 }}
ellipsis={{ rows: 6, expandable: true, symbol: symbol }}
>
{content}
</Typography.Paragraph>
</Space>
<Row wrap={false} gutter={[5, 5]}>
{footer && (
<Col>
<Typography.Paragraph style={{ fontSize: 13, margin: 0, fontStyle: 'italic' }}>
{footer}
</Typography.Paragraph>
</Col>
)}
{time && (
<Col className="margin-left-auto margin-top-auto">
<Space>
<span style={{ fontSize: 12 }}>{time}</span>
</Space>
</Col>
)}
</Row>
{buttons && buttons.length > 0 && (
<Space direction="vertical">
{buttons.map((button, idx) => (
<Button key={`${button.value}-${idx}`} className="w-100">
<div>{button.text}</div>
</Button>
))}
</Space>
)}
</div>
);
}