insomnia/app/ui/components/dropdowns/SyncDropdown.js

179 lines
5.4 KiB
JavaScript
Raw Normal View History

2016-11-11 23:06:24 +00:00
import React, {Component, PropTypes} from 'react';
import {Dropdown, DropdownDivider, DropdownItem, DropdownButton} from '../base/dropdown';
import {showModal} from '../modals';
2016-11-11 23:06:24 +00:00
import SyncLogsModal from '../modals/SyncLogsModal';
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';
import WorkspaceShareSettingsModal from '../modals/WorkspaceShareSettingsModal';
2016-11-11 23:06:24 +00:00
class SyncDropdown extends Component {
2016-11-26 08:29:16 +00:00
state = {
loggedIn: null,
syncData: null,
loading: false,
};
2016-11-11 23:06:24 +00:00
_trackShowMenu = () => trackEvent('Sync', 'Show Menu', 'Authenticated');
_handleShowLogs = () => showModal(SyncLogsModal);
_handleShowShareSettings = () => {
showModal(WorkspaceShareSettingsModal, {workspace: this.props.workspace});
};
_handleToggleSyncMode = async () => {
const {syncData} = this.state;
const resourceGroupId = syncData.resourceGroupId;
const config = await sync.getOrCreateConfig(resourceGroupId);
let syncMode = config.syncMode === syncStorage.SYNC_MODE_OFF ?
syncStorage.SYNC_MODE_ON : syncStorage.SYNC_MODE_OFF;
await sync.createOrUpdateConfig(resourceGroupId, syncMode);
await this._reloadData();
// Trigger a sync right away if we're turning it on
if (syncMode === syncStorage.SYNC_MODE_ON) {
await this._handleSyncResourceGroupId();
}
2016-11-17 23:22:23 +00:00
trackEvent('Sync', 'Change Mode', syncMode);
};
_handleSyncResourceGroupId = async () => {
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);
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');
};
2016-11-11 23:06:24 +00:00
async _reloadData () {
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
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
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,
name: workspace.name,
};
this.setState({syncData});
2016-11-11 23:06:24 +00:00
}
componentWillMount () {
this._interval = setInterval(() => this._reloadData(), 2000);
this._reloadData();
}
componentWillUnmount () {
clearInterval(this._interval);
}
_getSyncDescription (syncMode, syncPercentage) {
if (syncPercentage === 100) {
return 'Up To Date'
} else {
2016-11-12 01:01:54 +00:00
return syncMode === syncStorage.SYNC_MODE_ON ? 'Sync Pending' : 'Sync Required'
}
}
2016-11-11 23:06:24 +00:00
render () {
const {className, workspace} = this.props;
const {syncData, loading, loggedIn} = this.state;
// Don't show the sync menu unless we're logged in
if (!loggedIn) {
return null;
}
if (!syncData) {
return (
<div className={className}>
<button className="btn btn--compact wide" disabled={true}>
Initializing Sync...
</button>
</div>
)
} else {
const {syncMode, syncPercent} = syncData;
const description = this._getSyncDescription(syncMode, syncPercent);
const isPaused = syncMode === syncStorage.SYNC_MODE_OFF;
2016-11-11 23:06:24 +00:00
return (
<div className={className}>
<Dropdown wide={true} className="wide tall">
<DropdownButton className="btn btn--compact wide" onClick={this._trackShowMenu}>
{isPaused ? <span><i className="fa fa-pause-circle"/>&nbsp;</span> : null}
{description}
</DropdownButton>
<DropdownDivider>Workspace Synced {syncPercent}%</DropdownDivider>
<DropdownItem onClick={this._handleToggleSyncMode}
stayOpenAfterClick={true}>
{syncMode === syncStorage.SYNC_MODE_OFF ?
<i className="fa fa-toggle-off"></i> :
<i className="fa fa-toggle-on"></i>}
Automatic Sync
</DropdownItem>
2017-01-11 20:58:37 +00:00
<DropdownItem onClick={this._handleSyncResourceGroupId} stayOpenAfterClick={true}>
2016-11-12 08:38:55 +00:00
{loading ?
<i className="fa fa-refresh fa-spin"></i> :
<i className="fa fa-cloud-upload"></i>}
2017-01-11 20:58:37 +00:00
Sync Now
2016-11-12 08:38:55 +00:00
</DropdownItem>
<DropdownDivider>Other</DropdownDivider>
<DropdownItem onClick={this._handleShowShareSettings}>
<i className="fa fa-users"></i>
2017-01-11 18:35:44 +00:00
Configure Sharing
</DropdownItem>
<DropdownItem onClick={this._handleShowLogs}>
<i className="fa fa-bug"></i>
Show Debug Logs
</DropdownItem>
</Dropdown>
</div>
2016-11-11 23:06:24 +00:00
);
}
}
}
SyncDropdown.propTypes = {
workspace: PropTypes.object.isRequired
2016-11-11 23:06:24 +00:00
};
export default SyncDropdown;