/* eslint react/react-in-jsx-scope: 0*/
const React = window.React;
const ReactDOM = window.ReactDOM;
const {
SyncPolicy,
SetAllSyncPolicies,
AccountFilter,
SyncGraph,
SyncbackRequestDetails,
} = window;
class Account extends React.Component {
renderError() {
const {account} = this.props;
if (account.sync_error != null) {
const {message, stack} = account.sync_error
const error = {
message,
stack: stack.slice(0, 4),
}
return (
Error
{JSON.stringify(error, null, 2)}
)
}
return
}
render() {
const {account, assignment, active} = this.props;
const errorClass = account.sync_error ? ' errored' : ''
const numStoredSyncs = account.last_sync_completions.length;
const oldestSync = account.last_sync_completions[numStoredSyncs - 1];
const newestSync = account.last_sync_completions[0];
const avgBetweenSyncs = (newestSync - oldestSync) / (1000 * numStoredSyncs);
const timeSinceLastSync = (Date.now() - newestSync) / 1000;
let firstSyncDuration = "Incomplete";
if (account.first_sync_completion) {
firstSyncDuration = (new Date(account.first_sync_completion) - new Date(account.created_at)) / 1000;
}
return (
{account.email_address} {active ? '🌕' : '🌑'}
{assignment}
Sync Cycles
First Sync Duration (seconds):
{firstSyncDuration}
Average Time Between Syncs (seconds):
{avgBetweenSyncs}
Time Since Last Sync (seconds):
{timeSinceLastSync}
Recent Syncs:
{this.renderError()}
);
}
}
Account.propTypes = {
account: React.PropTypes.object,
active: React.PropTypes.bool,
assignment: React.PropTypes.string,
}
class Root extends React.Component {
constructor() {
super();
this.state = {
accounts: {},
assignments: {},
activeAccountIds: [],
visibleAccounts: AccountFilter.states.all,
};
}
componentDidMount() {
let url = null;
if (window.location.protocol === "https:") {
url = `wss://${window.location.host}/accounts`;
} else {
url = `ws://${window.location.host}/accounts`;
}
this.websocket = new WebSocket(url);
this.websocket.onopen = () => {
this.websocket.send("Message to send");
};
this.websocket.onmessage = (evt) => {
try {
const msg = JSON.parse(evt.data);
if (msg.cmd === 'ACCOUNT') {
this.onReceivedAccount(msg.payload);
}
if (msg.cmd === 'ASSIGNMENTS') {
this.onReceivedAssignments(msg.payload);
}
if (msg.cmd === 'ACTIVE') {
this.onReceivedActiveAccountIds(msg.payload);
}
} catch (err) {
console.error(err);
}
};
this.websocket.onclose = () => {
window.location.reload();
};
}
onReceivedAssignments(assignments) {
this.setState({assignments})
}
onReceivedActiveAccountIds(accountIds) {
this.setState({activeAccountIds: accountIds})
}
onReceivedAccount(account) {
const accounts = Object.assign({}, this.state.accounts);
accounts[account.id] = account;
this.setState({accounts});
}
onFilter() {
this.setState({visibleAccounts: document.getElementById('account-filter').value});
}
render() {
let ids = Object.keys(this.state.accounts);
switch (this.state.visibleAccounts) {
case AccountFilter.states.errored:
ids = ids.filter((id) => this.state.accounts[id].sync_error)
break;
case AccountFilter.states.notErrored:
ids = ids.filter((id) => !this.state.accounts[id].sync_error)
break;
default:
break;
}
return (
this.onFilter.call(this)} />
parseInt(id, 10))} />
{
ids.sort((a, b) => a.localeCompare(b)).map((id) =>
)
}
)
}
}
ReactDOM.render(
,
document.getElementById('root')
);