@@ -79,13 +79,31 @@ export function startThreadNotifications(
7979 pending . set ( notification . tag , { environmentId, threadId, notification } ) ;
8080 setNotificationBadge ( pending . size ) ;
8181 } ;
82- // Toast ids by thread key, so hiding a thread can close its open toasts.
82+ // Toast ids by thread key, so hiding or opening a thread can close its open toasts.
8383 const openToasts = new Map < string , Array < ToastId > > ( ) ;
8484 const forgetToast = ( key : string , toastId : ToastId ) => {
8585 const remaining = ( openToasts . get ( key ) ?? [ ] ) . filter ( ( id ) => id !== toastId ) ;
8686 if ( remaining . length === 0 ) openToasts . delete ( key ) ;
8787 else openToasts . set ( key , remaining ) ;
8888 } ;
89+ const closeToasts = ( shouldClose : ( key : string ) => boolean ) => {
90+ for ( const [ key , toastIds ] of openToasts ) {
91+ if ( ! shouldClose ( key ) ) continue ;
92+ for ( const toastId of toastIds ) toastManager . close ( toastId ) ;
93+ openToasts . delete ( key ) ;
94+ }
95+ } ;
96+ const activeThread = ( ) =>
97+ ( router . state . matches . at ( - 1 ) ?. params ?? { } ) as {
98+ environmentId ?: EnvironmentId ;
99+ threadId ?: ThreadId ;
100+ } ;
101+ const closeOpenedThreadToasts = ( ) => {
102+ const { environmentId, threadId } = activeThread ( ) ;
103+ if ( environmentId === undefined || threadId === undefined ) return ;
104+ const opened = threadKey ( { environmentId, threadId } ) ;
105+ closeToasts ( ( key ) => key === opened ) ;
106+ } ;
89107 // A notification shown before the tag filter turned on must not stay on screen. While hiding,
90108 // one whose thread left the live list (archived) cannot be judged, so it closes too.
91109 const closeHiddenPending = ( ) => {
@@ -94,11 +112,7 @@ export function startThreadNotifications(
94112 ? new Set ( registry . get ( environmentThreadShells . threadRefsAtom ) . map ( threadKey ) )
95113 : null ;
96114 const shouldClose = ( key : string ) => hidden . has ( key ) || ( live !== null && ! live . has ( key ) ) ;
97- for ( const [ key , toastIds ] of openToasts ) {
98- if ( ! shouldClose ( key ) ) continue ;
99- for ( const toastId of toastIds ) toastManager . close ( toastId ) ;
100- openToasts . delete ( key ) ;
101- }
115+ closeToasts ( shouldClose ) ;
102116 const count = pending . size ;
103117 for ( const [ tag , entry ] of pending ) {
104118 if ( ! shouldClose ( threadKey ( entry ) ) ) continue ;
@@ -126,10 +140,7 @@ export function startThreadNotifications(
126140 )
127141 return ;
128142 const { notificationMode : mode , inAppNotificationsEnabled } = getClientSettings ( ) ;
129- const active = ( router . state . matches . at ( - 1 ) ?. params ?? { } ) as {
130- environmentId ?: string ;
131- threadId ?: string ;
132- } ;
143+ const active = activeThread ( ) ;
133144 const title =
134145 kind === "completion"
135146 ? "Thread completed"
@@ -347,6 +358,7 @@ export function startThreadNotifications(
347358 const syncEnvironments = ( ) => {
348359 const { notificationMode, inAppNotificationsEnabled } = getClientSettings ( ) ;
349360 const enabled = notificationMode !== "off" || inAppNotificationsEnabled ;
361+ if ( ! enabled ) closeToasts ( ( ) => true ) ;
350362 const environmentIds = enabled ? registry . get ( environmentSummaries . environmentIdsAtom ) : [ ] ;
351363 const activeIds = new Set ( environmentIds ) ;
352364 for ( const [ environmentId , entry ] of watched ) {
@@ -419,14 +431,17 @@ export function startThreadNotifications(
419431 registry . subscribe ( environmentThreadShells . anyEnvironmentHidingAtom , closeHiddenPending ) ,
420432 registry . subscribe ( environmentThreadShells . threadRefsAtom , closeHiddenPending ) ,
421433 ] ;
434+ const unsubscribeRoute = router . subscribe ( "onResolved" , closeOpenedThreadToasts ) ;
422435 onSettings ( ) ;
423436
424437 return ( ) => {
425438 unsubscribeSettings ( ) ;
426439 unsubscribeEnvironmentIds ( ) ;
427440 for ( const unsubscribe of unsubscribeHidden ) unsubscribe ( ) ;
441+ unsubscribeRoute ( ) ;
428442 for ( const entry of watched . values ( ) ) entry . unsubscribe ( ) ;
429443 watched . clear ( ) ;
430444 stopModeListeners ( ) ;
445+ closeToasts ( ( ) => true ) ;
431446 } ;
432447}
0 commit comments