-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathExample.js
More file actions
173 lines (159 loc) · 4.33 KB
/
Example.js
File metadata and controls
173 lines (159 loc) · 4.33 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
import React, { useState } from 'react';
import ChatBox, { ChatFrame } from '../src';
import { FileSelectMode } from '../src/constant';
import RobotIcon from './bot.svg';
import './Example.css';
const Example = () => {
const [activeAuthor, setActiveAuthor] = useState({ id: 1, username: 'user1', avatarUrl: null })
const [attr, setAttr] = useState({
showChatbox: false,
showIcon: true,
messages: [
{
text: 'user2 has joined the conversation',
timestamp: 1578366389250,
type: 'notification',
},
{
author: {
username: 'user1',
id: 1,
avatarUrl: 'https://image.flaticon.com/icons/svg/2446/2446032.svg',
},
text: 'Hi',
type: 'text',
timestamp: 1578366393250,
},
{
author: {
username: 'user2',
id: 2,
avatarUrl: null
},
text: 'Show two buttons',
type: 'text',
timestamp: 1578366425250,
buttons: [
{
type: 'URL',
title: 'Yahoo',
payload: 'http://www.yahoo.com',
},
{
type: 'URL',
title: 'Example',
payload: 'http://www.example.com',
},
],
},
{
author: {
username: 'user1',
id: 1,
avatarUrl: 'https://image.flaticon.com/icons/svg/2446/2446032.svg',
},
text: "What's up?",
type: 'text',
timestamp: 1578366425250,
hasError: true,
},
{
author: {
username: 'user1',
id: 1
},
authorFor: {
username: 'user2',
id: 2
},
text: "Hello",
type: 'text',
timestamp: 1578366425251
},
{
author: {
username: 'user2',
id: 2
},
authorFor: {
username: 'user1',
id: 1
},
text: "Hi",
type: 'text',
timestamp: 1578366425252
}
],
});
const handleClickIcon = () => {
// toggle showChatbox and showIcon
setAttr({
...attr,
showChatbox: !attr.showChatbox,
showIcon: !attr.showIcon,
});
};
const handleOnSendMessage = (message, files = [], authorIdFor = "0") => {
/*
In this example, we are receiving the actual files.
In a real-world scenario, you would post the message, along with the files, to an endpoint/websocket,
and from the result, you would receive, for example, the link to the file you sent, along with other information, and you would
work with the link.
So, in this simple example, I fake a link/url for each file, using: URL.createObjectURL()
*/
let currMessage = {
author: {
username: 'user1',
id: 1
},
text: message,
type: 'text',
timestamp: +new Date()
};
if(authorIdFor && authorIdFor !== "0")
currMessage.authorFor = { id: authorIdFor, username: `user${authorIdFor}` };
if(files && files.length > 0) {
let buttons = []
for(let i = 0; i < files.length; i++) {
buttons.push({
type: 'URL',
title: files[i].name,
payload: URL.createObjectURL(files[i])
})
}
currMessage.buttons = buttons;
}
setAttr({...attr, messages: [...attr.messages, currMessage]});
};
const handleOnMessageButtonClick = (payload) => {
alert(`Clicked: ${payload}`);
}
return (
<ChatFrame
chatbox={
<ChatBox
style={{ width: '300px' }}
onSendMessage={handleOnSendMessage}
onMessageButtonClick={handleOnMessageButtonClick}
userId={1}
messages={attr.messages}
showTypingIndicator={true}
fileSelectMode={FileSelectMode.Multiple}
activeAuthor={activeAuthor}
authors={ [{ id: 2, username: 'user2' }, { id: 3, username: 'user3' }] }
allowDirectMessage={false}
/>
}
icon={<RobotIcon className="Icon" />}
clickIcon={handleClickIcon}
showChatbox={attr.showChatbox}
showIcon={attr.showIcon}
iconStyle={{ background: 'red', fill: 'white' }}
>
<div className="Greeting" style={{ width: '300px' }}>
👋 Hey, I’m a ChatBot! Want to see what I can do?
</div>
</ChatFrame>
);
}
export default Example;