-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathApp.tsx
198 lines (182 loc) · 6.65 KB
/
App.tsx
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
import React from 'react';
import {ChatWidget, ChatWindow} from '@papercups-io/chat-widget';
import {Papercups} from '@papercups-io/browser';
// NB: during development, replace this with valid account/inbox IDs from your dev db
const TEST_ACCOUNT_ID = '2ebbad4c-b162-4ed2-aff5-eaf9ebf469a5';
const TEST_INBOX_ID = 'eab9c66e-ea8a-46f7-9565-3927ec55e20d';
const cups = Papercups.init({
accountId: TEST_ACCOUNT_ID,
inboxId: TEST_INBOX_ID,
baseUrl: 'http://localhost:4000',
});
type Props = {disco?: boolean; displayChatWindow?: boolean};
const App = ({disco, displayChatWindow}: Props) => {
const colors = [
'#1890ff',
'#f5222d',
'#7cb305',
'#52c41a',
'#13c2c2',
'#722ed1',
'#eb2f96',
];
const [primaryColor, setPrimaryColor] = React.useState(colors[0]);
const [customer, setCustomerDetails] = React.useState<any>(null);
React.useEffect(() => {
if (!disco) {
return;
}
const interval = setInterval(() => {
const idx = colors.indexOf(primaryColor);
const next = (idx + 1) % (colors.length - 1);
setPrimaryColor(colors[next]);
}, 2000);
return () => clearInterval(interval);
}, [disco, colors, primaryColor]);
const handleIdentifyCustomer = () => {
const params = {
name: 'Demo User',
email: '[email protected]',
external_id: '789:[email protected]',
// Ad hoc metadata
metadata: {
plan: 'team',
registered_at: '2020-09-01',
age: 30,
valid: true,
},
};
cups.identify('789:[email protected]', params);
setCustomerDetails(params);
};
return (
<>
{displayChatWindow ? (
<div
style={{
padding: 32,
height: 480,
width: '50%',
minWidth: 320,
maxWidth: 400,
}}
>
<ChatWindow
token={TEST_ACCOUNT_ID}
inbox={TEST_INBOX_ID}
// deprecate `accountId`, use `token` instead
accountId={TEST_ACCOUNT_ID}
title='Welcome to Papercups!'
subtitle='Ask us anything in the chat window 😊'
primaryColor={primaryColor}
greeting='Hi there! How can I help you?'
newMessagePlaceholder='Start typing...'
agentAvailableText='Agents are online!'
agentUnavailableText='Agents are not available at the moment.'
customer={{
name: 'Test User',
email: '[email protected]',
external_id: '123',
// Ad hoc metadata
metadata: {
plan: 'starter',
registered_at: '2020-09-01',
age: 25,
valid: true,
},
}}
// NB: we override these values during development -- note that the
// API runs on port 4000 by default, and the iframe on 8080
baseUrl='http://localhost:4000'
iframeUrlOverride='http://localhost:8080'
requireEmailUpfront
showAgentAvailability
onChatLoaded={() => console.log('Chat loaded!')}
onChatClosed={() => console.log('Chat closed!')}
onChatOpened={() => console.log('Chat opened!')}
onMessageReceived={(message) =>
console.log('Message received!', message)
}
onMessageSent={(message) => console.log('Message sent!', message)}
/>
</div>
) : (
// Put <ChatWidget /> at the bottom of whatever pages you would
// like to render the widget on, or in your root/router component
// if you would like it to render on every page
<ChatWidget
token={TEST_ACCOUNT_ID}
inbox={TEST_INBOX_ID}
// deprecate `accountId`, use `token` instead
accountId={TEST_ACCOUNT_ID}
title='Welcome to Papercups!'
subtitle='Ask us anything in the chat window 😊'
primaryColor={primaryColor}
greeting='Hi there! How can I help you?'
awayMessage="Sorry, we're not available at the moment! Leave your email and we'll get back to you as soon as we can :)"
newMessagePlaceholder='Start typing...'
emailInputPlaceholder='What is your email address?'
newMessagesNotificationText='View new messages'
agentAvailableText='Agents are online!'
agentUnavailableText='Agents are not available at the moment.'
// customer={customer}
// NB: we override these values during development -- note that the
// API runs on port 4000 by default, and the iframe on 8080
baseUrl='http://localhost:4000'
iframeUrlOverride='http://localhost:8080'
requireEmailUpfront={!customer && !customer?.email}
showAgentAvailability
hideOutsideWorkingHours={false}
hideToggleButton={false}
popUpInitialMessage={1000}
isOpenByDefault
iconVariant='filled'
persistOpenState
// position={{side: 'right', offset: 80}}
debug
disableAnalyticsTracking
styles={{
chatContainer: {
// left: 20,
// right: 'auto',
// bottom: 160,
// maxHeight: 640,
},
toggleContainer: {
// left: 20,
// right: 'auto',
// bottom: 80,
},
toggleButton: {},
}}
// renderToggleButton={({isOpen, onToggleOpen}) => (
// <button onClick={onToggleOpen}>{isOpen ? 'Close' : 'Open'}</button>
// )}
onChatLoaded={({open, close, identify}) =>
console.log('Chat loaded!', {open, close, identify})
}
onChatClosed={() => console.log('Chat closed!')}
onChatOpened={() => console.log('Chat opened!')}
onMessageReceived={(message) =>
console.log('Message received!', message)
}
onMessageSent={(message) => console.log('Message sent!', message)}
setDefaultGreeting={(settings) => {
const shouldDisplayAwayMessage = !!settings?.account
?.is_outside_working_hours;
return shouldDisplayAwayMessage
? "We're away at the moment, but we'll be back on Monday!"
: 'Hi there! How can I help you?';
}}
/>
)}
<button onClick={Papercups.open}>Open</button>
<button onClick={Papercups.close}>Close</button>
<button onClick={Papercups.toggle}>Toggle</button>
<button onClick={handleIdentifyCustomer}>
{customer && customer.email ? 'Logged in' : 'Log in'}
</button>
</>
);
};
export default App;