import React, {Component} from 'react'; import classnames from 'classnames'; import GravatarImg from '../GravatarImg'; import Modal from '../base/Modal'; import PromptButton from '../base/PromptButton'; import ModalBody from '../base/ModalBody'; import ModalHeader from '../base/ModalHeader'; import ModalFooter from '../base/ModalFooter'; import {Tab, Tabs, TabList, TabPanel} from 'react-tabs'; import * as session from '../../../backend/sync/session'; import * as syncStorage from '../../../backend/sync/storage'; import * as sync from '../../../backend/sync'; import * as db from '../../../backend/database'; class SyncModal extends Component { constructor (props) { super(props); this.state = { firstName: 'n/a', email: 'n/a', sessionId: 'n/a', dirty: [], syncData: [], pushingResourceGroups: {}, pullingResourceGroups: {}, } } async _handlePushResourceGroupId (resourceGroupId) { // Set loading state const pushingResourceGroups = Object.assign({}, this.state.pushingResourceGroups); pushingResourceGroups[resourceGroupId] = true; this.setState({pushingResourceGroups}); await sync.getOrCreateConfig(resourceGroupId); await sync.push(resourceGroupId); // Unset loading state delete pushingResourceGroups[resourceGroupId]; this.setState({pushingResourceGroups}); await this._updateModal(); } async _handlePullResourceGroupId (resourceGroupId) { // Set loading state const pullingResourceGroups = Object.assign({}, this.state.pushingResourceGroups); pullingResourceGroups[resourceGroupId] = true; this.setState({pullingResourceGroups}); await sync.getOrCreateConfig(resourceGroupId); await sync.pull(resourceGroupId); // Unset loading state delete pullingResourceGroups[resourceGroupId]; this.setState({pullingResourceGroups}); await this._updateModal(); } async _handleSyncModeChange (syncData, e) { const syncMode = e.target.value; const {resourceGroupId} = syncData.resource; await sync.createOrUpdateConfig(resourceGroupId, syncMode); // Refresh the modal this._updateModal(); // Trigger a sync cycle right away await sync.triggerSync(); } async _handleReset () { for (const r of await syncStorage.allResources()) { await syncStorage.removeResource(r); } for (const c of await syncStorage.allConfigs()) { await syncStorage.removeConfig(c); } await session.logout(); this.hide(); } _handleLogout () { session.logout(); this.hide(); } async _updateModal () { const workspaces = await db.workspace.all(); const syncData = []; for (const workspace of workspaces) { // Get or create any related sync data const resource = await sync.getOrCreateResourceForDoc(workspace); const resourceGroupId = resource.resourceGroupId; const config = await sync.getOrCreateConfig(resourceGroupId); // Analyze it const dirty = await syncStorage.findActiveDirtyResourcesForResourceGroup(resourceGroupId); const all = await syncStorage.findResourcesForResourceGroup(resourceGroupId); const numClean = all.length - dirty.length; let syncPercent; if (all.length === 0) { syncPercent = 100; } else { syncPercent = parseInt(numClean / all.length * 1000) / 10 } syncData.push({ resource, syncPercent, syncMode: config.syncMode, name: workspace.name, }); } this.setState({ syncData, email: session.getEmail(), firstName: session.getFirstName(), sessionId: session.getCurrentSessionId(), }); } async show () { if (!session.isLoggedIn()) { console.error('Not logged in'); return; } this.modal.show(); clearInterval(this._interval); await this._updateModal(); this._interval = setInterval(() => this._updateModal(), 2000); } hide () { clearInterval(this._interval); this.modal.hide(); } render () { const s = this.state; const data = [ ['Email', s.email], ['Session', s.sessionId], ['Full Sync', `${sync.FULL_SYNC_INTERVAL / 1000} second interval`], ['Partial Sync', `${sync.PUSH_DEBOUNCE_TIME / 1000} seconds after change`], ]; const colors = { debug: 'info', warn: 'warning', error: 'danger' }; // Show last N logs const allLogs = sync.logger.tail(); const logs = allLogs.slice(allLogs.length - 500); return ( this.modal = m} tall={true} wide={true}> Sync Settings {" "} ({s.email})

Hi {this.state.firstName}!

You are currently signed in with {" "} {this.state.email}


{this.state.syncData.map(data => { const {syncMode, name, syncPercent, resource} = data; const {resourceGroupId} = resource; return ( ) } )}
Workspace Sync Synced Team
{name}    {" "} {syncPercent}%

Here is some useful debug info in case you need to report a bug.

{data.map(([label, value]) => ( ))}
{label} {value}
{logs.map((entry, i) => { function pad (n, length) { let s = n + ''; while (s.length < length) { s = '0' + s; } return s; } const dateString = pad(entry.date.getFullYear(), 4) + '/' + pad(entry.date.getMonth(), 2) + '/' + pad(entry.date.getDay(), 2) + ' ' + pad(entry.date.getHours(), 2) + ':' + pad(entry.date.getMinutes(), 2) + ':' + pad(entry.date.getSeconds(), 2); return (
                  {dateString}
                      {" "}
                      
                  [{entry.type}]
                  
                      {" "}
                      {entry.message}
                  
) })}
this._handleLogout()}> Logout this._handleReset()} confirmMessage="Delete all sync-related data?"> Reset Sync Account
) } } SyncModal.propTypes = {}; export default SyncModal;