-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
103 lines (92 loc) · 2.47 KB
/
main.js
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
// Libraries
const menubar = require('menubar');
const { Menu, shell, ipcMain } = require('electron');
const path = require('path');
const url = require('url');
const axios = require('axios');
// Keys
const apiKeys = require('./keys');
// ------- DEV SETUP --------- //
let dev = false,
indexPath = '';
if (
process.defaultApp ||
/[\\/]electron-prebuilt[\\/]/.test(process.execPath) ||
/[\\/]electron[\\/]/.test(process.execPath)
) {
dev = true;
}
if (dev && process.argv.indexOf('--noDevServer') === -1) {
indexPath = url.format({
protocol: 'http:',
host: 'localhost:8080',
pathname: 'index.html',
slashes: true
});
} else {
indexPath = url.format({
protocol: 'file:',
pathname: path.join(__dirname, 'dist', 'index.html'),
slashes: true
});
}
// ------- MENUBAR CONFIGURATION --------- //
const mb = menubar({
tooltip: 'CryptoCap',
icon: path.join(__dirname, 'assets', 'icons', 'png', 'iconTemplate.png'),
index: indexPath,
alwaysOnTop: dev,
width: 820,
height: 405,
preloadWindow: true
});
mb.on('ready', function() {
// Never highlight tray icon on OS X
this.tray.setHighlightMode('never');
// Allow right click context menu
const contextMenu = Menu.buildFromTemplate([
{
label: 'Learn More',
click: () => {
shell.openExternal('https://curtisrodgers.com/CryptoCap');
}
},
{ label: 'Quit', role: 'quit' }
]);
// attach context menu to right-click event
this.tray.on('right-click', () => {
this.tray.popUpContextMenu(contextMenu);
});
});
// Open dev tools if needed
mb.on('after-create-window', () => {
if (dev) {
mb.window.openDevTools();
}
});
// ------- MAIN EVENTS --------- //
// Get Global Market Data
let lastGlobalResponse = null;
let lastGlobalResponseDate = null;
ipcMain.on('api-get-global', event => {
const today = new Date().getDate();
if (today === lastGlobalResponseDate && lastGlobalResponse) {
// Serve from cache
event.sender.send('api-get-global-response', lastGlobalResponse);
} else {
// If first attempt or it's been more than a day, make request
axios({
method: 'get',
url: `https://pro-api.coinmarketcap.com/v1/global-metrics/quotes/latest`,
headers: { 'X-CMC_PRO_API_KEY': apiKeys.getGlobalApiKey() }
})
.then(response => {
lastGlobalResponse = response.data.data;
lastGlobalResponseDate = new Date().getDate();
event.sender.send('api-get-global-response', lastGlobalResponse);
})
.catch(() => {
event.sender.send('api-get-global-response', { error: true });
});
}
});