66import { useCallback , useRef , useEffect } from "react"
77import { useAtomValue } from "jotai"
88import { isDesktopApp } from "../../../lib/utils/platform"
9- import { desktopNotificationsEnabledAtom } from "../../../lib/atoms"
9+ import { desktopNotificationsEnabledAtom , notifyWhenFocusedAtom } from "../../../lib/atoms"
1010
1111// throttle interval to prevent notification spam (ms)
1212const NOTIFICATION_THROTTLE_MS = 3000
@@ -30,6 +30,7 @@ export interface NotificationOptions {
3030
3131export function useDesktopNotifications ( ) {
3232 const notificationsEnabled = useAtomValue ( desktopNotificationsEnabledAtom )
33+ const notifyWhenFocused = useAtomValue ( notifyWhenFocusedAtom )
3334
3435 // track last notification time to throttle rapid-fire notifications
3536 const lastNotificationTime = useRef < number > ( 0 )
@@ -98,15 +99,15 @@ export function useDesktopNotifications() {
9899 } , [ notificationsEnabled ] )
99100
100101 const notifyAgentComplete = useCallback ( ( chatName : string ) => {
101- // don't notify if window is focused - user is already watching
102- if ( document . hasFocus ( ) ) {
102+ // Skip if window is focused and user hasn't opted into focused notifications
103+ if ( ! notifyWhenFocused && document . hasFocus ( ) ) {
103104 return
104105 }
105106
106107 const title = "Agent Complete"
107108 const body = chatName ? `Finished working on "${ chatName } "` : "Agent has completed its task"
108109 showNotification ( title , body , { priority : "complete" } )
109- } , [ showNotification ] )
110+ } , [ showNotification , notifyWhenFocused ] )
110111
111112 const notifyAgentError = useCallback ( ( errorMessage : string ) => {
112113 // always notify on errors, even if window is focused
@@ -116,26 +117,24 @@ export function useDesktopNotifications() {
116117 } , [ showNotification ] )
117118
118119 const notifyAgentNeedsInput = useCallback ( ( chatName : string ) => {
119- // don't notify if window is focused
120- if ( document . hasFocus ( ) ) {
120+ if ( ! notifyWhenFocused && document . hasFocus ( ) ) {
121121 return
122122 }
123123
124124 const title = "Input Required"
125125 const body = chatName ? `"${ chatName } " is waiting for your input` : "Agent is waiting for your input"
126126 showNotification ( title , body , { priority : "input" } )
127- } , [ showNotification ] )
127+ } , [ showNotification , notifyWhenFocused ] )
128128
129129 const notifyPlanReady = useCallback ( ( chatName : string ) => {
130- // don't notify if window is focused
131- if ( document . hasFocus ( ) ) {
130+ if ( ! notifyWhenFocused && document . hasFocus ( ) ) {
132131 return
133132 }
134133
135134 const title = "Plan Ready"
136135 const body = chatName ? `"${ chatName } " has a plan ready for approval` : "A plan is ready for your approval"
137136 showNotification ( title , body , { priority : "plan" } )
138- } , [ showNotification ] )
137+ } , [ showNotification , notifyWhenFocused ] )
139138
140139 const requestPermission = useCallback ( async ( ) : Promise < NotificationPermission > => {
141140 if ( isDesktopApp ( ) ) {
0 commit comments