-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
121 lines (99 loc) · 3.24 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const { notion } = require("./src/notion");
const { flatMap } = require("rxjs/operators");
const { persistAuth, reviveAuth } = require("./src/auth");
const { Tray, BrowserWindow, ipcMain, app } = require("electron");
const { withLatestFrom, share } = require("rxjs/operators");
const { selectedMetricScore$ } = require("./src/selectedMetric");
const { getIcon, defaultIcon } = require("./src/icon");
const { getAuthenticatedMenu } = require("./src/menuTemplates");
const { ReactiveTrayMenu } = require("./src/menu");
const { syncDoToDisturb } = require("./src/doNotDisturb");
const { selectedMetric } = require("./src/selectedMetric");
const { getLoginMenu } = require("./src/menuTemplates");
const { streamReady } = require("./src/status");
const { onThemeUpdated } = require("./src/nativeTheme");
const path = require("path");
const isDev = require("electron-is-dev");
let tray = null;
let loginWindow = null;
app.on("ready", async () => {
tray = new Tray(defaultIcon);
tray.setToolTip("Neurosity macOS");
reviveAuth();
loginWindow = new BrowserWindow({
width: 400,
height: 600,
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
}
});
loginWindow.loadURL(
isDev
? `file://${path.join(__dirname, "src/login/login.html")}`
: `file://${path.join(__dirname, "build/src/login/login.html")}`
);
loginWindow.on("close", (e) => {
e.preventDefault();
loginWindow.hide();
});
const menu = new ReactiveTrayMenu(tray, getLoginMenu(loginWindow));
// hack metric checkbox to work as radios since it's buggy
tray.on("click", () => {
menu.setSelectedMetric(selectedMetric.getValue());
});
ipcMain.on("open-tray-menu", () => {
tray.popUpContextMenu();
});
ipcMain.on("login-submit", (event, credentials) => {
notion
.login(credentials)
.then(() => {
event.reply("login-response", { ok: true });
persistAuth();
})
.catch((error) => {
event.reply("login-response", {
ok: false,
error: error.message
});
});
});
notion.onAuthStateChanged().subscribe(async (auth) => {
if (!auth) {
menu.setState(() => getLoginMenu(loginWindow));
return;
}
menu.setState(() => getAuthenticatedMenu(loginWindow));
const { selectedDevice } = auth;
const devices = await notion.getDevices();
menu.setDevices(devices, selectedDevice);
notion.onDeviceChange().subscribe((device) => {
menu.setSelectedDevice(device);
notion.getInfo().then((info) => {
menu.setDeviceInfo(info);
});
});
const status$ = notion.status().pipe(share());
status$.subscribe((status) => {
menu.setStatus(status);
});
// updates tray icon with selected metric
selectedMetricScore$
.pipe(withLatestFrom(status$, onThemeUpdated()))
.subscribe(async ([score, status, theme]) => {
const iconWithMetric = await getIcon({ score, theme });
if (streamReady(status)) {
tray.setImage(iconWithMetric);
} else {
tray.setImage(defaultIcon);
}
});
});
notion.settings().subscribe((settings) => {
menu.setDeviceSettings(settings);
});
syncDoToDisturb();
});