44} from "@t3tools/client-runtime/state/models" ;
55import { pendingSubagentRequestsByThread } from "@t3tools/client-runtime/state/threads" ;
66import { useAtomValue } from "@effect/atom-react" ;
7- import { useNavigate , useParams } from "@tanstack/react-router" ;
7+ import { useNavigate } from "@tanstack/react-router" ;
88import type { EnvironmentId , OrchestrationV2ThreadShell , ThreadId } from "@t3tools/contracts" ;
99import * as Option from "effect/Option" ;
1010import {
@@ -15,7 +15,9 @@ import {
1515} from "lucide-react" ;
1616import { useEffect , useRef } from "react" ;
1717
18+ import { useComposerDraftStore } from "../composerDraftStore" ;
1819import { getClientSettings , useClientSettings } from "../hooks/useSettings" ;
20+ import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget" ;
1921import { useEnvironmentIds } from "../state/environments" ;
2022import { environmentShell } from "../state/shell" ;
2123import {
@@ -26,6 +28,7 @@ import {
2628 threadNotificationTag ,
2729 unlockNotificationAudio ,
2830} from "../threadNotifications" ;
31+ import { resolveActiveThreadRouteRef } from "../threadRoutes" ;
2932import { resolveSidebarThreadStatus } from "./Sidebar.logic" ;
3033import { toastManager } from "./ui/toast" ;
3134
@@ -35,6 +38,12 @@ export function ThreadNotificationCoordinator() {
3538 const inAppNotificationsEnabled = useClientSettings (
3639 ( settings ) => settings . inAppNotificationsEnabled ,
3740 ) ;
41+ const routeTarget = useThreadRouteTarget ( ) ;
42+ const routeDraftThread = useComposerDraftStore ( ( store ) =>
43+ routeTarget ?. kind === "draft" ? store . getDraftSession ( routeTarget . draftId ) : null ,
44+ ) ;
45+ // A promoted draft route already renders its server thread.
46+ const openThreadRef = resolveActiveThreadRouteRef ( routeTarget , routeDraftThread ) ;
3847 const pending = useRef (
3948 new Map < string , { environmentId : EnvironmentId ; notification : Notification } > ( ) ,
4049 ) ;
@@ -88,6 +97,7 @@ export function ThreadNotificationCoordinator() {
8897 < EnvironmentNotifications
8998 key = { environmentId }
9099 environmentId = { environmentId }
100+ openThreadId = { openThreadRef ?. environmentId === environmentId ? openThreadRef . threadId : null }
91101 onNotification = { onNotification }
92102 />
93103 ) ) ;
@@ -102,9 +112,11 @@ interface NotificationState {
102112
103113function EnvironmentNotifications ( {
104114 environmentId,
115+ openThreadId,
105116 onNotification,
106117} : {
107118 environmentId : EnvironmentId ;
119+ openThreadId : ThreadId | null ;
108120 onNotification : ( environmentId : EnvironmentId , notification : Notification ) => void ;
109121} ) {
110122 const shell = useAtomValue ( environmentShell . stateValueAtom ( environmentId ) ) ;
@@ -115,10 +127,14 @@ function EnvironmentNotifications({
115127 ( settings ) => settings . inAppNotificationsEnabled ,
116128 ) ;
117129 const navigate = useNavigate ( ) ;
118- const { environmentId : activeEnvironmentId , threadId : activeThreadId } = useParams ( {
119- strict : false ,
120- } ) ;
121130 const previous = useRef ( new Map < ThreadId , NotificationState > ( ) ) ;
131+ const toasts = useRef ( new Map < ThreadId , ReturnType < typeof toastManager . add > [ ] > ( ) ) ;
132+
133+ useEffect ( ( ) => {
134+ if ( openThreadId === null ) return ;
135+ for ( const toastId of toasts . current . get ( openThreadId ) ?? [ ] ) toastManager . close ( toastId ) ;
136+ toasts . current . delete ( openThreadId ) ;
137+ } , [ openThreadId ] ) ;
122138
123139 useEffect ( ( ) => {
124140 if ( threads === null ) {
@@ -180,7 +196,7 @@ function EnvironmentNotifications({
180196 inAppNotificationsEnabled &&
181197 document . visibilityState === "visible" &&
182198 document . hasFocus ( ) &&
183- ( activeEnvironmentId !== environmentId || activeThreadId !== thread . id )
199+ openThreadId !== thread . id
184200 ) {
185201 const toastId = toastManager . add ( {
186202 type : kind === "completion" ? "success" : status === "failed" ? "error" : "warning" ,
@@ -210,6 +226,7 @@ function EnvironmentNotifications({
210226 } ,
211227 } ,
212228 } ) ;
229+ toasts . current . set ( thread . id , [ ...( toasts . current . get ( thread . id ) ?? [ ] ) , toastId ] ) ;
213230 continue ;
214231 }
215232 if (
@@ -241,13 +258,12 @@ function EnvironmentNotifications({
241258 }
242259 previous . current = next ;
243260 } , [
244- activeEnvironmentId ,
245- activeThreadId ,
246261 environmentId ,
247262 inAppNotificationsEnabled ,
248263 mode ,
249264 navigate ,
250265 onNotification ,
266+ openThreadId ,
251267 threads ,
252268 ] ) ;
253269
0 commit comments