2016-05-28 05:05:27 +08:00
|
|
|
import React from 'react';
|
2016-08-30 10:07:44 +08:00
|
|
|
import {shell} from 'electron'
|
2016-05-28 05:05:27 +08:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import {IdentityStore} from 'nylas-exports';
|
|
|
|
import {RetinaImg} from 'nylas-component-kit';
|
|
|
|
import networkErrors from 'chromium-net-errors';
|
2016-10-18 08:59:33 +08:00
|
|
|
import OnboardingActions from './onboarding-actions';
|
2016-05-28 05:05:27 +08:00
|
|
|
|
|
|
|
class InitialLoadingCover extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
ready: React.PropTypes.bool,
|
|
|
|
error: React.PropTypes.string,
|
|
|
|
onTryAgain: React.PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this._slowTimeout = setTimeout(() => {
|
|
|
|
this.setState({slow: true});
|
2016-05-28 06:37:44 +08:00
|
|
|
}, 3500);
|
2016-05-28 05:05:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
clearTimeout(this._slowTimeout);
|
|
|
|
this._slowTimeout = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const classes = classnames({
|
|
|
|
'webview-cover': true,
|
|
|
|
'ready': this.props.ready,
|
|
|
|
'error': this.props.error,
|
|
|
|
'slow': this.state.slow,
|
|
|
|
});
|
|
|
|
|
|
|
|
let message = this.props.error;
|
|
|
|
if (this.props.error) {
|
|
|
|
message = this.props.error;
|
|
|
|
} else if (this.state.slow) {
|
|
|
|
message = "Still trying to reach Nylas…";
|
|
|
|
} else {
|
|
|
|
message = ' '
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classes}>
|
|
|
|
<div style={{flex: 1}} />
|
|
|
|
<RetinaImg
|
|
|
|
className="spinner"
|
|
|
|
style={{width: 20, height: 20}}
|
|
|
|
name="inline-loading-spinner.gif"
|
|
|
|
mode={RetinaImg.Mode.ContentPreserve}
|
|
|
|
/>
|
|
|
|
<div className="message">{message}</div>
|
|
|
|
<div className="btn try-again" onClick={this.props.onTryAgain}>Try Again</div>
|
|
|
|
<div style={{flex: 1}} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class AuthenticatePage extends React.Component {
|
|
|
|
static displayName = "AuthenticatePage";
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
accountInfo: React.PropTypes.object,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
ready: false,
|
|
|
|
error: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const webview = ReactDOM.findDOMNode(this.refs.webview);
|
2016-08-31 01:41:31 +08:00
|
|
|
const n1Version = NylasEnv.getVersion();
|
2016-09-29 02:08:34 +08:00
|
|
|
webview.partition = 'in-memory-only';
|
2016-08-31 01:41:31 +08:00
|
|
|
webview.src = `${IdentityStore.URLRoot}/onboarding?utm_medium=N1&utm_source=OnboardingPage&N1_version=${n1Version}`;
|
2016-05-28 05:05:27 +08:00
|
|
|
webview.addEventListener('did-start-loading', this.webviewDidStartLoading);
|
2016-06-04 07:35:00 +08:00
|
|
|
webview.addEventListener('did-get-response-details', this.webviewDidGetResponseDetails);
|
2016-05-28 05:05:27 +08:00
|
|
|
webview.addEventListener('did-fail-load', this.webviewDidFailLoad);
|
|
|
|
webview.addEventListener('did-finish-load', this.webviewDidFinishLoad);
|
|
|
|
webview.addEventListener('console-message', (e) => {
|
2016-08-30 10:07:44 +08:00
|
|
|
if (/^http.+/i.test(e.message)) { shell.openExternal(e.message) }
|
2016-05-28 05:05:27 +08:00
|
|
|
console.log('Guest page logged a message:', e.message);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onTryAgain = () => {
|
|
|
|
const webview = ReactDOM.findDOMNode(this.refs.webview);
|
|
|
|
webview.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
webviewDidStartLoading = () => {
|
|
|
|
this.setState({error: null, webviewLoading: true});
|
|
|
|
}
|
|
|
|
|
2016-06-04 07:35:00 +08:00
|
|
|
webviewDidGetResponseDetails = ({httpResponseCode, originalURL}) => {
|
|
|
|
if (httpResponseCode >= 400) {
|
|
|
|
const error = `
|
|
|
|
Could not reach Nylas to sign in. Please try again or contact
|
|
|
|
support@nylas.com if the issue persists.
|
|
|
|
(${originalURL}: ${httpResponseCode})
|
|
|
|
`;
|
|
|
|
this.setState({ready: false, error: error, webviewLoading: false});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
webviewDidFailLoad = ({errorCode, validatedURL}) => {
|
2016-05-28 05:05:27 +08:00
|
|
|
// "Operation was aborted" can be fired when we move between pages quickly.
|
|
|
|
if (errorCode === -3) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-04 07:35:00 +08:00
|
|
|
const e = networkErrors.createByCode(errorCode);
|
|
|
|
const error = `Could not reach ${validatedURL}. ${e ? e.message : errorCode}`;
|
2016-05-28 05:05:27 +08:00
|
|
|
this.setState({ready: false, error: error, webviewLoading: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
webviewDidFinishLoad = () => {
|
|
|
|
// this is sometimes called right after did-fail-load
|
2016-08-31 10:20:51 +08:00
|
|
|
if (this.state.error) return;
|
2016-05-28 05:05:27 +08:00
|
|
|
|
2016-08-31 10:20:51 +08:00
|
|
|
const webview = ReactDOM.findDOMNode(this.refs.webview);
|
|
|
|
|
|
|
|
const receiveUserInfo = `
|
2016-05-28 05:05:27 +08:00
|
|
|
var a = document.querySelector('#pro-account');
|
|
|
|
result = a ? a.innerText : null;
|
|
|
|
`;
|
2016-08-31 10:20:51 +08:00
|
|
|
webview.executeJavaScript(receiveUserInfo, false, (result) => {
|
2016-05-28 05:05:27 +08:00
|
|
|
this.setState({ready: true, webviewLoading: false});
|
|
|
|
if (result !== null) {
|
|
|
|
OnboardingActions.authenticationJSONReceived(JSON.parse(result));
|
|
|
|
}
|
|
|
|
});
|
2016-08-31 10:20:51 +08:00
|
|
|
|
|
|
|
const openExternalLink = `
|
|
|
|
var el = document.querySelector('.open-external');
|
|
|
|
if (el) {el.addEventListener('click', function(event) {console.log(this.href); event.preventDefault(); return false;})}
|
|
|
|
`;
|
|
|
|
webview.executeJavaScript(openExternalLink);
|
2016-05-28 05:05:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="page authenticate">
|
2016-11-04 08:42:20 +08:00
|
|
|
<webview ref="webview" is partition="in-memory-only" />
|
2016-05-28 05:05:27 +08:00
|
|
|
<div className={`webview-loading-spinner loading-${this.state.webviewLoading}`}>
|
|
|
|
<RetinaImg
|
|
|
|
style={{width: 20, height: 20}}
|
|
|
|
name="inline-loading-spinner.gif"
|
|
|
|
mode={RetinaImg.Mode.ContentPreserve}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<InitialLoadingCover
|
|
|
|
ready={this.state.ready}
|
|
|
|
error={this.state.error}
|
|
|
|
onTryAgain={this.onTryAgain}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|