Skip to content

Commit bba1c13

Browse files
fix(web): opening a thread dismisses its notification toasts (#24313)
1 parent 5f82942 commit bba1c13

2 files changed

Lines changed: 80 additions & 12 deletions

File tree

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

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const state = vi.hoisted(() => ({
1212
mode: "notifications",
1313
inApp: false,
1414
toast: vi.fn(),
15+
closeToast: vi.fn(),
1516
shells: null as unknown as { set: (id: string, value: unknown) => void },
1617
navigate: vi.fn(),
1718
sound: vi.fn(),
@@ -20,7 +21,14 @@ const state = vi.hoisted(() => ({
2021
}));
2122
vi.mock(import("./ui/toast"), async (original) => {
2223
const actual = await original();
23-
return { ...actual, toastManager: { ...actual.toastManager, add: state.toast as never } };
24+
return {
25+
...actual,
26+
toastManager: {
27+
...actual.toastManager,
28+
add: state.toast as never,
29+
close: state.closeToast as never,
30+
},
31+
};
2432
});
2533
vi.mock(import("../state/shell"), async (original) => {
2634
const actual = await original();
@@ -206,15 +214,25 @@ function setup() {
206214
.spyOn(document, "visibilityState", "get")
207215
.mockImplementation(() => visibility as DocumentVisibilityState),
208216
);
217+
const routeListeners = new Set<() => void>();
218+
const route = { params: {} as Record<string, string> };
209219
const router = {
210-
state: { matches: [{ params: {} }] },
220+
state: { matches: [route] },
211221
navigate: state.navigate,
222+
subscribe: (_event: string, listener: () => void) => {
223+
routeListeners.add(listener);
224+
return () => routeListeners.delete(listener);
225+
},
212226
} as unknown as AnyRouter;
213227
return {
214228
start: () => {
215229
stack.defer(startThreadNotifications(testRegistry, router));
216230
},
217231
setEnvironmentIds: (ids: ReadonlyArray<string>) => testRegistry.set(environmentIdsAtom, ids),
232+
openThread: (environmentId: string, threadId: string) => {
233+
route.params = { environmentId, threadId };
234+
for (const listener of routeListeners) listener();
235+
},
218236
changeSettings: () => {
219237
for (const listener of state.settingsListeners) listener();
220238
},
@@ -400,3 +418,38 @@ it.each([false, true])(
400418
expect(TestNotification.sent.map(({ title }) => title)).toEqual(["Thread completed"]);
401419
},
402420
);
421+
422+
it("opening a thread closes only its in-app toasts, and disabling notifications closes the rest", async () => {
423+
await using app = setup();
424+
state.inApp = true;
425+
focused = true;
426+
state.toast
427+
.mockReturnValueOnce("one-completed")
428+
.mockReturnValueOnce("one-approval")
429+
.mockReturnValueOnce("one-other")
430+
.mockReturnValueOnce("two-completed");
431+
state.shells.set("one", shell({}, { id: "other" }));
432+
app.start();
433+
const completed = {
434+
latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:01:00Z" },
435+
};
436+
state.shells.set("one", shell(completed, { id: "other" }));
437+
state.shells.set("one", shell({ ...completed, hasPendingApprovals: true }, { id: "other" }));
438+
state.shells.set(
439+
"one",
440+
shell({ ...completed, hasPendingApprovals: true }, { ...completed, id: "other" }),
441+
);
442+
state.shells.set("two", shell(completed));
443+
expect(state.toast).toHaveBeenCalledTimes(4);
444+
445+
app.openThread("one", "thread");
446+
expect(state.closeToast.mock.calls).toEqual([["one-completed"], ["one-approval"]]);
447+
448+
state.mode = "off";
449+
state.inApp = false;
450+
app.changeSettings();
451+
expect(state.closeToast.mock.calls.slice(2).flat().toSorted()).toEqual([
452+
"one-other",
453+
"two-completed",
454+
]);
455+
});

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)