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

213 lines
6.5 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 SettingsModal, {TAB_PLUS} from '../modals/SettingsModal';
import LoginModal from '../modals/LoginModal';
import PromptButton from '../base/PromptButton';
2016-11-11 23:06:24 +00:00
class SyncDropdown extends Component {
constructor (props) {
super(props);
this.state = {
loggedIn: null,
2016-11-11 23:06:24 +00:00
syncData: null,
loading: false,
hide: false,
2016-11-11 23:06:24 +00:00
}
}
_handleHideMenu () {
this.setState({hide: true});
2016-11-17 23:22:23 +00:00
trackEvent('Sync', 'Hide Menu')
}
async _handleToggleSyncMode (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(resourceGroupId);
}
2016-11-17 23:22:23 +00:00
trackEvent('Sync', 'Change Mode', syncMode);
}
2016-11-11 23:06:24 +00:00
async _handleSyncResourceGroupId (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.pushActiveDirtyResources(resourceGroupId);
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} = this.props;
const {syncData, loading, loggedIn, hide} = this.state;
if (hide) {
return null;
}
if (!loggedIn) {
return (
<div className={className}>
<Dropdown wide={true} className="wide tall">
<DropdownButton className="btn btn--compact wide"
onClick={e => trackEvent('Sync', 'Show Menu', 'Guest')}>
Sync Settings
</DropdownButton>
<DropdownDivider name="Insomnia Cloud Sync"/>
2016-11-17 23:22:23 +00:00
<DropdownItem onClick={e => {
showModal(SettingsModal, TAB_PLUS);
trackEvent('Sync', 'Create Account');
}}>
<i className="fa fa-user"></i>
Create Account
</DropdownItem>
2016-11-17 23:22:23 +00:00
<DropdownItem onClick={e => {
showModal(LoginModal);
trackEvent('Sync', 'Login');
}}>
<i className="fa fa-empty"></i>
Login
</DropdownItem>
<DropdownDivider/>
<DropdownItem buttonClass={PromptButton}
addIcon={true}
onClick={e => this._handleHideMenu()}>
<i className="fa fa-eye-slash"></i>
Hide This Menu
</DropdownItem>
</Dropdown>
</div>
)
}
if (!syncData) {
return (
<div className={className}>
<button className="btn btn--compact wide" disabled={true}>
Initializing Sync...
</button>
</div>
)
} else {
2016-11-12 01:04:08 +00:00
const {resourceGroupId, syncMode, syncPercent} = syncData;
const description = this._getSyncDescription(syncMode, syncPercent);
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={e => trackEvent('Sync', 'Show Menu', 'Authenticated')}>
{description}
</DropdownButton>
<DropdownDivider name={`Workspace Synced ${syncPercent}%`}/>
<DropdownItem onClick={e => this._handleToggleSyncMode(resourceGroupId)}
stayOpenAfterClick={true}>
{syncMode === syncStorage.SYNC_MODE_OFF ?
<i className="fa fa-toggle-off"></i> :
<i className="fa fa-toggle-on"></i>}
Automatic Sync
</DropdownItem>
2016-11-12 08:38:55 +00:00
<DropdownItem onClick={e => this._handleSyncResourceGroupId(resourceGroupId)}
disabled={syncPercent === 100}
stayOpenAfterClick={true}>
{loading ?
<i className="fa fa-refresh fa-spin"></i> :
<i className="fa fa-cloud-upload"></i>}
Sync Now {syncPercent === 100 ? '(up to date)' : ''}
</DropdownItem>
<DropdownDivider name="Other"/>
<DropdownItem onClick={e => showModal(SettingsModal, TAB_PLUS)}>
<i className="fa fa-user"></i>
Manage Account
</DropdownItem>
<DropdownItem onClick={e => showModal(SyncLogsModal)}>
<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;