-
Notifications
You must be signed in to change notification settings - Fork 496
/
IntegrationsOverview.tsx
206 lines (183 loc) · 5.22 KB
/
IntegrationsOverview.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
199
200
201
202
203
204
205
206
import React from 'react';
import {RouteComponentProps} from 'react-router-dom';
import dayjs from 'dayjs';
import {Box, Flex} from 'theme-ui';
import qs from 'query-string';
import {colors, Button, Paragraph, Table, Tag, Text, Title} from '../common';
import Spinner from '../Spinner';
import * as API from '../../api';
type IntegrationType = {
key: 'slack' | 'gmail';
integration: string;
status: 'connected' | 'not_connected';
created_at?: string | null;
icon: string;
};
type Props = RouteComponentProps<{type?: string}> & {};
type State = {
loading: boolean;
integrations: Array<IntegrationType>;
};
class IntegrationsOverview extends React.Component<Props, State> {
state: State = {
loading: true,
integrations: [],
};
async componentDidMount() {
try {
const {match, location} = this.props;
const {search} = location;
const {type} = match.params;
if (type) {
await this.handleIntegrationType(type, search);
}
const integrations = await Promise.all([
this.fetchSlackIntegration(),
this.fetchGmailIntegration(),
]);
this.setState({integrations, loading: false});
} catch (err) {
console.log('Error loading integrations:', err);
this.setState({loading: false});
}
}
fetchSlackIntegration = async (): Promise<IntegrationType> => {
const auth = await API.fetchSlackAuthorization();
return {
key: 'slack',
integration: 'Slack',
status: auth ? 'connected' : 'not_connected',
created_at: auth ? auth.created_at : null,
icon: '/slack.svg',
};
};
fetchGmailIntegration = async (): Promise<IntegrationType> => {
return {
key: 'gmail',
integration: 'Gmail',
status: 'not_connected',
created_at: null,
icon: '/gmail.svg',
};
};
handleIntegrationType = (type: string, query: string = '') => {
const q = qs.parse(query);
switch (type) {
case 'slack':
const code = String(q.code);
return API.authorizeSlackIntegration(code).catch((err) =>
console.log('Failed to authorize Slack:', err)
);
default:
return null;
}
};
render() {
const {loading, integrations = []} = this.state;
const columns = [
{
title: 'Integration',
dataIndex: 'integration',
key: 'integration',
render: (value: string, record: any) => {
const {icon} = record;
return (
<Flex sx={{alignItems: 'center'}}>
<img src={icon} alt={value} style={{height: 20}} />
<Text strong style={{marginLeft: 8}}>
{value}
</Text>
</Flex>
);
},
},
{
title: 'Status',
dataIndex: 'status',
key: 'status',
render: (value: string) => {
return value === 'connected' ? (
<Tag color={colors.green}>Connected</Tag>
) : (
<Tag>Not connected</Tag>
);
},
},
{
title: 'Connected since',
dataIndex: 'created_at',
key: 'created_at',
render: (value: string) => {
if (!value) {
return '--';
}
return dayjs(value).format('MMMM DD, YYYY');
},
},
{
title: 'Action',
dataIndex: 'action',
key: 'action',
render: (action: any, record: any) => {
const {key, status} = record;
const isConnected = status === 'connected';
// NB: when testing locally, update `origin` to an ngrok url
// pointing at localhost: 4000 (or wherever your server is running)
const origin = window.location.origin;
const redirect = `${origin}/integrations/slack`;
const q = {
scope:
'incoming-webhook chat:write channels:history im:history channels:manage chat:write.public',
user_scope: 'channels:history im:history',
client_id: '1192316529232.1250363411891',
redirect_uri: redirect,
};
const query = qs.stringify(q);
switch (key) {
case 'slack':
return (
<a href={`https://slack.com/oauth/v2/authorize?${query}`}>
<Button>{isConnected ? 'Reconnect' : 'Connect'}</Button>
</a>
);
default:
return <Button disabled>Coming soon!</Button>;
}
},
},
];
if (loading) {
return (
<Flex
sx={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
height: '100%',
}}
>
<Spinner size={40} />
</Flex>
);
}
return (
<Box p={4}>
<Box mb={5}>
<Title level={4}>Integrations</Title>
<Paragraph>
<Text>
Connect with your favorite apps{' '}
<span role="img" aria-label="apps">
🚀
</span>
</Text>
</Paragraph>
<Box my={4}>
<Table dataSource={integrations} columns={columns} />;
</Box>
</Box>
</Box>
);
}
}
export default IntegrationsOverview;