2017-02-28 21:32:23 +00:00
|
|
|
import React, {PureComponent, PropTypes} from 'react';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2016-11-11 23:06:24 +00:00
|
|
|
import {Dropdown, DropdownDivider, DropdownItem, DropdownButton} from '../base/dropdown';
|
2016-11-16 17:18:39 +00:00
|
|
|
import {showModal} from '../modals';
|
2016-11-11 23:06:24 +00:00
|
|
|
import * as syncStorage from '../../../sync/storage';
|
|
|
|
import * as session from '../../../sync/session';
|
|
|
|
import * as sync from '../../../sync';
|
2016-11-17 23:22:23 +00:00
|
|
|
import {trackEvent} from '../../../analytics';
|
2017-03-08 05:52:17 +00:00
|
|
|
import WorkspaceShareSettingsModal from '../modals/workspace-share-settings-modal';
|
|
|
|
import SetupSyncModal from '../modals/setup-sync-modal';
|
2016-11-11 23:06:24 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class SyncDropdown extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this._hasPrompted = false;
|
2017-07-27 22:59:07 +00:00
|
|
|
this._isMounted = false;
|
2016-11-11 23:06:24 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
this.state = {
|
|
|
|
loggedIn: null,
|
|
|
|
syncData: null,
|
2017-03-03 20:09:08 +00:00
|
|
|
loading: false
|
2017-03-03 01:44:07 +00:00
|
|
|
};
|
|
|
|
}
|
2016-12-21 23:37:48 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_trackShowMenu () {
|
|
|
|
trackEvent('Sync', 'Show Menu', 'Authenticated');
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleShowShareSettings () {
|
2017-01-11 03:18:15 +00:00
|
|
|
showModal(WorkspaceShareSettingsModal, {workspace: this.props.workspace});
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2017-01-11 03:18:15 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
async _handleSyncResourceGroupId () {
|
2016-12-21 23:37:48 +00:00
|
|
|
const {syncData} = this.state;
|
|
|
|
const resourceGroupId = syncData.resourceGroupId;
|
2016-11-11 23:06:24 +00:00
|
|
|
|
|
|
|
// Set loading state
|
|
|
|
this.setState({loading: true});
|
|
|
|
|
|
|
|
await sync.getOrCreateConfig(resourceGroupId);
|
|
|
|
await sync.pull(resourceGroupId);
|
2017-01-11 03:18:15 +00:00
|
|
|
await sync.push(resourceGroupId);
|
2016-11-11 23:06:24 +00:00
|
|
|
|
|
|
|
await this._reloadData();
|
|
|
|
|
|
|
|
// Unset loading state
|
|
|
|
this.setState({loading: false});
|
|
|
|
|
2016-11-17 23:22:23 +00:00
|
|
|
trackEvent('Sync', 'Manual Sync');
|
2017-03-03 01:44:07 +00:00
|
|
|
}
|
2016-11-11 23:06:24 +00:00
|
|
|
|
|
|
|
async _reloadData () {
|
2016-11-16 17:18:39 +00:00
|
|
|
const loggedIn = session.isLoggedIn();
|
|
|
|
|
|
|
|
if (loggedIn !== this.state.loggedIn) {
|
|
|
|
this.setState({loggedIn});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!loggedIn) {
|
2016-11-12 08:38:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-11 23:06:24 +00:00
|
|
|
// Get or create any related sync data
|
2016-11-16 17:18:39 +00:00
|
|
|
const {workspace} = this.props;
|
2016-11-12 01:04:08 +00:00
|
|
|
const {resourceGroupId} = await sync.getOrCreateResourceForDoc(workspace);
|
2016-11-11 23:06:24 +00:00
|
|
|
const config = await sync.getOrCreateConfig(resourceGroupId);
|
|
|
|
|
|
|
|
// Analyze it
|
2016-11-16 17:18:39 +00:00
|
|
|
const dirty = await syncStorage.findDirtyResourcesForResourceGroup(resourceGroupId);
|
2016-11-11 23:06:24 +00:00
|
|
|
const all = await syncStorage.findResourcesForResourceGroup(resourceGroupId);
|
|
|
|
const numClean = all.length - dirty.length;
|
|
|
|
const syncPercent = all.length === 0 ? 100 : parseInt(numClean / all.length * 1000) / 10;
|
|
|
|
|
|
|
|
const syncData = {
|
2016-11-12 01:04:08 +00:00
|
|
|
resourceGroupId,
|
2016-11-11 23:06:24 +00:00
|
|
|
syncPercent,
|
|
|
|
syncMode: config.syncMode,
|
2017-03-03 20:09:08 +00:00
|
|
|
name: workspace.name
|
2016-11-11 23:06:24 +00:00
|
|
|
};
|
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
if (this._isMounted) {
|
|
|
|
this.setState({syncData});
|
|
|
|
}
|
2016-11-11 23:06:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
async _handleShowSyncModePrompt () {
|
|
|
|
await showModal(SetupSyncModal);
|
|
|
|
await this._reloadData();
|
2017-03-03 20:09:08 +00:00
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
2017-03-06 17:51:58 +00:00
|
|
|
async componentWillMount () {
|
2017-03-31 21:59:12 +00:00
|
|
|
this._interval = setInterval(this._reloadData, 500);
|
2017-03-06 17:51:58 +00:00
|
|
|
await this._reloadData();
|
2016-11-11 23:06:24 +00:00
|
|
|
}
|
|
|
|
|
2017-07-27 22:59:07 +00:00
|
|
|
componentDidMount () {
|
|
|
|
this._isMounted = true;
|
|
|
|
}
|
|
|
|
|
2016-11-11 23:06:24 +00:00
|
|
|
componentWillUnmount () {
|
|
|
|
clearInterval(this._interval);
|
2017-07-27 22:59:07 +00:00
|
|
|
this._isMounted = false;
|
2016-11-11 23:06:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
async componentDidUpdate () {
|
|
|
|
const {syncData} = this.state;
|
|
|
|
|
|
|
|
if (!syncData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync has not yet been configured for this workspace, so prompt the user to do so
|
2017-07-03 18:30:20 +00:00
|
|
|
const isModeUnset = !syncData.syncMode || syncData.syncMode === syncStorage.SYNC_MODE_UNSET;
|
2017-03-03 01:44:07 +00:00
|
|
|
if (isModeUnset && !this._hasPrompted) {
|
|
|
|
this._hasPrompted = true;
|
|
|
|
await this._handleShowSyncModePrompt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 00:58:23 +00:00
|
|
|
_getSyncDescription (syncMode, syncPercentage) {
|
2017-03-03 01:44:07 +00:00
|
|
|
let el = null;
|
|
|
|
if (syncMode === syncStorage.SYNC_MODE_NEVER) {
|
2017-03-03 20:09:08 +00:00
|
|
|
el = <span>Sync Disabled</span>;
|
2017-03-03 01:44:07 +00:00
|
|
|
} else if (syncPercentage === 100) {
|
2017-03-03 20:09:08 +00:00
|
|
|
el = <span>Sync Up To Date</span>;
|
2017-03-03 01:44:07 +00:00
|
|
|
} else if (syncMode === syncStorage.SYNC_MODE_OFF) {
|
2017-03-03 20:09:08 +00:00
|
|
|
el = <span><i className="fa fa-pause-circle-o"/> Sync Required</span>;
|
2017-03-03 01:44:07 +00:00
|
|
|
} else if (syncMode === syncStorage.SYNC_MODE_ON) {
|
2017-03-03 20:09:08 +00:00
|
|
|
el = <span>Sync Pending</span>;
|
2017-07-03 18:30:20 +00:00
|
|
|
} else if (!syncMode || syncMode === syncStorage.SYNC_MODE_UNSET) {
|
2017-03-03 20:09:08 +00:00
|
|
|
el = <span><i className="fa fa-exclamation-circle"/> Configure Sync</span>;
|
2016-11-12 00:58:23 +00:00
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
|
|
|
return el;
|
2016-11-12 00:58:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 23:06:24 +00:00
|
|
|
render () {
|
2017-03-03 01:44:07 +00:00
|
|
|
const {className} = this.props;
|
2016-12-21 23:37:48 +00:00
|
|
|
const {syncData, loading, loggedIn} = this.state;
|
2016-11-16 17:18:39 +00:00
|
|
|
|
2016-12-21 23:37:48 +00:00
|
|
|
// Don't show the sync menu unless we're logged in
|
2016-11-16 17:18:39 +00:00
|
|
|
if (!loggedIn) {
|
2016-12-21 23:37:48 +00:00
|
|
|
return null;
|
2016-11-16 17:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!syncData) {
|
|
|
|
return (
|
|
|
|
<div className={className}>
|
2017-03-01 21:15:56 +00:00
|
|
|
<button className="btn btn--compact wide" disabled>
|
2016-11-16 17:18:39 +00:00
|
|
|
Initializing Sync...
|
|
|
|
</button>
|
|
|
|
</div>
|
2017-03-03 20:09:08 +00:00
|
|
|
);
|
2016-11-16 17:18:39 +00:00
|
|
|
} else {
|
2016-12-21 23:37:48 +00:00
|
|
|
const {syncMode, syncPercent} = syncData;
|
2016-11-11 23:06:24 +00:00
|
|
|
return (
|
2016-11-16 17:18:39 +00:00
|
|
|
<div className={className}>
|
2017-03-01 21:15:56 +00:00
|
|
|
<Dropdown wide className="wide tall">
|
2016-12-21 23:37:48 +00:00
|
|
|
<DropdownButton className="btn btn--compact wide" onClick={this._trackShowMenu}>
|
2017-03-03 01:44:07 +00:00
|
|
|
{this._getSyncDescription(syncMode, syncPercent)}
|
2016-11-16 17:18:39 +00:00
|
|
|
</DropdownButton>
|
2016-12-21 23:37:48 +00:00
|
|
|
<DropdownDivider>Workspace Synced {syncPercent}%</DropdownDivider>
|
2017-03-03 01:44:07 +00:00
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
<DropdownItem onClick={this._handleShowSyncModePrompt}>
|
|
|
|
<i className="fa fa-wrench"/>
|
|
|
|
Change Sync Mode
|
|
|
|
</DropdownItem>
|
2017-03-03 01:44:07 +00:00
|
|
|
|
|
|
|
{/* SYNCED */}
|
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
{syncMode !== syncStorage.SYNC_MODE_NEVER
|
|
|
|
? <DropdownItem onClick={this._handleSyncResourceGroupId} stayOpenAfterClick>
|
|
|
|
{loading
|
|
|
|
? <i className="fa fa-refresh fa-spin"/>
|
2017-03-06 17:51:58 +00:00
|
|
|
: <i className="fa fa-cloud-upload"/>}
|
2017-03-03 01:44:07 +00:00
|
|
|
Sync Now
|
|
|
|
</DropdownItem> : null
|
|
|
|
}
|
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
{syncMode !== syncStorage.SYNC_MODE_NEVER
|
|
|
|
? <DropdownItem onClick={this._handleShowShareSettings}>
|
2017-07-25 22:28:53 +00:00
|
|
|
<i className="fa fa-users"/>
|
2017-03-06 17:51:58 +00:00
|
|
|
Share Settings
|
2017-03-03 01:44:07 +00:00
|
|
|
</DropdownItem> : null
|
|
|
|
}
|
2016-11-16 17:18:39 +00:00
|
|
|
</Dropdown>
|
|
|
|
</div>
|
2016-11-11 23:06:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncDropdown.propTypes = {
|
2017-03-08 05:52:17 +00:00
|
|
|
// Required
|
|
|
|
workspace: PropTypes.object.isRequired,
|
|
|
|
|
|
|
|
// Optional
|
|
|
|
className: PropTypes.string
|
2016-11-11 23:06:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default SyncDropdown;
|