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
123
124
125
|
// @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 { Backend } from './lib/backend';
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);
//////////////////////////////////////////////////////////////////////////
// Backend
//////////////////////////////////////////////////////////////////////////
const backend = new Backend(store);
ipcRenderer.on('backend-info', (_event, args) => {
backend.setCredentials(args.credentials);
backend.sync();
backend.autologin()
.then( () => {
return backend.syncRelaySettings();
})
.then( () => {
const { settings } = store.getState();
return backend.connect(settings.relayConstraints.host);
})
.catch( e => {
if (e.type === 'NO_ACCOUNT') {
log.debug('No user set in the backend, showing window');
ipcRenderer.send('show-window');
}
});
});
ipcRenderer.on('shutdown', () => {
log.info('Been told by the node process to shutdown');
backend.shutdown()
.catch( e => {
log.warn('Unable to shut down the backend', e.message);
});
});
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// 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();
// TODO: Only update the tray icon if the connection status changed
ipcRenderer.send('changeTrayIcon', getIconType(connection.status));
};
store.subscribe(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()
);
|