Skip to content

Commit c1ac3c1

Browse files
committed
feat(web): show pending notification count in favicon
1 parent bfb254c commit c1ac3c1

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

‎apps/web/src/threadNotifications.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ export function hasDesktopNotifications(mode: NotificationMode) {
1919
return mode === "notifications" || mode === "notifications-and-sound";
2020
}
2121

22+
let originalFavicon: HTMLLinkElement | undefined;
23+
let badgeFavicon: HTMLLinkElement | undefined;
24+
2225
export function setNotificationBadge(count: number) {
2326
const bridge = window.desktopBridge;
24-
if (!bridge?.setNotificationBadge) return;
2527
let image: string | null = null;
26-
if (count > 0 && bridge.getClientPlatform?.() === "win32") {
28+
if (count > 0 && (!bridge || bridge.getClientPlatform?.() === "win32")) {
2729
const canvas = document.createElement("canvas");
2830
canvas.width = canvas.height = 64;
2931
const context = canvas.getContext("2d");
@@ -40,7 +42,26 @@ export function setNotificationBadge(count: number) {
4042
image = canvas.toDataURL("image/png");
4143
}
4244
}
43-
void bridge.setNotificationBadge({ count, image }).catch(() => undefined);
45+
if (!bridge) {
46+
if (image) {
47+
if (!badgeFavicon) {
48+
originalFavicon = document.querySelector<HTMLLinkElement>('link[rel="icon"]') ?? undefined;
49+
badgeFavicon = document.createElement("link");
50+
badgeFavicon.rel = "icon";
51+
badgeFavicon.type = "image/png";
52+
badgeFavicon.sizes.value = "64x64";
53+
originalFavicon?.remove();
54+
document.head.append(badgeFavicon);
55+
}
56+
badgeFavicon.href = image;
57+
} else if (badgeFavicon) {
58+
badgeFavicon.remove();
59+
badgeFavicon = undefined;
60+
if (originalFavicon) document.head.append(originalFavicon);
61+
originalFavicon = undefined;
62+
}
63+
}
64+
void bridge?.setNotificationBadge?.({ count, image }).catch(() => undefined);
4465
}
4566

4667
let audioContext: AudioContext | undefined;

0 commit comments

Comments
 (0)