Skip to content

Commit 6558df3

Browse files
feat(notifications): notify when focused on a different task
Previously, permission requests and prompt completions only fired a sound, desktop notification, and dock badge if the window was unfocused. If the user was viewing task A in PostHog Code while task B asked for input, nothing surfaced — they had to manually check the other task. Now also notify when the window is focused but the user is viewing a different task (or no task at all). The window-focus path is unchanged. Generated-By: PostHog Code Task-Id: b14a2144-3143-4d34-a0e3-2139aabdaed9
1 parent 6551355 commit 6558df3

1 file changed

Lines changed: 30 additions & 22 deletions

File tree

apps/code/src/renderer/utils/notifications.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useSettingsStore } from "@features/settings/stores/settingsStore";
22
import { trpcClient } from "@renderer/trpc/client";
3+
import { useNavigationStore } from "@stores/navigationStore";
34
import { logger } from "@utils/logger";
45
import { playCompletionSound } from "@utils/sounds";
56

@@ -12,6 +13,15 @@ function truncateTitle(title: string): string {
1213
return `${title.slice(0, MAX_TITLE_LENGTH)}...`;
1314
}
1415

16+
function shouldNotifyForTask(taskId?: string): boolean {
17+
if (!document.hasFocus()) return true;
18+
if (!taskId) return false;
19+
const view = useNavigationStore.getState().view;
20+
const viewedTaskId =
21+
view.type === "task-detail" ? (view.data?.id ?? view.taskId) : undefined;
22+
return viewedTaskId !== taskId;
23+
}
24+
1525
function sendDesktopNotification(
1626
title: string,
1727
body: string,
@@ -52,8 +62,7 @@ export function notifyPromptComplete(
5262
dockBounceNotifications,
5363
} = useSettingsStore.getState();
5464

55-
const isWindowFocused = document.hasFocus();
56-
if (isWindowFocused) return;
65+
if (!shouldNotifyForTask(taskId)) return;
5766

5867
const willPlayCustomSound = completionSound !== "none";
5968
playCompletionSound(completionSound, completionVolume);
@@ -85,25 +94,24 @@ export function notifyPermissionRequest(
8594
dockBadgeNotifications,
8695
dockBounceNotifications,
8796
} = useSettingsStore.getState();
88-
const isWindowFocused = document.hasFocus();
89-
90-
if (!isWindowFocused) {
91-
const willPlayCustomSound = completionSound !== "none";
92-
playCompletionSound(completionSound, completionVolume);
93-
94-
if (desktopNotifications) {
95-
sendDesktopNotification(
96-
"PostHog Code",
97-
`"${truncateTitle(taskTitle)}" needs your input`,
98-
willPlayCustomSound,
99-
taskId,
100-
);
101-
}
102-
if (dockBadgeNotifications) {
103-
showDockBadge();
104-
}
105-
if (dockBounceNotifications) {
106-
bounceDock();
107-
}
97+
98+
if (!shouldNotifyForTask(taskId)) return;
99+
100+
const willPlayCustomSound = completionSound !== "none";
101+
playCompletionSound(completionSound, completionVolume);
102+
103+
if (desktopNotifications) {
104+
sendDesktopNotification(
105+
"PostHog Code",
106+
`"${truncateTitle(taskTitle)}" needs your input`,
107+
willPlayCustomSound,
108+
taskId,
109+
);
110+
}
111+
if (dockBadgeNotifications) {
112+
showDockBadge();
113+
}
114+
if (dockBounceNotifications) {
115+
bounceDock();
108116
}
109117
}

0 commit comments

Comments
 (0)