Skip to content

Commit 6e22847

Browse files
committed
feat(web): show completion toasts for inactive threads
1 parent 20363c3 commit 6e22847

3 files changed

Lines changed: 215 additions & 6 deletions

File tree

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
import type { ClientSettings } from "@t3tools/contracts/settings";
2+
import * as Option from "effect/Option";
3+
import { act } from "react";
4+
import { create, type ReactTestRenderer } from "react-test-renderer";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
6+
7+
const state = vi.hoisted(() => ({
8+
mode: "off" as ClientSettings["notificationMode"],
9+
active: { environmentId: "env-1", threadId: "other-thread" },
10+
focused: true,
11+
visible: "visible",
12+
live: true,
13+
completedAt: null as string | null,
14+
archivedAt: null as string | null,
15+
input: false,
16+
add: vi.fn(
17+
(_toast: { title: string; description: string; actionProps: { onClick: () => void } }) =>
18+
"toast-1",
19+
),
20+
close: vi.fn(),
21+
navigate: vi.fn(),
22+
sound: vi.fn(),
23+
notification: vi.fn(function () {}),
24+
}));
25+
26+
vi.mock("@effect/atom-react", () => ({
27+
useAtomValue: () => ({
28+
status: state.live ? "live" : "disconnected",
29+
snapshot: Option.some({
30+
threads: [
31+
{
32+
id: "thread-1",
33+
title: "Fix the login form",
34+
archivedAt: state.archivedAt,
35+
hasPendingUserInput: state.input,
36+
latestTurn: {
37+
turnId: "turn-1",
38+
state: state.completedAt ? "completed" : "running",
39+
completedAt: state.completedAt,
40+
},
41+
},
42+
],
43+
}),
44+
}),
45+
}));
46+
vi.mock("@tanstack/react-router", () => ({
47+
useNavigate: () => state.navigate,
48+
useParams: () => state.active,
49+
}));
50+
vi.mock("../hooks/useSettings", () => ({
51+
useClientSettings: (select: (settings: Pick<ClientSettings, "notificationMode">) => unknown) =>
52+
select({ notificationMode: state.mode }),
53+
getClientSettings: () => ({ notificationMode: state.mode }),
54+
}));
55+
vi.mock("../state/environments", () => ({
56+
useEnvironments: () => ({ environments: [{ environmentId: "env-1" }] }),
57+
}));
58+
vi.mock("../state/shell", () => ({
59+
environmentShell: { stateValueAtom: vi.fn() },
60+
}));
61+
vi.mock("../threadNotifications", async (importOriginal) => ({
62+
...(await importOriginal<typeof import("../threadNotifications")>()),
63+
playNotificationSound: state.sound,
64+
}));
65+
vi.mock("./ui/toast", () => ({
66+
toastManager: { add: state.add, close: state.close },
67+
}));
68+
69+
import { ThreadNotificationCoordinator } from "./ThreadNotificationCoordinator";
70+
71+
let renderer: ReactTestRenderer | undefined;
72+
73+
async function render() {
74+
await act(() => {
75+
if (renderer) renderer.update(<ThreadNotificationCoordinator />);
76+
else renderer = create(<ThreadNotificationCoordinator />);
77+
});
78+
}
79+
80+
async function complete() {
81+
state.completedAt = "2026-09-13T10:00:00.000Z";
82+
await render();
83+
}
84+
85+
beforeEach(() => {
86+
vi.clearAllMocks();
87+
Object.assign(state, {
88+
mode: "off",
89+
active: { environmentId: "env-1", threadId: "other-thread" },
90+
focused: true,
91+
visible: "visible",
92+
live: true,
93+
completedAt: null,
94+
archivedAt: null,
95+
input: false,
96+
});
97+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
98+
vi.stubGlobal("document", {
99+
get visibilityState() {
100+
return state.visible;
101+
},
102+
hasFocus: () => state.focused,
103+
addEventListener: vi.fn(),
104+
removeEventListener: vi.fn(),
105+
});
106+
vi.stubGlobal("Notification", Object.assign(state.notification, { permission: "granted" }));
107+
});
108+
109+
afterEach(async () => {
110+
await act(() => renderer?.unmount());
111+
renderer = undefined;
112+
vi.unstubAllGlobals();
113+
});
114+
115+
describe("thread completion toasts", () => {
116+
it("alerts once with system alerts off and opens the completed thread", async () => {
117+
await render();
118+
await complete();
119+
await render();
120+
expect(state.add).toHaveBeenCalledTimes(1);
121+
const toast = state.add.mock.calls[0]?.[0];
122+
expect(toast?.title).toBe("Thread completed");
123+
expect(toast?.description).toBe("Fix the login form");
124+
toast?.actionProps.onClick();
125+
expect(state.close).toHaveBeenCalledWith("toast-1");
126+
expect(state.navigate).toHaveBeenCalledWith({
127+
to: "/$environmentId/$threadId",
128+
params: { environmentId: "env-1", threadId: "thread-1" },
129+
});
130+
expect(state.notification).not.toHaveBeenCalled();
131+
});
132+
133+
it.each(["active", "blurred", "hidden", "archived", "input"])(
134+
"does not show a completion toast for %s threads",
135+
async (condition) => {
136+
await render();
137+
if (condition === "active") state.active.threadId = "thread-1";
138+
if (condition === "blurred") state.focused = false;
139+
if (condition === "hidden") state.visible = "hidden";
140+
if (condition === "archived") state.archivedAt = "2026-09-13T09:00:00.000Z";
141+
if (condition === "input") state.input = true;
142+
await complete();
143+
expect(state.add).not.toHaveBeenCalled();
144+
},
145+
);
146+
147+
it("compares the environment as well as the thread", async () => {
148+
state.active = { environmentId: "env-2", threadId: "thread-1" };
149+
await render();
150+
await complete();
151+
expect(state.add).toHaveBeenCalledTimes(1);
152+
});
153+
154+
it("does not replay completed threads on first load or reconnect", async () => {
155+
await complete();
156+
state.live = false;
157+
await render();
158+
state.live = true;
159+
await render();
160+
expect(state.add).not.toHaveBeenCalled();
161+
});
162+
163+
it("keeps sound but replaces the system popup when showing a toast", async () => {
164+
state.mode = "notifications-and-sound";
165+
await render();
166+
await complete();
167+
expect(state.sound).toHaveBeenCalledWith("completion", expect.any(Function));
168+
expect(state.add).toHaveBeenCalledTimes(1);
169+
expect(state.notification).not.toHaveBeenCalled();
170+
});
171+
172+
it("keeps system alerts when the app is in the background", async () => {
173+
state.mode = "notifications";
174+
state.focused = false;
175+
await render();
176+
await complete();
177+
expect(state.add).not.toHaveBeenCalled();
178+
expect(state.notification).toHaveBeenCalledWith("Thread completed", {
179+
body: "Fix the login form",
180+
tag: "env-1:thread-1",
181+
silent: true,
182+
});
183+
});
184+
});

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useAtomValue } from "@effect/atom-react";
2-
import { useNavigate } from "@tanstack/react-router";
2+
import { useNavigate, useParams } from "@tanstack/react-router";
33
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
44
import * as Option from "effect/Option";
55
import { useEffect, useRef } from "react";
@@ -14,6 +14,7 @@ import {
1414
unlockNotificationAudio,
1515
} from "../threadNotifications";
1616
import { resolveSidebarThreadStatus } from "./Sidebar.logic";
17+
import { toastManager } from "./ui/toast";
1718

1819
export function ThreadNotificationCoordinator() {
1920
const { environments } = useEnvironments();
@@ -29,8 +30,6 @@ export function ThreadNotificationCoordinator() {
2930
};
3031
}, [mode]);
3132

32-
if (mode === "off") return null;
33-
3433
return environments.map((environment) => (
3534
<EnvironmentNotifications
3635
key={environment.environmentId}
@@ -43,6 +42,9 @@ function EnvironmentNotifications({ environmentId }: { environmentId: Environmen
4342
const shell = useAtomValue(environmentShell.stateValueAtom(environmentId));
4443
const mode = useClientSettings((settings) => settings.notificationMode);
4544
const navigate = useNavigate();
45+
const { environmentId: activeEnvironmentId, threadId: activeThreadId } = useParams({
46+
strict: false,
47+
});
4648
const previous = useRef(new Map<ThreadId, { input: string | null; completion: number | null }>());
4749

4850
useEffect(() => {
@@ -66,7 +68,7 @@ function EnvironmentNotifications({ environmentId }: { environmentId: Environmen
6668
? completedAt
6769
: (prior?.completion ?? null);
6870
next.set(thread.id, { input, completion });
69-
if (!prior || mode === "off" || thread.archivedAt !== null) continue;
71+
if (!prior || thread.archivedAt !== null) continue;
7072
const kind =
7173
input && input !== prior.input
7274
? "input"
@@ -79,6 +81,29 @@ function EnvironmentNotifications({ environmentId }: { environmentId: Environmen
7981
hasNotificationSound(getClientSettings().notificationMode),
8082
);
8183
}
84+
if (
85+
kind === "completion" &&
86+
document.visibilityState === "visible" &&
87+
document.hasFocus() &&
88+
(activeEnvironmentId !== environmentId || activeThreadId !== thread.id)
89+
) {
90+
const toastId = toastManager.add({
91+
type: "success",
92+
title: "Thread completed",
93+
description: thread.title,
94+
actionProps: {
95+
children: "Open thread",
96+
onClick: () => {
97+
toastManager.close(toastId);
98+
void navigate({
99+
to: "/$environmentId/$threadId",
100+
params: { environmentId, threadId: thread.id },
101+
});
102+
},
103+
},
104+
});
105+
continue;
106+
}
82107
if (
83108
!hasDesktopNotifications(mode) ||
84109
typeof Notification === "undefined" ||
@@ -107,7 +132,7 @@ function EnvironmentNotifications({ environmentId }: { environmentId: Environmen
107132
}
108133
}
109134
previous.current = next;
110-
}, [environmentId, mode, navigate, shell]);
135+
}, [activeEnvironmentId, activeThreadId, environmentId, mode, navigate, shell]);
111136

112137
return null;
113138
}

‎apps/web/src/components/settings/NotificationSettings.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function NotificationSettings() {
2222
{...searchableSetting("thread-notifications")}
2323
description={
2424
permissionMessage ??
25-
"Alert when a thread finishes or needs input. Applies to this device while T3 Code is open."
25+
"System alerts when a thread finishes or needs input. Applies to this device while T3 Code is open."
2626
}
2727
control={
2828
<Select

0 commit comments

Comments
 (0)