2026-03-20 19:50:50 +02:00
|
|
|
import { app, BrowserWindow, shell, ipcMain, dialog, Menu } from 'electron';
|
2026-03-19 20:20:26 +02:00
|
|
|
import { autoUpdater } from 'electron-updater';
|
2026-03-20 19:44:48 +02:00
|
|
|
import { registerGlobalErrorHandlers, showErrorDialog } from './modules/errors';
|
2026-03-20 19:55:44 +02:00
|
|
|
import { startNextServer } from './modules/server';
|
|
|
|
|
import { createMainWindow } from './modules/window';
|
|
|
|
|
import { setupDeepLinks, handlePendingDeepLink } from './modules/deep-links';
|
2026-03-17 16:22:14 +02:00
|
|
|
|
2026-03-20 19:44:48 +02:00
|
|
|
registerGlobalErrorHandlers();
|
2026-03-18 19:49:50 +02:00
|
|
|
|
2026-03-20 19:55:44 +02:00
|
|
|
if (!setupDeepLinks()) {
|
|
|
|
|
app.quit();
|
|
|
|
|
}
|
2026-03-17 16:22:14 +02:00
|
|
|
|
2026-03-20 19:55:44 +02:00
|
|
|
const isDev = !app.isPackaged;
|
2026-03-17 16:22:14 +02:00
|
|
|
|
|
|
|
|
// IPC handlers
|
|
|
|
|
ipcMain.on('open-external', (_event, url: string) => {
|
2026-03-18 20:58:49 +02:00
|
|
|
try {
|
|
|
|
|
const parsed = new URL(url);
|
|
|
|
|
if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
|
|
|
|
|
shell.openExternal(url);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
// invalid URL — ignore
|
|
|
|
|
}
|
2026-03-17 16:22:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('get-app-version', () => {
|
|
|
|
|
return app.getVersion();
|
|
|
|
|
});
|
|
|
|
|
|
2026-03-19 20:20:26 +02:00
|
|
|
function setupAutoUpdater() {
|
|
|
|
|
if (isDev) return;
|
|
|
|
|
|
|
|
|
|
autoUpdater.autoDownload = true;
|
|
|
|
|
|
|
|
|
|
autoUpdater.on('update-available', (info) => {
|
|
|
|
|
console.log(`Update available: ${info.version}`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
autoUpdater.on('update-downloaded', (info) => {
|
|
|
|
|
console.log(`Update downloaded: ${info.version}`);
|
|
|
|
|
dialog.showMessageBox({
|
|
|
|
|
type: 'info',
|
|
|
|
|
buttons: ['Restart', 'Later'],
|
|
|
|
|
defaultId: 0,
|
|
|
|
|
title: 'Update Ready',
|
|
|
|
|
message: `Version ${info.version} has been downloaded. Restart to apply the update.`,
|
|
|
|
|
}).then(({ response }) => {
|
|
|
|
|
if (response === 0) {
|
|
|
|
|
autoUpdater.quitAndInstall();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
autoUpdater.on('error', (err) => {
|
|
|
|
|
console.error('Auto-updater error:', err);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
autoUpdater.checkForUpdates();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 20:10:30 +02:00
|
|
|
function setupMenu() {
|
|
|
|
|
const isMac = process.platform === 'darwin';
|
|
|
|
|
const template: Electron.MenuItemConstructorOptions[] = [
|
|
|
|
|
...(isMac ? [{ role: 'appMenu' as const }] : []),
|
|
|
|
|
{ role: 'fileMenu' as const },
|
|
|
|
|
{ role: 'editMenu' as const },
|
|
|
|
|
{ role: 'viewMenu' as const },
|
|
|
|
|
{ role: 'windowMenu' as const },
|
|
|
|
|
];
|
|
|
|
|
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:22:14 +02:00
|
|
|
// App lifecycle
|
|
|
|
|
app.whenReady().then(async () => {
|
2026-03-18 20:10:30 +02:00
|
|
|
setupMenu();
|
2026-03-18 19:49:59 +02:00
|
|
|
try {
|
|
|
|
|
await startNextServer();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
showErrorDialog('Failed to start SurfSense', error);
|
|
|
|
|
setTimeout(() => app.quit(), 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-03-20 19:50:50 +02:00
|
|
|
createMainWindow();
|
2026-03-19 20:20:26 +02:00
|
|
|
setupAutoUpdater();
|
2026-03-17 16:22:14 +02:00
|
|
|
|
2026-03-20 19:55:44 +02:00
|
|
|
handlePendingDeepLink();
|
2026-03-17 17:32:28 +02:00
|
|
|
|
2026-03-17 16:22:14 +02:00
|
|
|
app.on('activate', () => {
|
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
2026-03-20 19:50:50 +02:00
|
|
|
createMainWindow();
|
2026-03-17 16:22:14 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
|
app.quit();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.on('will-quit', () => {
|
2026-03-18 17:51:47 +02:00
|
|
|
// Server runs in-process — no child process to kill
|
2026-03-17 16:22:14 +02:00
|
|
|
});
|