-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.js
More file actions
237 lines (211 loc) · 6.81 KB
/
main.js
File metadata and controls
237 lines (211 loc) · 6.81 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* eslint-disable no-undef */
const { app, BrowserWindow, ipcMain, Notification, nativeTheme, Menu, protocol, net } = require('electron');
const path = require('path');
const fs = require('fs');
const { pathToFileURL } = require('url');
// Register custom protocol scheme before app is ready
// This allows serving the Expo web export with absolute paths (/_expo/static/...)
// via a custom protocol instead of file://, which breaks absolute path resolution.
protocol.registerSchemesAsPrivileged([
{
scheme: 'app',
privileges: {
standard: true,
secure: true,
supportFetchAPI: true,
corsEnabled: true,
stream: true,
},
},
]);
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) {
app.quit();
}
let mainWindow = null;
const isDev = process.env.NODE_ENV === 'development' || !app.isPackaged;
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
minWidth: 800,
minHeight: 600,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js'),
},
// MacOS: use hidden title bar with traffic lights
titleBarStyle: process.platform === 'darwin' ? 'hiddenInset' : 'default',
// Windows/Linux: show frame
frame: process.platform !== 'darwin',
// Set the background color to match the app theme
backgroundColor: nativeTheme.shouldUseDarkColors ? '#1a1a1a' : '#ffffff',
icon: path.join(__dirname, '../assets/icon.png'),
show: false, // Don't show until ready
});
// Load the app
if (isDev) {
// In development, load from the Expo dev server
mainWindow.loadURL('http://localhost:8081');
} else {
// In production, load via the custom app:// protocol
// which correctly resolves absolute paths (/_expo/static/...) from the dist directory
mainWindow.loadURL('app://bundle/index.html');
}
// Show window when ready
mainWindow.once('ready-to-show', () => {
mainWindow.show();
// Open DevTools in development
if (isDev) {
mainWindow.webContents.openDevTools();
}
});
// Handle window closed
mainWindow.on('closed', () => {
mainWindow = null;
});
// Handle external links
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
require('electron').shell.openExternal(url);
return { action: 'deny' };
});
}
// Build application menu
function createMenu() {
const isMac = process.platform === 'darwin';
const template = [
// App Menu (macOS only)
...(isMac
? [
{
label: app.name,
submenu: [{ role: 'about' }, { type: 'separator' }, { role: 'services' }, { type: 'separator' }, { role: 'hide' }, { role: 'hideOthers' }, { role: 'unhide' }, { type: 'separator' }, { role: 'quit' }],
},
]
: []),
// File Menu
{
label: 'File',
submenu: [isMac ? { role: 'close' } : { role: 'quit' }],
},
// Edit Menu
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
...(isMac ? [{ role: 'pasteAndMatchStyle' }, { role: 'delete' }, { role: 'selectAll' }] : [{ role: 'delete' }, { type: 'separator' }, { role: 'selectAll' }]),
],
},
// View Menu
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ type: 'separator' },
{ role: 'togglefullscreen' },
],
},
// Window Menu
{
label: 'Window',
submenu: [{ role: 'minimize' }, { role: 'zoom' }, ...(isMac ? [{ type: 'separator' }, { role: 'front' }, { type: 'separator' }, { role: 'window' }] : [{ role: 'close' }])],
},
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
// IPC Handlers for notifications
ipcMain.handle('show-notification', async (event, { title, body, data }) => {
if (!Notification.isSupported()) {
console.warn('Notifications are not supported on this system');
return false;
}
const notification = new Notification({
title: title || 'Resgrid Unit',
body: body || '',
silent: false,
icon: path.join(__dirname, '../assets/icon.png'),
});
notification.on('click', () => {
// Focus the window
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
// Send notification data to renderer
mainWindow.webContents.send('notification-clicked', data);
}
});
notification.show();
return true;
});
// Handle getting platform info
ipcMain.handle('get-platform', () => {
return process.platform;
});
// Handle app ready
app.whenReady().then(() => {
// Register custom protocol handler for serving the Expo web export
// This resolves absolute paths like /_expo/static/js/... from the dist directory
const distPath = path.join(__dirname, '..', 'dist');
const resolvedDist = path.resolve(distPath);
protocol.handle('app', (request) => {
const url = new URL(request.url);
// Decode the pathname and resolve to a file in dist/
const resolvedPath = path.resolve(distPath, decodeURIComponent(url.pathname));
// Security check: ensure resolved path is within distPath to prevent directory traversal
let filePath;
if (!resolvedPath.startsWith(resolvedDist + path.sep) && resolvedPath !== resolvedDist) {
// Path escapes distPath - fall back to index.html
filePath = path.join(resolvedDist, 'index.html');
} else {
filePath = resolvedPath;
// If the path points to a directory or file doesn't exist, fall back to index.html
// This supports SPA client-side routing
try {
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
filePath = path.join(resolvedDist, 'index.html');
}
} catch {
// File not found - serve index.html for client-side routing
filePath = path.join(resolvedDist, 'index.html');
}
}
return net.fetch(pathToFileURL(filePath).toString());
});
createMenu();
createWindow();
// On macOS, re-create window when dock icon is clicked
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
// Quit when all windows are closed, except on macOS
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
// Handle deep links (for future use)
app.on('open-url', (event, url) => {
event.preventDefault();
// Handle the URL
console.log('Deep link received:', url);
});