2016-04-07 02:44:41 +08:00
|
|
|
import {
|
|
|
|
NylasAPI,
|
|
|
|
NylasSyncStatusStore,
|
|
|
|
React,
|
|
|
|
Actions,
|
|
|
|
} from 'nylas-exports';
|
2016-04-05 08:11:09 +08:00
|
|
|
import {RetinaImg} from 'nylas-component-kit';
|
|
|
|
|
|
|
|
export default class ConnectionStatusHeader extends React.Component {
|
|
|
|
static displayName = 'ConnectionStatusHeader';
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this._updateInterval = null;
|
|
|
|
this.state = this.getStateFromStores();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2016-05-06 13:30:34 +08:00
|
|
|
this.unsubscribe = NylasSyncStatusStore.listen(() => {
|
2016-04-05 08:11:09 +08:00
|
|
|
const nextState = this.getStateFromStores();
|
|
|
|
if ((nextState.connected !== this.state.connected) || (nextState.nextRetryText !== this.state.nextRetryText)) {
|
|
|
|
this.setState(nextState);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener('browser-window-focus', this.onWindowFocusChanged);
|
|
|
|
window.addEventListener('browser-window-blur', this.onWindowFocusChanged);
|
|
|
|
this.ensureCountdownInterval();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
|
|
|
this.ensureCountdownInterval();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('browser-window-focus', this.onWindowFocusChanged);
|
|
|
|
window.removeEventListener('browser-window-blur', this.onWindowFocusChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
onTryAgain = () => {
|
|
|
|
Actions.retrySync();
|
|
|
|
}
|
|
|
|
|
|
|
|
onWindowFocusChanged = () => {
|
|
|
|
this.setState(this.getStateFromStores());
|
|
|
|
this.ensureCountdownInterval();
|
|
|
|
}
|
|
|
|
|
|
|
|
getStateFromStores() {
|
2016-04-09 04:52:14 +08:00
|
|
|
const nextRetryDelay = NylasSyncStatusStore.nextRetryDelay();
|
2016-04-05 08:11:09 +08:00
|
|
|
const nextRetryTimestamp = NylasSyncStatusStore.nextRetryTimestamp();
|
2016-04-09 04:52:14 +08:00
|
|
|
let connected = NylasSyncStatusStore.connected();
|
|
|
|
|
|
|
|
if (nextRetryDelay < 5000) {
|
|
|
|
connected = true;
|
|
|
|
}
|
2016-04-05 08:11:09 +08:00
|
|
|
|
|
|
|
let nextRetryText = null;
|
|
|
|
if (!connected) {
|
|
|
|
if (document.body.classList.contains('is-blurred')) {
|
|
|
|
nextRetryText = 'soon';
|
|
|
|
} else {
|
|
|
|
const seconds = Math.ceil((nextRetryTimestamp - Date.now()) / 1000.0);
|
|
|
|
if (seconds > 1) {
|
|
|
|
nextRetryText = `in ${seconds} seconds`;
|
|
|
|
} else {
|
|
|
|
nextRetryText = `now`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {connected, nextRetryText};
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureCountdownInterval = () => {
|
|
|
|
if (this._updateInterval) {
|
|
|
|
clearInterval(this._updateInterval);
|
|
|
|
}
|
|
|
|
// only count down the "Reconnecting in..." label if the window is in the
|
|
|
|
// foreground to avoid the battery hit.
|
|
|
|
if (!this.state.connected && !document.body.classList.contains('is-blurred')) {
|
|
|
|
this._updateInterval = setInterval(() => {
|
|
|
|
this.setState(this.getStateFromStores());
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {connected, nextRetryText} = this.state;
|
|
|
|
|
|
|
|
if (connected) {
|
2016-05-07 07:06:16 +08:00
|
|
|
return (<span />);
|
2016-04-05 08:11:09 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 02:44:41 +08:00
|
|
|
const apiDomain = NylasAPI.APIRoot.split('//').pop();
|
|
|
|
|
2016-04-05 08:11:09 +08:00
|
|
|
return (
|
|
|
|
<div className="connection-status-header notifications-sticky">
|
|
|
|
<div className={"notifications-sticky-item notification-offline"}>
|
|
|
|
<RetinaImg
|
|
|
|
className="icon"
|
|
|
|
name="icon-alert-onred.png"
|
2016-05-07 07:06:16 +08:00
|
|
|
mode={RetinaImg.Mode.ContentPreserve}
|
|
|
|
/>
|
2016-04-05 08:11:09 +08:00
|
|
|
<div className="message">
|
2016-04-07 02:44:41 +08:00
|
|
|
Nylas N1 isn't able to reach {apiDomain}. Retrying {nextRetryText}.
|
2016-04-05 08:11:09 +08:00
|
|
|
</div>
|
|
|
|
<a className="action default" onClick={this.onTryAgain}>
|
|
|
|
Try Again Now
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|