Skip to content

Commit 25d2239

Browse files
fix(web): screen readers hear when a thread is pinned or an action is undone (pingdotgg#25901)
1 parent 9b98bd0 commit 25d2239

4 files changed

Lines changed: 63 additions & 8 deletions

File tree

‎apps/web/src/hooks/showThreadUndoNotice.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Cause } from "effect";
12
import { AsyncResult } from "effect/reactivity";
23
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
34

@@ -88,4 +89,24 @@ describe("thread undo notice", () => {
8889
await notice().undo();
8990
expect(cancelAnnouncement).toHaveBeenCalledWith("thread-undo-notice");
9091
});
92+
93+
it("announces that the undo happened, naming the undone action", async () => {
94+
const { options } = setup();
95+
showThreadUndoNotice(options);
96+
vi.mocked(announce).mockClear();
97+
await notice().undo();
98+
expect(announce).toHaveBeenCalledExactlyOnceWith("Undid unpin of 1 thread");
99+
});
100+
101+
it("does not announce an undo that failed", async () => {
102+
const { options, add } = setup();
103+
showThreadUndoNotice({
104+
...options,
105+
undo: async () => AsyncResult.failure(Cause.fail(new Error("offline"))),
106+
});
107+
vi.mocked(announce).mockClear();
108+
await notice().undo();
109+
expect(add).toHaveBeenCalledOnce();
110+
expect(announce).not.toHaveBeenCalled();
111+
});
91112
});

‎apps/web/src/hooks/showThreadUndoNotice.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,21 @@ type UndoNotice = {
2828

2929
export const useThreadUndoNotice = create<{ notice: UndoNotice | null }>(() => ({ notice: null }));
3030

31+
const UNDONE_ACTION = {
32+
Settled: "settle",
33+
Snoozed: "snooze",
34+
Unpinned: "unpin",
35+
Archived: "archive",
36+
Discarded: "discard",
37+
} satisfies Record<UndoOptions["action"], string>;
38+
39+
function countLabel(action: UndoOptions["action"], count: number) {
40+
const noun = action === "Discarded" ? "draft" : "thread";
41+
return `${count} ${noun}${count === 1 ? "" : "s"}`;
42+
}
43+
3144
function threadUndoNoticeSummary(notice: Pick<UndoNotice, "action" | "count">): string {
32-
const noun = notice.action === "Discarded" ? "draft" : "thread";
33-
return `${notice.action} ${notice.count} ${noun}${notice.count === 1 ? "" : "s"}`;
45+
return `${notice.action} ${countLabel(notice.action, notice.count)}`;
3446
}
3547

3648
export function threadUndoLabel(keybindings: Parameters<typeof shortcutLabelForCommand>[0]) {
@@ -87,6 +99,7 @@ function refreshNotice() {
8799
// Claim before awaiting to prevent repeated actions restoring a group twice.
88100
for (const entry of current) entry.claim.finish();
89101
refreshNotice();
102+
let undone = 0;
90103
await Promise.all(
91104
current.map(async ({ undo, failureTitle }) => {
92105
const reportFailure = (error: unknown) => {
@@ -100,14 +113,19 @@ function refreshNotice() {
100113
};
101114
try {
102115
const result = await undo();
103-
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
116+
if (result._tag === "Success") undone++;
117+
else if (!isAtomCommandInterrupted(result)) {
104118
reportFailure(squashAtomCommandFailure(result));
105119
}
106120
} catch (error) {
107121
reportFailure(error);
108122
}
109123
}),
110124
);
125+
// Failures already raise a toast, so only what was actually restored is spoken.
126+
if (undone > 0) {
127+
announce(`Undid ${UNDONE_ACTION[latest.action]} of ${countLabel(latest.action, undone)}`);
128+
}
111129
},
112130
});
113131
}

‎apps/web/src/hooks/useThreadActions.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { vcsEnvironment } from "../state/vcs";
3535
import { useNewThreadHandler } from "./useHandleNewThread";
3636
import { refreshArchivedThreadsForEnvironment } from "../lib/archivedThreadsState";
3737
import { releaseComposerDraftUploads } from "../lib/composerDraftUploads";
38+
import { announce } from "../lib/announce";
3839
import { readLocalApi } from "../localApi";
3940
import {
4041
readEnvironmentServerConfigLoaded,
@@ -727,7 +728,7 @@ export function useThreadActions() {
727728
});
728729
};
729730

730-
const pinThread = async (target: ScopedThreadRef, opts: { orderKey?: string } = {}) => {
731+
const sendPin = async (target: ScopedThreadRef, opts: { orderKey?: string } = {}) => {
731732
if (!readEnvironmentServerConfigLoaded(target.environmentId)) {
732733
return AsyncResult.failure(
733734
Cause.fail(
@@ -752,6 +753,12 @@ export function useThreadActions() {
752753
});
753754
};
754755

756+
const pinThread = async (target: ScopedThreadRef, opts: { orderKey?: string } = {}) => {
757+
const result = await sendPin(target, opts);
758+
if (result._tag === "Success") announce("Pinned 1 thread");
759+
return result;
760+
};
761+
755762
const unpinThread = async (target: ScopedThreadRef) => {
756763
if (!readEnvironmentServerConfigLoaded(target.environmentId)) {
757764
return AsyncResult.failure(
@@ -947,10 +954,7 @@ export function useThreadActions() {
947954
case "unsnooze":
948955
return unsnoozeThread(target);
949956
case "pin":
950-
return pinThread(
951-
target,
952-
step.orderKey === undefined ? {} : { orderKey: step.orderKey },
953-
);
957+
return sendPin(target, step.orderKey === undefined ? {} : { orderKey: step.orderKey });
954958
case "snooze":
955959
return snoozeThreadMutation({
956960
environmentId: target.environmentId,

‎apps/web/src/hooks/useThreadActions.undo.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useThreadActions } from "./useThreadActions";
66
import { threadEnvironment } from "../state/threads";
77
import { toastManager } from "../components/ui/toast";
88
import { useThreadUndoNotice } from "./showThreadUndoNotice";
9+
import { announce } from "../lib/announce";
910

1011
const commands = vi.hoisted(() => ({
1112
pin: vi.fn(),
@@ -44,6 +45,7 @@ vi.mock("../composerDraftStore", () => ({ useComposerDraftStore: () => vi.fn() }
4445
vi.mock("../lib/composerDraftUploads", () => ({ releaseComposerDraftUploads: vi.fn() }));
4546
vi.mock("../terminalUiStateStore", () => ({ useTerminalUiStateStore: () => vi.fn() }));
4647
vi.mock("../uiStateStore", () => ({ useUiStateStore: () => vi.fn() }));
48+
vi.mock("../lib/announce", () => ({ announce: vi.fn(), cancelAnnouncement: vi.fn() }));
4749
vi.mock("../lib/archivedThreadsState", () => ({ refreshArchivedThreadsForEnvironment: vi.fn() }));
4850
const threadShell = vi.hoisted(() => ({
4951
title: "Thread",
@@ -139,12 +141,22 @@ describe("concurrent thread action Undo", () => {
139141
);
140142
});
141143

144+
describe("pin", () => {
145+
it("announces that the thread was pinned", async () => {
146+
vi.mocked(announce).mockClear();
147+
await useThreadActions().pinThread(target);
148+
expect(announce).toHaveBeenCalledExactlyOnceWith("Pinned 1 thread");
149+
});
150+
});
151+
142152
describe("unpin Undo", () => {
143153
it("restores the original snooze after restoring the pin", async () => {
144154
threadShell.pinnedAt = "2026-01-01T00:00:00.000Z";
145155
threadShell.snoozedUntil = "2030-01-01T09:00:00.000Z";
146156
await useThreadActions().unpinThread(target);
157+
vi.mocked(announce).mockClear();
147158
await currentUndo()();
159+
expect(announce).not.toHaveBeenCalledWith("Pinned 1 thread");
148160
expect(commands.pin).toHaveBeenCalledExactlyOnceWith({
149161
environmentId: target.environmentId,
150162
input: { threadId: target.threadId, orderKey: "a0" },

0 commit comments

Comments
 (0)