Skip to content

Commit fdfe51f

Browse files
committed
feat(web): share all thread alerts with in-app toasts
1 parent 658e025 commit fdfe51f

3 files changed

Lines changed: 68 additions & 22 deletions

File tree

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const state = vi.hoisted(() => ({
1414
completedAt: null as string | null,
1515
archivedAt: null as string | null,
1616
input: false,
17+
approval: false,
18+
sessionError: false,
19+
turnError: false,
1720
add: vi.fn(
1821
(_toast: { title: string; description: string; actionProps: { onClick: () => void } }) =>
1922
"toast-1",
@@ -34,9 +37,11 @@ vi.mock("@effect/atom-react", () => ({
3437
title: "Fix the login form",
3538
archivedAt: state.archivedAt,
3639
hasPendingUserInput: state.input,
40+
hasPendingApprovals: state.approval,
41+
session: state.sessionError ? { status: "error" } : null,
3742
latestTurn: {
3843
turnId: "turn-1",
39-
state: state.completedAt ? "completed" : "running",
44+
state: state.turnError ? "error" : state.completedAt ? "completed" : "running",
4045
completedAt: state.completedAt,
4146
},
4247
},
@@ -98,6 +103,9 @@ beforeEach(() => {
98103
completedAt: null,
99104
archivedAt: null,
100105
input: false,
106+
approval: false,
107+
sessionError: false,
108+
turnError: false,
101109
});
102110
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
103111
vi.stubGlobal("document", {
@@ -135,21 +143,51 @@ describe("thread notifications", () => {
135143
expect(state.notification).not.toHaveBeenCalled();
136144
});
137145

138-
it.each(["active", "blurred", "hidden", "archived", "input", "disabled"])(
146+
it.each(["active", "blurred", "hidden", "archived", "disabled"])(
139147
"does not show a completion toast for %s threads",
140148
async (condition) => {
141149
await render();
142150
if (condition === "active") state.active.threadId = "thread-1";
143151
if (condition === "blurred") state.focused = false;
144152
if (condition === "hidden") state.visible = "hidden";
145153
if (condition === "archived") state.archivedAt = "2026-09-13T09:00:00.000Z";
146-
if (condition === "input") state.input = true;
147154
if (condition === "disabled") state.inApp = false;
148155
await complete();
149156
expect(state.add).not.toHaveBeenCalled();
150157
},
151158
);
152159

160+
it.each([
161+
["input", "Input needed"],
162+
["approval", "Approval needed"],
163+
["sessionError", "Thread failed"],
164+
["turnError", "Thread failed"],
165+
] as const)("uses the same %s event for in-app and desktop alerts", async (event, title) => {
166+
state.mode = "notifications-and-sound";
167+
await render();
168+
state[event] = true;
169+
await render();
170+
await render();
171+
expect(state.add).toHaveBeenCalledTimes(1);
172+
expect(state.add).toHaveBeenLastCalledWith(expect.objectContaining({ title }));
173+
expect(state.sound).toHaveBeenCalledWith("input", expect.any(Function));
174+
expect(state.notification).not.toHaveBeenCalled();
175+
176+
state[event] = false;
177+
await render();
178+
state.focused = false;
179+
state[event] = true;
180+
await render();
181+
await render();
182+
expect(state.add).toHaveBeenCalledTimes(1);
183+
expect(state.notification).toHaveBeenCalledTimes(1);
184+
expect(state.notification).toHaveBeenCalledWith(title, {
185+
body: "Fix the login form",
186+
tag: "env-1:thread-1",
187+
silent: true,
188+
});
189+
});
190+
153191
it("keeps desktop alerts when in-app notifications are disabled", async () => {
154192
state.inApp = false;
155193
state.mode = "notifications";

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,22 @@ function EnvironmentNotifications({ environmentId }: { environmentId: Environmen
5353
const { environmentId: activeEnvironmentId, threadId: activeThreadId } = useParams({
5454
strict: false,
5555
});
56-
const previous = useRef(new Map<ThreadId, { input: string | null; completion: number | null }>());
56+
const previous = useRef(
57+
new Map<ThreadId, { attention: string | null; completion: number | null }>(),
58+
);
5759

5860
useEffect(() => {
5961
if (shell.status !== "live" || Option.isNone(shell.snapshot)) {
6062
previous.current.clear();
6163
return;
6264
}
63-
const next = new Map<ThreadId, { input: string | null; completion: number | null }>();
65+
const next = new Map<ThreadId, { attention: string | null; completion: number | null }>();
6466
for (const thread of shell.snapshot.value.threads) {
65-
const status = resolveSidebarThreadStatus(thread);
67+
let status = resolveSidebarThreadStatus(thread);
68+
if (status === "ready" && thread.latestTurn?.state === "error") status = "failed";
6669
const prior = previous.current.get(thread.id);
67-
const input =
68-
status === "input" || status === "approval"
70+
const attention =
71+
status === "input" || status === "approval" || status === "failed"
6972
? `${thread.latestTurn?.turnId ?? ""}:${status}`
7073
: null;
7174
const completedAt = Date.parse(thread.latestTurn?.completedAt ?? "");
@@ -75,31 +78,39 @@ function EnvironmentNotifications({ environmentId }: { environmentId: Environmen
7578
Number.isFinite(completedAt)
7679
? completedAt
7780
: (prior?.completion ?? null);
78-
next.set(thread.id, { input, completion });
81+
next.set(thread.id, { attention, completion });
7982
if (!prior || thread.archivedAt !== null) continue;
8083
const kind =
81-
input && input !== prior.input
84+
attention && attention !== prior.attention
8285
? "input"
8386
: completion !== null && (prior.completion === null || completion > prior.completion)
8487
? "completion"
8588
: null;
8689
if (!kind) continue;
90+
const title =
91+
kind === "completion"
92+
? "Thread completed"
93+
: status === "approval"
94+
? "Approval needed"
95+
: status === "failed"
96+
? "Thread failed"
97+
: "Input needed";
8798
if (hasNotificationSound(mode)) {
8899
void playNotificationSound(kind, () =>
89100
hasNotificationSound(getClientSettings().notificationMode),
90101
);
91102
}
92103
if (
93104
inAppNotificationsEnabled &&
94-
kind === "completion" &&
95105
document.visibilityState === "visible" &&
96106
document.hasFocus() &&
97107
(activeEnvironmentId !== environmentId || activeThreadId !== thread.id)
98108
) {
99109
const toastId = toastManager.add({
100-
type: "success",
101-
title: "Thread completed",
110+
type: kind === "completion" ? "success" : status === "failed" ? "error" : "warning",
111+
title,
102112
description: thread.title,
113+
data: { hideCopyButton: true },
103114
actionProps: {
104115
children: "Open thread",
105116
onClick: () => {
@@ -120,14 +131,11 @@ function EnvironmentNotifications({ environmentId }: { environmentId: Environmen
120131
)
121132
continue;
122133
try {
123-
const notification = new Notification(
124-
kind === "completion"
125-
? "Thread completed"
126-
: status === "approval"
127-
? "Approval needed"
128-
: "Input needed",
129-
{ body: thread.title, tag: `${environmentId}:${thread.id}`, silent: true },
130-
);
134+
const notification = new Notification(title, {
135+
body: thread.title,
136+
tag: `${environmentId}:${thread.id}`,
137+
silent: true,
138+
});
131139
notification.addEventListener("click", () => {
132140
notification.close();
133141
window.focus();

‎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-
"System alerts when a thread finishes or needs input. Applies to this device while T3 Code is open."
25+
"System alerts when a thread finishes, fails, or needs input or approval. Applies to this device while T3 Code is open."
2626
}
2727
control={
2828
<Select

0 commit comments

Comments
 (0)