Skip to content

Commit 619936a

Browse files
committed
fix(web): mod+z undoes thread actions from the composer
The sidebar notice offers mod+z to undo a settle, snooze, archive or unpin, but the shortcut skipped focused text fields, and the composer is almost always focused. Settling the open thread even moves focus into the next thread's composer. A new notice now claims mod+z from text fields until the user presses another key or edits text. The shortcut listens in the capture phase so the editor's own undo cannot consume the key first, and it moves next to the sidebar so it works on every route that shows the notice. Fixes #16159
1 parent b12b3f7 commit 619936a

6 files changed

Lines changed: 89 additions & 13 deletions

File tree

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import {
99
} from "react";
1010
import { useLocation, useNavigate, useParams } from "@tanstack/react-router";
1111

12+
import { isCommandPaletteOpen } from "../commandPaletteBus";
1213
import { isElectron } from "../env";
14+
import {
15+
releaseThreadUndoShortcut,
16+
threadUndoClaimsShortcut,
17+
undoLatestThreadAction,
18+
} from "../hooks/showThreadUndoNotice";
1319
import { getLocalStorageItem, removeLocalStorageItem } from "../hooks/useLocalStorage";
1420
import {
1521
isRichTextBoldShortcut,
@@ -208,6 +214,48 @@ function NavigationHistoryShortcuts() {
208214
return null;
209215
}
210216

217+
const MODIFIER_KEYS = new Set(["Alt", "Control", "Meta", "Shift"]);
218+
219+
// Undoes the thread actions in the sidebar's notice. Mounted wherever the
220+
// thread sidebar shows, so it never acts on a notice the user cannot see. Listens in the capture phase so mod+z reaches it before the composer's
221+
// own undo handler consumes the key.
222+
function ThreadUndoShortcut() {
223+
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
224+
225+
useEffect(() => {
226+
const onKeyDown = (event: KeyboardEvent) => {
227+
const command = resolveShortcutCommand(event, keybindings, {
228+
context: {
229+
terminalFocus: isTerminalFocused(),
230+
previewFocus: isPreviewFocused(),
231+
editableFocus: isEditableFocused(event.target) && !threadUndoClaimsShortcut(),
232+
modelPickerOpen: isModelPickerOpen(),
233+
},
234+
});
235+
if (command !== "thread.undo") {
236+
if (!MODIFIER_KEYS.has(event.key)) releaseThreadUndoShortcut();
237+
return;
238+
}
239+
if (event.repeat || isCommandPaletteOpen() || isModelPickerOpen()) return;
240+
if (undoLatestThreadAction()) {
241+
event.preventDefault();
242+
event.stopPropagation();
243+
}
244+
};
245+
// Pastes from a menu, dictation and drops edit text without a keypress.
246+
const onInput = () => releaseThreadUndoShortcut();
247+
248+
window.addEventListener("keydown", onKeyDown, true);
249+
window.addEventListener("input", onInput, true);
250+
return () => {
251+
window.removeEventListener("keydown", onKeyDown, true);
252+
window.removeEventListener("input", onInput, true);
253+
};
254+
}, [keybindings]);
255+
256+
return null;
257+
}
258+
211259
// Settings swaps the thread sidebar out of the tree. Keep the lightweight
212260
// project projection subscribed so returning to a draft never renders the
213261
// zero-project state while the environment snapshot reconnects.
@@ -338,6 +386,7 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
338386
{children}
339387
<SidebarControl />
340388
<NavigationHistoryShortcuts />
389+
{isOnSettings ? null : <ThreadUndoShortcut />}
341390
<MainAppLocationTracker />
342391
</SidebarProvider>
343392
</PanelAnimationSuppressionProvider>

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"
44

55
import { toastManager } from "../components/ui/toast";
66
import {
7+
releaseThreadUndoShortcut,
78
showThreadUndoNotice,
9+
threadUndoClaimsShortcut,
810
undoLatestThreadAction,
911
useThreadUndoNotice,
1012
} from "./showThreadUndoNotice";
@@ -149,4 +151,24 @@ describe("thread undo notice", () => {
149151
expect(older).toHaveBeenCalledOnce();
150152
expect(undoLatestThreadAction()).toBe(false);
151153
});
154+
it("claims mod+z from text fields until the user types, then again for a new notice", () => {
155+
const { options } = setup();
156+
expect(threadUndoClaimsShortcut()).toBe(false);
157+
showThreadUndoNotice(options);
158+
expect(threadUndoClaimsShortcut()).toBe(true);
159+
releaseThreadUndoShortcut();
160+
expect(threadUndoClaimsShortcut()).toBe(false);
161+
showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/next") });
162+
expect(threadUndoClaimsShortcut()).toBe(true);
163+
});
164+
165+
it("stops claiming mod+z once nothing is left to undo", async () => {
166+
const { options } = setup();
167+
showThreadUndoNotice(options);
168+
await notice().undo();
169+
expect(threadUndoClaimsShortcut()).toBe(false);
170+
showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/expiring") });
171+
vi.advanceTimersByTime(5_000);
172+
expect(threadUndoClaimsShortcut()).toBe(false);
173+
});
152174
});

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export const useThreadUndoNotice = create<{ notice: UndoNotice | null }>(() => (
2929
// same kind share one notice and can be restored together.
3030
let liveUndos: UndoOptions[] = [];
3131
let expiry: ReturnType<typeof setTimeout> | undefined;
32+
// A new notice owns mod+z even inside a text field, until the user presses
33+
// another key or edits text. After that, mod+z is the field's own undo again.
34+
let shortcutClaimed = false;
3235

3336
function refreshNotice() {
3437
const stale = liveUndos.filter(({ claim }) => !claim.isCurrent());
@@ -95,10 +98,21 @@ export function undoLatestThreadAction(): boolean {
9598
return true;
9699
}
97100

101+
/** Whether mod+z should undo the notice's actions even while a text field is focused. */
102+
export function threadUndoClaimsShortcut(): boolean {
103+
return shortcutClaimed && useThreadUndoNotice.getState().notice !== null;
104+
}
105+
106+
/** Called on any other keypress or text edit; hands mod+z back to text fields. */
107+
export function releaseThreadUndoShortcut() {
108+
shortcutClaimed = false;
109+
}
110+
98111
/** Shows one compact confirmation for the currently undoable thread actions. */
99112
export function showThreadUndoNotice(options: UndoOptions) {
100113
if (!options.claim.isCurrent()) return;
101114
liveUndos.push(options);
115+
shortcutClaimed = true;
102116
refreshNotice();
103117
clearTimeout(expiry);
104118
expiry = setTimeout(() => {

‎apps/web/src/routes/_chat.tsx‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { isPreviewFocused } from "../lib/previewFocus";
1919
import { isTerminalFocused } from "../lib/terminalFocus";
2020
import { isEditableFocused } from "../lib/editableFocus";
2121
import { isModelPickerOpen } from "../modelPickerVisibility";
22-
import { undoLatestThreadAction } from "../hooks/showThreadUndoNotice";
2322
import { resolveShortcutCommand } from "../keybindings";
2423
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
2524
import { isPreviewSupportedInRuntime } from "../previewStateStore";
@@ -80,15 +79,6 @@ function ChatRouteGlobalShortcuts() {
8079
return;
8180
}
8281

83-
if (command === "thread.undo") {
84-
if (event.repeat || isModelPickerOpen()) return;
85-
if (undoLatestThreadAction()) {
86-
event.preventDefault();
87-
event.stopPropagation();
88-
}
89-
return;
90-
}
91-
9282
if (event.key === "Escape" && selectedThreadKeysSize > 0) {
9383
event.preventDefault();
9484
clearSelection();

‎docs/user/keybindings.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,9 @@ shortcut; assign one in **Settings → Keybindings**.
127127
bottom of the sidebar, such as unpin, settle, snooze, archive, or discarding a
128128
draft. Consecutive
129129
actions of the same kind undo together. The notice remains available for five
130-
seconds after the latest action. The default shortcut skips text fields and
131-
terminals so native undo keeps working there.
130+
seconds after the latest action. It also works while a text field is focused,
131+
until you type there; after that, the shortcut undoes your typing as usual.
132+
Terminals always keep their native shortcut.
132133

133134
`navigation.back` (`mod+[` by default) and `navigation.forward` (`mod+]`) move
134135
through the pages you have visited, like a browser's back and forward buttons.

‎docs/user/thread-sidebar.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ On web and desktop, unpinning, settling, snoozing, and archiving a thread each s
5151
a notification with **Undo** for five seconds. Undo restores the thread's previous
5252
state, including its pinned position, and reopens an archived thread you were
5353
viewing. Discarding an unsent draft from the sidebar works the same way: Undo brings
54-
back its text and attachments. `mod+z` triggers the most recent Undo when no text field is focused; see
54+
back its text and attachments. `mod+z` triggers the most recent Undo, even from the composer until you type in it; see
5555
[Keybindings](./keybindings.md#commands-with-special-behavior).
5656

5757
On web and desktop, you can also drag files from your computer onto any thread row:

0 commit comments

Comments
 (0)