Skip to content

Commit fa1adaf

Browse files
feat(web): add browser notifications for thread lifecycle events
Add an opt-in notifications toggle in Settings that fires browser notifications when the app is backgrounded. Covers task completions, approval requests, and user input requests with project name and thread title in the notification body. Clicking navigates to the thread. Batches 4+ simultaneous notifications into a summary. Closes #780
1 parent d8aa2f8 commit fa1adaf

7 files changed

Lines changed: 724 additions & 1 deletion

File tree

‎apps/web/src/components/settings/SettingsPanels.tsx‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ import { resolveAndPersistPreferredEditor } from "../../editorPreferences";
3535
import { isElectron } from "../../env";
3636
import { useTheme } from "../../hooks/useTheme";
3737
import { useSettings, useUpdateSettings } from "../../hooks/useSettings";
38+
import {
39+
getNotificationPermissionState,
40+
requestNotificationPermission,
41+
} from "../../hooks/useThreadNotifications";
3842
import { useThreadActions } from "../../hooks/useThreadActions";
3943
import {
4044
setDesktopUpdateStateQueryData,
@@ -462,6 +466,9 @@ export function useSettingsRestore(onRestored?: () => void) {
462466
...(settings.timestampFormat !== DEFAULT_UNIFIED_SETTINGS.timestampFormat
463467
? ["Time format"]
464468
: []),
469+
...(settings.notificationsEnabled !== DEFAULT_UNIFIED_SETTINGS.notificationsEnabled
470+
? ["Notifications"]
471+
: []),
465472
...(settings.diffWordWrap !== DEFAULT_UNIFIED_SETTINGS.diffWordWrap
466473
? ["Diff line wrapping"]
467474
: []),
@@ -488,6 +495,7 @@ export function useSettingsRestore(onRestored?: () => void) {
488495
settings.defaultThreadEnvMode,
489496
settings.diffWordWrap,
490497
settings.enableAssistantStreaming,
498+
settings.notificationsEnabled,
491499
settings.timestampFormat,
492500
theme,
493501
],
@@ -771,6 +779,46 @@ export function GeneralSettingsPanel() {
771779
}
772780
/>
773781

782+
<SettingsRow
783+
title="Notifications"
784+
description="Get notified when a task completes, or needs approval or input while the app is in the background."
785+
status={
786+
settings.notificationsEnabled && getNotificationPermissionState() === "unsupported" ? (
787+
<span className="text-warning">Notifications are not supported in this browser.</span>
788+
) : settings.notificationsEnabled && getNotificationPermissionState() === "denied" ? (
789+
<span className="text-warning">
790+
Browser notifications are blocked. Allow them in your browser settings to receive
791+
alerts.
792+
</span>
793+
) : null
794+
}
795+
resetAction={
796+
settings.notificationsEnabled !== DEFAULT_UNIFIED_SETTINGS.notificationsEnabled ? (
797+
<SettingResetButton
798+
label="notifications"
799+
onClick={() =>
800+
updateSettings({
801+
notificationsEnabled: DEFAULT_UNIFIED_SETTINGS.notificationsEnabled,
802+
})
803+
}
804+
/>
805+
) : null
806+
}
807+
control={
808+
<Switch
809+
checked={settings.notificationsEnabled}
810+
onCheckedChange={(checked) => {
811+
const enabled = Boolean(checked);
812+
updateSettings({ notificationsEnabled: enabled });
813+
if (enabled) {
814+
void requestNotificationPermission();
815+
}
816+
}}
817+
aria-label="Enable notifications"
818+
/>
819+
}
820+
/>
821+
774822
<SettingsRow
775823
title="Time format"
776824
description="System default follows your browser or OS clock preference."

‎apps/web/src/hooks/useSettings.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ export function buildLegacyClientSettingsMigrationPatch(
218218
patch.timestampFormat = legacySettings.timestampFormat;
219219
}
220220

221+
if (Predicate.isBoolean(legacySettings.notificationsEnabled)) {
222+
patch.notificationsEnabled = legacySettings.notificationsEnabled;
223+
}
224+
221225
return patch;
222226
}
223227

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { useEffect, useRef } from "react";
2+
import type { ThreadId } from "@t3tools/contracts";
3+
import { isElectron } from "../env";
4+
import { useStore } from "../store";
5+
import {
6+
type ThreadNotificationSnapshot,
7+
collectThreadNotificationSnapshots,
8+
consolidateNotifications,
9+
diffThreadNotifications,
10+
} from "../lib/threadNotifications";
11+
12+
function isAppBackgrounded(): boolean {
13+
return document.visibilityState === "hidden" || !document.hasFocus();
14+
}
15+
16+
function canSendNotifications(): boolean {
17+
return typeof Notification !== "undefined" && Notification.permission === "granted";
18+
}
19+
20+
export async function requestNotificationPermission(): Promise<boolean> {
21+
if (typeof Notification === "undefined") return false;
22+
if (Notification.permission === "granted") return true;
23+
if (Notification.permission === "denied") return false;
24+
const result = await Notification.requestPermission();
25+
return result === "granted";
26+
}
27+
28+
export function getNotificationPermissionState(): NotificationPermission | "unsupported" {
29+
if (typeof Notification === "undefined") return "unsupported";
30+
return Notification.permission;
31+
}
32+
33+
export function useThreadNotifications(enabled: boolean): void {
34+
const previousSnapshotRef = useRef<ReadonlyMap<ThreadId, ThreadNotificationSnapshot> | null>(
35+
null,
36+
);
37+
const bootstrapComplete = useStore((state) => state.bootstrapComplete);
38+
const threads = useStore((state) => state.threads);
39+
const projects = useStore((state) => state.projects);
40+
41+
useEffect(() => {
42+
if (!bootstrapComplete) return;
43+
44+
const currentSnapshot = collectThreadNotificationSnapshots(threads, projects);
45+
46+
if (!enabled) {
47+
// Keep in sync so toggling on doesn't fire for stale transitions.
48+
previousSnapshotRef.current = currentSnapshot;
49+
return;
50+
}
51+
52+
if (previousSnapshotRef.current === null) {
53+
previousSnapshotRef.current = currentSnapshot;
54+
return;
55+
}
56+
57+
if (!isAppBackgrounded() || !canSendNotifications()) {
58+
previousSnapshotRef.current = currentSnapshot;
59+
return;
60+
}
61+
62+
const rawNotifications = diffThreadNotifications(previousSnapshotRef.current, currentSnapshot);
63+
64+
if (rawNotifications.length > 0) {
65+
const consolidated = consolidateNotifications(rawNotifications);
66+
for (const notification of consolidated) {
67+
const browserNotification = new Notification(notification.title, {
68+
body: notification.body,
69+
tag: notification.threadId ?? "t3code-batch",
70+
});
71+
72+
if (notification.threadId) {
73+
const threadId = notification.threadId;
74+
browserNotification.addEventListener("click", () => {
75+
window.focus();
76+
if (isElectron) {
77+
window.location.hash = `#/${threadId}`;
78+
} else {
79+
window.history.pushState(null, "", `/${threadId}`);
80+
window.dispatchEvent(new PopStateEvent("popstate"));
81+
}
82+
});
83+
} else {
84+
browserNotification.addEventListener("click", () => {
85+
window.focus();
86+
});
87+
}
88+
}
89+
}
90+
91+
previousSnapshotRef.current = currentSnapshot;
92+
}, [bootstrapComplete, enabled, projects, threads]);
93+
}

0 commit comments

Comments
 (0)