summaryrefslogtreecommitdiffhomepage
path: root/app/app.js
blob: 5ff241089f7854633a790378c09f0587053d548b (plain)
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
// @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.setLocation(args.addr);
  backend.sync();
  if(store.getState().settings.autoSecure) {
    backend.autologin();
  }
});
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
// 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()
);