import * as React from 'react'; import { Switch, Route, Redirect } from 'react-router'; import { JournalList } from './JournalList'; import { JournalView } from './JournalView'; import * as EteSync from './api/EteSync'; import { routeResolver } from './App'; import * as C from './Constants'; const CONTEXT_SESSION_KEY = 'EteSyncContext'; enum LoadState { Initial = 'INIT', Working = 'WORKING', Done = 'DONE', } export interface EteSyncContextType { serviceApiUrl: string; credentials: EteSync.Credentials; encryptionKey: string; } export class EteSyncContext extends React.Component { username: HTMLInputElement; password: HTMLInputElement; encryptionPassword: HTMLInputElement; state: { context?: EteSyncContextType; loadState: LoadState; error?: Error; }; constructor(props: any) { super(props); this.state = {loadState: LoadState.Initial}; this.generateEncryption = this.generateEncryption.bind(this); const contextStr = sessionStorage.getItem(CONTEXT_SESSION_KEY); if (contextStr !== null) { const context: EteSyncContextType = JSON.parse(contextStr); this.state = { loadState: LoadState.Done, context }; } } generateEncryption(e: any) { e.preventDefault(); let authenticator = new EteSync.Authenticator(C.serviceApiBase); this.setState({ loadState: LoadState.Working }); const username = this.username.value; const password = this.password.value; const encryptionPassword = this.encryptionPassword.value; authenticator.getAuthToken(username, password).then((authToken) => { const credentials = new EteSync.Credentials(username, authToken); const derived = EteSync.deriveKey(username, encryptionPassword); const context = { serviceApiUrl: C.serviceApiBase, credentials, encryptionKey: derived, }; sessionStorage.setItem(CONTEXT_SESSION_KEY, JSON.stringify(context)); this.setState({ loadState: LoadState.Done, context }); }).catch((error) => { this.setState({ loadState: LoadState.Initial, error }); }); } render() { if (this.state.loadState === LoadState.Initial) { return (