Skip to content

Commit 9c67bd1

Browse files
CopilotStanzilla
andauthored
Add native Download Now / Close actions to update notifications (#2973)
* Initial plan * feat: add update notification action buttons Agent-Logs-Url: https://github.com/WeakAuras/WeakAuras-Companion/sessions/55514c90-82ca-48b1-984a-ae85c832d219 * chore: refine notification action handling Agent-Logs-Url: https://github.com/WeakAuras/WeakAuras-Companion/sessions/55514c90-82ca-48b1-984a-ae85c832d219 * fix: harden update download notification link Agent-Logs-Url: https://github.com/WeakAuras/WeakAuras-Companion/sessions/55514c90-82ca-48b1-984a-ae85c832d219 * chore: simplify notification close action handling Agent-Logs-Url: https://github.com/WeakAuras/WeakAuras-Companion/sessions/55514c90-82ca-48b1-984a-ae85c832d219 * fix: improve asset name validation and update stale comment - Accept spaces and other valid filename chars in asset name by only rejecting paths that contain directory separators (path traversal) - Update tests: cover separator fallback and space-in-basename encoding - Update stale comment in index.ts to match renamed guard/behavior Agent-Logs-Url: https://github.com/WeakAuras/WeakAuras-Companion/sessions/2e0e1627-0981-404a-a044-4d34c238ac57 * merge: bring in origin/main changes Agent-Logs-Url: https://github.com/WeakAuras/WeakAuras-Companion/sessions/56a7f660-c2cd-439b-9d59-7dcae702ba6b --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: Stanzilla <[email protected]>
1 parent 8369106 commit 9c67bd1

4 files changed

Lines changed: 876 additions & 1399 deletions

File tree

electron/main/index.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ import type {
2323
} from "electron-updater";
2424
import { autoUpdater } from "electron-updater";
2525

26+
import {
27+
buildUpdateAvailableNotificationOptions,
28+
buildUpdateDownloadUrl,
29+
downloadNowNotificationActionIndex,
30+
} from "./update-available-notification";
31+
2632
const __filename = fileURLToPath(import.meta.url);
2733
const __dirname = path.dirname(__filename);
2834

@@ -74,6 +80,7 @@ let tray: Tray | null = null;
7480
let contextMenu: Menu | null = null;
7581
let mainWindow: BrowserWindow | null = null;
7682
const winURL = null;
83+
let updateAvailableNotificationShown = false;
7784

7885
const trayIconPath = join(
7986
process.env.PUBLIC,
@@ -456,17 +463,22 @@ autoUpdater.on("update-available", (info: UpdateInfo) => {
456463
if (mainWindow?.webContents) {
457464
mainWindow?.webContents.send("updaterHandler", "update-available", info);
458465
}
459-
let installNagAlreadyShown;
460466

461-
if (!installNagAlreadyShown) {
462-
new Notification({
463-
title: "A new update is available",
464-
body: `WeakAuras Companion ${info.version} is available for download.`,
465-
icon: notificationIcon,
466-
}).show();
467+
if (!updateAvailableNotificationShown) {
468+
const notification = new Notification(
469+
buildUpdateAvailableNotificationOptions(info, notificationIcon),
470+
);
467471

468-
// show install nag only once
469-
installNagAlreadyShown = true;
472+
notification.on("action", (_event, actionIndex) => {
473+
if (actionIndex === downloadNowNotificationActionIndex) {
474+
void shell.openExternal(buildUpdateDownloadUrl(info));
475+
}
476+
});
477+
478+
notification.show();
479+
480+
// show update-available notification only once
481+
updateAvailableNotificationShown = true;
470482
}
471483
});
472484

@@ -494,10 +506,8 @@ autoUpdater.on("download-progress", (info: ProgressInfo) => {
494506
}
495507
});
496508

497-
const installNagAlreadyShown = false;
498-
499509
autoUpdater.on("update-downloaded", (event: UpdateDownloadedEvent) => {
500-
if (!installNagAlreadyShown && mainWindow?.webContents) {
510+
if (mainWindow?.webContents) {
501511
mainWindow?.webContents.send("updaterHandler", "update-downloaded", event);
502512
mainWindow?.setProgressBar(-1);
503513
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type {
2+
NativeImage,
3+
NotificationAction,
4+
NotificationConstructorOptions,
5+
} from "electron";
6+
import type { UpdateInfo } from "electron-updater";
7+
8+
const DOWNLOAD_NOW_ACTION: NotificationAction = {
9+
type: "button",
10+
text: "Download Now",
11+
};
12+
13+
const CLOSE_ACTION: NotificationAction = {
14+
type: "button",
15+
text: "Close",
16+
};
17+
18+
export const downloadNowNotificationActionIndex = 0;
19+
20+
export const updateAvailableNotificationActions = [
21+
DOWNLOAD_NOW_ACTION,
22+
CLOSE_ACTION,
23+
];
24+
25+
export function buildUpdateDownloadUrl(
26+
info: Pick<UpdateInfo, "version" | "path">,
27+
) {
28+
const releaseTag = `v${encodeURIComponent(info.version)}`;
29+
const assetName = info.path;
30+
31+
if (/[\\/]/.test(assetName)) {
32+
return `https://github.com/WeakAuras/WeakAuras-Companion/releases/tag/${releaseTag}`;
33+
}
34+
35+
return `https://github.com/WeakAuras/WeakAuras-Companion/releases/download/${releaseTag}/${encodeURIComponent(assetName)}`;
36+
}
37+
38+
export function buildUpdateAvailableNotificationOptions(
39+
info: Pick<UpdateInfo, "version">,
40+
icon: NativeImage,
41+
platform = process.platform,
42+
): NotificationConstructorOptions {
43+
return {
44+
title: "A new update is available",
45+
body: `WeakAuras Companion ${info.version} is available for download.`,
46+
icon,
47+
...(platform === "win32"
48+
? { actions: updateAvailableNotificationActions }
49+
: {}),
50+
};
51+
}

0 commit comments

Comments
 (0)