Skip to content

Commit 78c35b4

Browse files
fix(web): thread toasts close when their thread opens
A completion or attention toast stayed on screen after its thread was opened another way (sidebar, keyboard, palette, navigation). The coordinator now remembers the toasts it shows per thread and closes them once the open thread matches, resolving the open thread like BackgroundThreadAnnouncer so a promoted draft route counts as its server thread. The same resolution suppresses toasts for the open thread. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4e27243 commit 78c35b4

2 files changed

Lines changed: 60 additions & 10 deletions

File tree

‎apps/web/src/components/ThreadNotificationCoordinator.badge.browser.test.tsx‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RegistryContext, useAtomValue } from "@effect/atom-react";
2-
import { EnvironmentId } from "@t3tools/contracts";
2+
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
3+
import { EnvironmentId, ProjectId, ThreadId } from "@t3tools/contracts";
34
import * as DateTime from "effect/DateTime";
45
import * as Option from "effect/Option";
56
import { Atom, AtomRegistry } from "effect/reactivity";
@@ -12,7 +13,7 @@ const state = vi.hoisted(() => ({
1213
inApp: false,
1314
toast: vi.fn(),
1415
closeToast: vi.fn(),
15-
open: (_params: { environmentId: string; threadId: string }) => {},
16+
open: (_params: { environmentId: string; threadId: string } | { draftId: string }) => {},
1617
shells: null as unknown as { set: (id: string, value: unknown) => void },
1718
navigate: vi.fn(),
1819
sound: vi.fn(),
@@ -22,7 +23,10 @@ const state = vi.hoisted(() => ({
2223
vi.mock(import("@tanstack/react-router"), async (original) => ({
2324
...(await original()),
2425
useNavigate: (() => state.navigate) as never,
25-
useParams: (() => useAtomValue(paramsAtom)) as never,
26+
useParams: ((options?: { select?: (params: object) => unknown }) => {
27+
const params = useAtomValue(paramsAtom);
28+
return options?.select ? options.select(params) : params;
29+
}) as never,
2630
}));
2731
vi.mock(import("./ui/toast"), async (original) => {
2832
const actual = await original();
@@ -63,6 +67,12 @@ vi.mock(import("../threadNotifications"), async (original) => ({
6367
setNotificationBadge: state.badge,
6468
}));
6569

70+
import {
71+
clearComposerDraftsEnvironment,
72+
DraftId,
73+
markPromotedDraftThreadByRef,
74+
useComposerDraftStore,
75+
} from "../composerDraftStore";
6676
import { ThreadNotificationCoordinator } from "./ThreadNotificationCoordinator";
6777

6878
let registry: AtomRegistry.AtomRegistry;
@@ -287,3 +297,27 @@ it("keeps toasts open for threads that were not opened", async () => {
287297
await app.render();
288298
expect(state.closeToast.mock.calls).toEqual([["Thread completed:Test thread"]]);
289299
});
300+
301+
it("closes the toast when a promoted draft route shows its thread", async () => {
302+
await using app = setup();
303+
const environmentId = EnvironmentId.make("one");
304+
const threadRef = scopeThreadRef(environmentId, ThreadId.make("thread"));
305+
useComposerDraftStore
306+
.getState()
307+
.setProjectDraftThreadId(
308+
scopeProjectRef(environmentId, ProjectId.make("project")),
309+
DraftId.make("draft"),
310+
{ threadId: threadRef.threadId },
311+
);
312+
markPromotedDraftThreadByRef(threadRef);
313+
using _drafts = { [Symbol.dispose]: () => clearComposerDraftsEnvironment(environmentId) };
314+
state.inApp = true;
315+
focused = true;
316+
state.toast.mockReturnValue("toast");
317+
await app.render();
318+
state.shells.set("one", shell(completed));
319+
await app.render();
320+
state.open({ draftId: "draft" });
321+
await app.render();
322+
expect(state.closeToast).toHaveBeenCalledWith("toast");
323+
});

‎apps/web/src/components/ThreadNotificationCoordinator.tsx‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from "@t3tools/client-runtime/state/models";
55
import { pendingSubagentRequestsByThread } from "@t3tools/client-runtime/state/threads";
66
import { useAtomValue } from "@effect/atom-react";
7-
import { useNavigate, useParams } from "@tanstack/react-router";
7+
import { useNavigate } from "@tanstack/react-router";
88
import type { EnvironmentId, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts";
99
import * as Option from "effect/Option";
1010
import {
@@ -15,7 +15,9 @@ import {
1515
} from "lucide-react";
1616
import { useEffect, useRef } from "react";
1717

18+
import { useComposerDraftStore } from "../composerDraftStore";
1819
import { getClientSettings, useClientSettings } from "../hooks/useSettings";
20+
import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
1921
import { useEnvironmentIds } from "../state/environments";
2022
import { environmentShell } from "../state/shell";
2123
import {
@@ -26,6 +28,7 @@ import {
2628
threadNotificationTag,
2729
unlockNotificationAudio,
2830
} from "../threadNotifications";
31+
import { resolveActiveThreadRouteRef } from "../threadRoutes";
2932
import { resolveSidebarThreadStatus } from "./Sidebar.logic";
3033
import { 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

103113
function 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

Comments
 (0)