Open History to see the last commit
1import type { ClientSettings } from "@t3tools/contracts/settings";
2
3import completionUrl from "./assets/notification-completion.mp3";
4import inputUrl from "./assets/notification-input.mp3";
5
6type NotificationMode = ClientSettings["notificationMode"];
7export const NOTIFICATION_MODE_LABELS = {
8 off: "Off",
9 notifications: "Notifications only",
10 sound: "Sound only",
11 "notifications-and-sound": "Notifications with sound",
12} satisfies Record<NotificationMode, string>;
13
14export function hasNotificationSound(mode: NotificationMode) {
15 return mode === "sound" || mode === "notifications-and-sound";
16}
17
18export function hasDesktopNotifications(mode: NotificationMode) {
19 return mode === "notifications" || mode === "notifications-and-sound";
20}
21
22let originalFavicon: HTMLLinkElement | undefined;
23let badgeFavicon: HTMLLinkElement | undefined;
24
25export function setNotificationBadge(count: number) {
26 const bridge = window.desktopBridge;
27 let image: string | null = null;
28 if (count > 0 && (!bridge || bridge.getClientPlatform?.() === "win32")) {
29 const canvas = document.createElement("canvas");
30 canvas.width = canvas.height = 64;
31 const context = canvas.getContext("2d");
32 if (context) {
33 context.fillStyle = "#e5484d";
34 context.beginPath();
35 context.arc(32, 32, 28, 0, Math.PI * 2);
36 context.fill();
37 context.fillStyle = "white";
38 context.font = `600 ${count > 9 ? 30 : 40}px "Segoe UI", sans-serif`;
39 context.textAlign = "center";
40 context.textBaseline = "middle";
41 context.fillText(count > 9 ? "9+" : String(count), 32, 34);
42 image = canvas.toDataURL("image/png");
43 }
44 }
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);
65}
66
67let audioContext: AudioContext | undefined;
68const buffers = new Map<string, Promise<AudioBuffer>>();
69/** When each sound's current playback ends, on the audio context's clock. */
70const playingUntil = new Map<string, number>();
71
72/** Called from a gesture so browsers allow later background playback. */
73export function unlockNotificationAudio() {
74 audioContext ??= new AudioContext();
75 void audioContext.resume().catch(() => undefined);
76}
77
78export async function playNotificationSound(
79 kind: "completion" | "input",
80 shouldPlay: () => boolean,
81) {
82 if (!audioContext || audioContext.state !== "running") return;
83 const context = audioContext;
84 const url = kind === "completion" ? completionUrl : inputUrl;
85 try {
86 let buffer = buffers.get(url);
87 if (!buffer) {
88 buffer = fetch(url)
89 .then((response) => response.arrayBuffer())
90 .then((data) => context.decodeAudioData(data));
91 buffers.set(url, buffer);
92 }
93 const decoded = await buffer;
94 if (!shouldPlay() || context.state !== "running") return;
95 // Threads that finish together each ask for this sound, and full-volume copies sum into
96 // clipping. One playback stands for the burst. Checked after the decode, since every caller
97 // in a burst is waiting on it. The other kind keeps its own slot, so an input alert is still
98 // heard over a completion.
99 if (context.currentTime < (playingUntil.get(url) ?? 0)) return;
100 const source = context.createBufferSource();
101 source.buffer = decoded;
102 source.connect(context.destination);
103 source.start();
104 playingUntil.set(url, context.currentTime + decoded.duration);
105 } catch {
106 buffers.delete(url);
107 }
108}