-
Notifications
You must be signed in to change notification settings - Fork 53
/
App.js
201 lines (185 loc) · 5.63 KB
/
App.js
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
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import React, { Component } from 'react';
import publicIp from 'public-ip';
import styles from './App.scss';
// local development mode
// eat your own dog food!
class App extends Component {
constructor(props) {
super(props);
this.getIP = this.getIP.bind(this);
this.state = {
ip: undefined,
};
}
syntaxHighlight(data) {
// strip secret
let json = {
...data,
apiToken: 'xxxxx',
};
json = JSON.stringify(json, undefined, 4);
json = json
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
// eslint-disable-next-line
return json.replace(
/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
}
);
}
createMarkup(dom) {
return { __html: dom };
}
getIP() {
return (
this.state.ip ||
publicIp
.v4()
.then((ip) => this.setState({ ip }))
.catch(console.log)
);
}
render() {
const getClientID = this.state.ip || this.getIP();
const getClientAvatar = `https://robohash.org/${getClientID}`;
const slackChatProps = {
botName: getClientID,
channels: [
{
name: 'mac',
icon: 'https://image.flaticon.com/icons/svg/141/141021.svg',
},
{
name: 'pc',
icon: 'https://image.flaticon.com/icons/svg/224/224597.svg',
},
{
name: 'linux',
icon: 'https://image.flaticon.com/icons/svg/226/226772.svg',
},
{
name: 'test',
id: '',
icon: '',
},
],
apiToken: '==',
helpText: 'Need Help?',
singleUserMode: false,
defaultMessage: 'Welcome! Someone will help shortly.',
themeColor: '#856090',
debugMode: true,
userImage: getClientAvatar,
hooks: [
{
/* My Custom Hook */
id: 'getSystemInfo',
action: () => 'MY SYSTEM INFO!',
},
],
};
const Chat = ({ ReactSlackChat }) =>
!this.state.ip ? (
<div className={styles['loading']}>
<h2>Now Loading...</h2>
</div>
) : (
<ReactSlackChat {...slackChatProps} />
);
const codeHighlight = this.createMarkup(
this.syntaxHighlight(slackChatProps)
);
const promisedReactSlackChat = !process.env.DEPLOY_MODE
? // local development for library
import(
/* webpackChunkName: "local-dev-ReactSlackChat" */ '../ReactSlackChat'
)
: // deploy mode will consume the generated node module
// gotta eat your own dog food
import(
/* webpackChunkName: "node-module-ReactSlackChat" */ '../../../dist/react-slack-chat-with-default-hooks'
);
!this.state.resolvedReactSlackChat &&
promisedReactSlackChat.then((ReactSlackChat) => {
console.log('Got a promisifed React Slack Chat chunk', ReactSlackChat);
return this.setState({
resolvedReactSlackChat: ReactSlackChat,
});
});
const { ReactSlackChat } = this.state.resolvedReactSlackChat || {};
return (
<>
{!ReactSlackChat ? (
<div className={styles['loading']}>
<h2>Now Loading...</h2>
</div>
) : (
<div className={styles['App']}>
<div className={styles['App-header']}>
<img
src={`https://robohash.org/${new Date()}`}
className={styles['App-logo']}
alt="logo"
/>
<h2>
Welcome to{' '}
<a
className={styles['gitLink']}
href="https://github.com/5punk/react-slack-chat"
>
React Slack Chat
</a>
</h2>
<a href="https://github.com/5punk/react-slack-chat">
<img
className={styles['ribbon']}
src="https://camo.githubusercontent.com/52760788cde945287fbb584134c4cbc2bc36f904/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f77686974655f6666666666662e706e67"
alt="Fork me on GitHub"
data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png"
/>
</a>
</div>
<p className={styles['App-intro']}>
Here's an example configuration to load the widget.
</p>
<pre className={styles['codeBlock']}>
<p><ReactSlackChat</p>
<pre dangerouslySetInnerHTML={codeHighlight} />
<p>/></p>
</pre>
<div className={styles['welp']}>
<hr />
<h1>Welp! How does all this magic work?</h1>
<p>
Easy! Read the TLDR{' '}
<a href="https://github.com/5punk/react-slack-chat#react-slack-chat">
setup instructions
</a>
.
</p>
</div>
<Chat ReactSlackChat={ReactSlackChat} />
</div>
)}
</>
);
}
}
export default App;