1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
// @flow
import path from 'path';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { ConnectedRouter } from 'react-router-redux';
import { createMemoryHistory } from 'history';
import { webFrame, ipcRenderer } from 'electron';
import log from 'electron-log';
import makeRoutes from './routes';
import configureStore from './redux/store';
import accountActions from './redux/account/actions';
import connectionActions from './redux/connection/actions';
import { Backend } from './lib/backend';
import mapBackendEventsToReduxActions from './lib/backend-redux-actions';
import mapBackendEventsToRouter from './lib/backend-routing';
import type { LoginState } from './redux/account/reducers';
import type { ConnectionState } from './redux/connection/reducers';
import type { TrayIconType } from './lib/tray-icon-manager';
const initialState = null;
const memoryHistory = createMemoryHistory();
const store = configureStore(initialState, memoryHistory);
const backend = new Backend(store);
// reset login state if user quit the app during login
if((['connecting', 'failed']: Array<LoginState>).includes(store.getState().account.status)) {
store.dispatch(accountActions.loginChange({
status: 'none'
}));
}
// reset connection state if user quit the app when connecting
if(store.getState().connection.status === 'connecting') {
store.dispatch(connectionActions.connectionChange({
status: 'disconnected'
}));
}
// Tray icon
/**
* Get tray icon type based on connection state
*/
const getIconType = (s: ConnectionState): TrayIconType => {
switch(s) {
case 'connected': return 'secured';
case 'connecting': return 'securing';
default: return 'unsecured';
}
};
/**
* Update tray icon via IPC call
*/
const updateTrayIcon = () => {
const { connection } = store.getState();
ipcRenderer.send('changeTrayIcon', getIconType(connection.status));
};
// Setup primary event handlers to translate backend events into redux dispatch
mapBackendEventsToReduxActions(backend, store);
// Setup routing based on backend events
mapBackendEventsToRouter(backend, store);
ipcRenderer.on('backend-info', (_event, args) => {
backend.setLocation(args.addr);
backend.sync();
});
// Setup events to update tray icon
backend.on('connect', updateTrayIcon);
backend.on('connecting', updateTrayIcon);
backend.on('disconnect', updateTrayIcon);
// force update tray
updateTrayIcon();
// disable smart pinch.
webFrame.setZoomLevelLimits(1, 1);
if(navigator.serviceWorker) {
navigator.serviceWorker.register(path.join(__dirname, 'tilecache.sw.js'))
.then((registration) => {
log.info('ServiceWorker registration successful with scope: ', registration.scope);
}).catch((err) => {
log.info('ServiceWorker registration failed: ', err);
});
}
ipcRenderer.send('on-browser-window-ready');
function getRootElement() {
const currentScript = document.currentScript;
if (!currentScript) {
throw new Error('Missing document.currentScript');
}
const containerId = currentScript.getAttribute('data-container');
if(!containerId) {
throw new Error('Missing data-container attribute.');
}
const rootElement = document.querySelector(containerId);
if(!rootElement) {
throw new Error('Missing root element.');
}
return rootElement;
}
ReactDOM.render(
<Provider store={ store }>
<ConnectedRouter history={ memoryHistory }>
{ makeRoutes(store.getState, { backend }) }
</ConnectedRouter>
</Provider>,
getRootElement()
);
|