-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.js
223 lines (198 loc) · 6.2 KB
/
index.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
* External dependencies
*/
import React, { Component, Fragment } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { find, last, noop, some } from 'lodash';
import { localize } from 'i18n-calypso';
/**
* Internal dependencies
*/
import { Button } from '@automattic/components';
import {
deleteStoredKeyringConnection,
requestKeyringConnections,
} from 'calypso/state/sharing/keyring/actions';
import { getKeyringServiceByName } from 'calypso/state/sharing/services/selectors';
import QueryKeyringServices from 'calypso/components/data/query-keyring-services';
import requestExternalAccess from '@automattic/request-external-access';
import {
getKeyringConnectionsByName,
isKeyringConnectionsFetching,
} from 'calypso/state/sharing/keyring/selectors';
class KeyringConnectButton extends Component {
static propTypes = {
service: PropTypes.oneOfType( [
PropTypes.shape( {
ID: PropTypes.string.isRequired,
connect_URL: PropTypes.string.isRequired,
} ),
PropTypes.bool,
] ),
isFetching: PropTypes.bool,
keyringConnections: PropTypes.array,
onClick: PropTypes.func,
onConnect: PropTypes.func,
forceReconnect: PropTypes.bool,
primary: PropTypes.bool,
};
static defaultProps = {
onClick: noop,
onConnect: noop,
forceReconnect: false,
primary: false,
};
state = {
isOpen: false, // The service is visually opened
isConnecting: false, // A pending connection is awaiting authorization
isRefreshing: false, // A pending refresh is awaiting completion
isAwaitingConnections: false, // Waiting for Keyring Connections request to finish
};
onClick = () => {
this.props.onClick();
this.performAction();
};
/**
* Returns the current status of the service's connection.
*
* @returns {string} Connection status.
*/
getConnectionStatus() {
if ( this.props.isFetching || this.props.isAwaitingConnections ) {
// When connections are still loading, we don't know the status
return 'unknown';
}
// keyringConnections are already filtered for this.props.service.ID
if ( this.props.keyringConnections.length === 0 ) {
// If no connections exist, the service isn't connected
return 'not-connected';
}
if ( some( this.props.keyringConnections, { status: 'broken' } ) ) {
// A problematic connection exists
return 'reconnect';
}
// If all else passes, assume service is connected
return 'connected';
}
performAction = () => {
const { forceReconnect, keyringConnections } = this.props;
const connectionStatus = this.getConnectionStatus();
// Depending on current status, perform an action when user clicks the
// service action button
if ( 'connected' === connectionStatus && ! forceReconnect ) {
this.props.onConnect( last( keyringConnections ) );
} else {
this.addConnection();
}
};
/**
* Establishes a new connection.
*/
addConnection = () => {
this.setState( { isConnecting: true } );
if ( this.props.service ) {
// Attempt to create a new connection. If a Keyring connection ID
// is not provided, the user will need to authorize the app
requestExternalAccess( this.props.service.connect_URL, ( { keyring_id: keyringId } ) => {
if ( ! keyringId ) {
this.setState( { isConnecting: false } );
return;
}
// When the user has finished authorizing the connection
// (or otherwise closed the window), force a refresh
this.props.requestKeyringConnections( true );
// In the case that a Keyring connection doesn't exist, wait for app
// authorization to occur, then display with the available connections
this.setState( { isAwaitingConnections: true, keyringId } );
} );
} else {
this.setState( { isConnecting: false } );
}
};
UNSAFE_componentWillReceiveProps( nextProps ) {
if ( this.state.isAwaitingConnections ) {
this.setState( {
isAwaitingConnections: false,
isRefreshing: false,
} );
if ( this.didKeyringConnectionSucceed( nextProps.keyringConnections ) ) {
const newKeyringConnection = find( nextProps.keyringConnections, {
ID: this.state.keyringId,
} );
if ( newKeyringConnection ) {
this.props.onConnect( newKeyringConnection );
}
this.setState( { keyringId: null, isConnecting: false } );
}
}
}
/**
* Returns whether the Keyring authorization attempt succeeded
* in creating new Keyring account options.
*
* @param {Array} keyringConnections props to check on if a keyring connection succeeded.
* @returns {boolean} Whether the Keyring authorization attempt succeeded
*/
didKeyringConnectionSucceed( keyringConnections ) {
const hasAnyConnectionOptions = some(
keyringConnections,
( keyringConnection ) =>
keyringConnection.isConnected === false || keyringConnection.isConnected === undefined
);
if ( ! this.state.keyringId || keyringConnections.length === 0 || ! hasAnyConnectionOptions ) {
this.setState( { isConnecting: false } );
return false;
}
return true;
}
render() {
const { primary, service, translate } = this.props;
const { isConnecting, isRefreshing } = this.state;
const status = service ? this.getConnectionStatus() : 'unknown';
let localPrimary = false;
let warning = false;
let label;
const isPending = 'unknown' === status || isRefreshing || isConnecting;
if ( 'unknown' === status ) {
label = translate( 'Loading…' );
} else if ( isRefreshing ) {
label = translate( 'Reconnecting…' );
warning = true;
} else if ( isConnecting ) {
label = translate( 'Connecting…' );
} else {
label = this.props.children;
localPrimary = primary;
}
return (
<Fragment>
<QueryKeyringServices />
<Button
primary={ localPrimary }
scary={ warning }
onClick={ this.onClick }
disabled={ isPending }
>
{ label }
</Button>
</Fragment>
);
}
}
export default connect(
( state, ownProps ) => {
const service = getKeyringServiceByName( state, ownProps.serviceId );
const keyringConnections = service ? getKeyringConnectionsByName( state, service.ID ) : [];
const isFetching = isKeyringConnectionsFetching( state );
return {
service,
isFetching,
keyringConnections,
};
},
{
deleteStoredKeyringConnection,
requestKeyringConnections,
}
)( localize( KeyringConnectButton ) );