The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 20:45 UTC
fix(web): ⌘Z undoes a settle even when the composer has focus (#22607)
Co-authored-by: ishaanko <ishaanko.mail@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Theo Browne committed
Oct 21, 2026 at 01:57 UTC
21da3061aa006fc852ea4bd84c31ec084a3afa26
6 files changed
+129
-24
apps/web/src/components/AppSidebarLayout.tsx
+83
-10
@@ -2,6 +2,7 @@ import { useAtomValue } from "@effect/atom-react";
2
import * as Schema from "effect/Schema";
3
import {
4
useEffect,
5
+ useRef,
6
useState,
7
useSyncExternalStore,
8
type CSSProperties,
@@ -9,7 +10,13 @@ import {
10
} from "react";
11
import { useLocation, useNavigate } from "@tanstack/react-router";
12
13
+import { isCommandPaletteOpen } from "../commandPaletteBus";
14
import { isElectron } from "../env";
15
+import {
16
+ releaseThreadUndoShortcut,
17
+ threadUndoClaimsShortcut,
18
+ undoLatestThreadAction,
19
+} from "../hooks/showThreadUndoNotice";
20
import { getLocalStorageItem, removeLocalStorageItem } from "../hooks/useLocalStorage";
21
import {
22
isRichTextBoldShortcut,
@@ -199,6 +206,19 @@ function SidebarControl() {
206
);
207
}
208
209
+function routePanelsOpen(routeThreadRef: ReturnType<typeof useThreadRouteRef>) {
210
+ if (!routeThreadRef) return { terminalOpen: false, previewOpen: false };
211
+ return {
212
+ terminalOpen: selectThreadTerminalUiState(
213
+ useTerminalUiStateStore.getState().terminalUiStateByThreadKey,
214
+ routeThreadRef,
215
+ ).terminalOpen,
216
+ previewOpen:
217
+ selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, routeThreadRef) ===
218
+ "preview",
219
+ };
220
+}
221
+
222
function NavigationHistoryShortcuts() {
223
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
224
const routeThreadRef = useThreadRouteRef();
@@ -215,17 +235,8 @@ function NavigationHistoryShortcuts() {
235
const command = resolveShortcutCommand(event, keybindings, {
236
context: {
237
terminalFocus: isTerminalFocused(),
218
- terminalOpen: routeThreadRef
219
- ? selectThreadTerminalUiState(
220
- useTerminalUiStateStore.getState().terminalUiStateByThreadKey,
221
- routeThreadRef,
222
- ).terminalOpen
223
- : false,
238
previewFocus: isPreviewFocused(),
225
- previewOpen: routeThreadRef
226
- ? selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, routeThreadRef) ===
227
- "preview"
228
- : false,
239
+ ...routePanelsOpen(routeThreadRef),
240
editableFocus: isEditableFocused(event.target),
241
modelPickerOpen: isModelPickerOpen(),
242
},
@@ -245,6 +256,67 @@ function NavigationHistoryShortcuts() {
256
return null;
257
}
258
259
+const MODIFIER_KEYS = new Set(["Alt", "Control", "Meta", "Shift"]);
260
+
261
+// Undoes the thread actions in the sidebar's notice. Mounted wherever the
262
+// thread sidebar can show; Settings replaces it. Listens in the capture phase
263
+// so mod+z reaches it before the composer's own undo handler consumes the key.
264
+function ThreadUndoShortcut() {
265
+ const keybindings = useAtomValue(primaryServerKeybindingsAtom);
266
+ const routeThreadRef = useThreadRouteRef();
267
+ // A collapsed sidebar hides the notice, so text fields keep their own undo.
268
+ const noticeVisible = useSidebarVisibility();
269
+ // Outlives the effect: undoing an archive navigates, which re-runs it mid-hold.
270
+ const heldUndoKey = useRef<string | null>(null);
271
+
272
+ useEffect(() => {
273
+ const onKeyDown = (event: KeyboardEvent) => {
274
+ // A held key undoes once. Cancel its repeats so they cannot fall through
275
+ // to the editor's own undo, even after the last notice is gone.
276
+ if (event.repeat) {
277
+ if (event.code === heldUndoKey.current) {
278
+ event.preventDefault();
279
+ event.stopPropagation();
280
+ }
281
+ return;
282
+ }
283
+ // A modifier tapped mid-hold leaves the held key repeating.
284
+ if (!MODIFIER_KEYS.has(event.key)) heldUndoKey.current = null;
285
+ const command = resolveShortcutCommand(event, keybindings, {
286
+ context: {
287
+ terminalFocus: isTerminalFocused(),
288
+ previewFocus: isPreviewFocused(),
289
+ ...routePanelsOpen(routeThreadRef),
290
+ editableFocus:
291
+ isEditableFocused(event.target) && !(noticeVisible && threadUndoClaimsShortcut()),
292
+ modelPickerOpen: isModelPickerOpen(),
293
+ },
294
+ });
295
+ if (command !== "thread.undo") {
296
+ if (!MODIFIER_KEYS.has(event.key)) releaseThreadUndoShortcut();
297
+ return;
298
+ }
299
+ if (isCommandPaletteOpen() || isModelPickerOpen()) return;
300
+ if (undoLatestThreadAction()) {
301
+ heldUndoKey.current = event.code;
302
+ event.preventDefault();
303
+ event.stopPropagation();
304
+ }
305
+ };
306
+ // Pastes from a menu, dictation and drops edit text without a keypress.
307
+ const onInput = () => releaseThreadUndoShortcut();
308
+
309
+ window.addEventListener("keydown", onKeyDown, true);
310
+ window.addEventListener("input", onInput, true);
311
+ return () => {
312
+ window.removeEventListener("keydown", onKeyDown, true);
313
+ window.removeEventListener("input", onInput, true);
314
+ };
315
+ }, [keybindings, noticeVisible, routeThreadRef]);
316
+
317
+ return null;
318
+}
319
+
320
// Settings swaps the thread sidebar out of the tree. Keep the lightweight
321
// project projection subscribed so returning to a draft never renders the
322
// zero-project state while the environment snapshot reconnects.
@@ -357,6 +429,7 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
429
{children}
430
<SidebarControl />
431
<NavigationHistoryShortcuts />
432
+ {isOnSettings ? null : <ThreadUndoShortcut />}
433
<MainAppLocationTracker />
434
</SidebarProvider>
435
</PanelAnimationSuppressionProvider>
apps/web/src/hooks/showThreadUndoNotice.test.ts
+27
-1
@@ -2,7 +2,12 @@ import { AsyncResult } from "effect/reactivity";
2
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4
import { toastManager } from "../components/ui/toast";
5
-import { showThreadUndoNotice, useThreadUndoNotice } from "./showThreadUndoNotice";
5
+import {
6
+ releaseThreadUndoShortcut,
7
+ showThreadUndoNotice,
8
+ threadUndoClaimsShortcut,
9
+ useThreadUndoNotice,
10
+} from "./showThreadUndoNotice";
11
import * as ThreadUndo from "./threadUndo";
12
13
beforeEach(() => vi.useFakeTimers());
@@ -39,4 +44,25 @@ describe("thread undo notice", () => {
44
await notice().undo();
45
expect(undo).toHaveBeenCalledOnce();
46
});
47
+
48
+ it("claims mod+z from text fields until the user types, then again for a new notice", () => {
49
+ const { options } = setup();
50
+ expect(threadUndoClaimsShortcut()).toBe(false);
51
+ showThreadUndoNotice(options);
52
+ expect(threadUndoClaimsShortcut()).toBe(true);
53
+ releaseThreadUndoShortcut();
54
+ expect(threadUndoClaimsShortcut()).toBe(false);
55
+ showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/next") });
56
+ expect(threadUndoClaimsShortcut()).toBe(true);
57
+ });
58
+
59
+ it("stops claiming mod+z once nothing is left to undo", async () => {
60
+ const { options } = setup();
61
+ showThreadUndoNotice(options);
62
+ await notice().undo();
63
+ expect(threadUndoClaimsShortcut()).toBe(false);
64
+ showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/expiring") });
65
+ vi.advanceTimersByTime(5_000);
66
+ expect(threadUndoClaimsShortcut()).toBe(false);
67
+ });
68
});
apps/web/src/hooks/showThreadUndoNotice.ts
+15
@@ -26,6 +26,9 @@ export const useThreadUndoNotice = create<{ notice: UndoNotice | null }>(() => (
26
27
let liveUndos: UndoOptions[] = [];
28
let expiry: ReturnType<typeof setTimeout> | undefined;
29
+// A new notice owns mod+z even inside a text field, until the user presses
30
+// another key or edits text. After that, mod+z is the field's own undo again.
31
+let shortcutClaimed = false;
32
33
function refreshNotice() {
34
const stale = liveUndos.filter(({ claim }) => !claim.isCurrent());
@@ -91,9 +94,21 @@ export function undoLatestThreadAction(): boolean {
94
return true;
95
}
96
97
+/** Whether mod+z should undo the notice's actions even while a text field is focused. */
98
+export function threadUndoClaimsShortcut(): boolean {
99
+ return shortcutClaimed && useThreadUndoNotice.getState().notice !== null;
100
+}
101
+
102
+/** Called on any other keypress or text edit; hands mod+z back to text fields. */
103
+export function releaseThreadUndoShortcut() {
104
+ shortcutClaimed = false;
105
+}
106
+
107
+/** Shows one compact confirmation for the currently undoable thread actions. */
108
export function showThreadUndoNotice(options: UndoOptions) {
109
if (!options.claim.isCurrent()) return;
110
liveUndos.push(options);
111
+ shortcutClaimed = true;
112
refreshNotice();
113
clearTimeout(expiry);
114
expiry = setTimeout(() => {
apps/web/src/routes/_chat.tsx
-10
@@ -22,7 +22,6 @@ import { isPreviewFocused } from "../lib/previewFocus";
22
import { isTerminalFocused } from "../lib/terminalFocus";
23
import { isEditableFocused } from "../lib/editableFocus";
24
import { isModelPickerOpen } from "../modelPickerVisibility";
25
-import { undoLatestThreadAction } from "../hooks/showThreadUndoNotice";
25
import { resolveShortcutCommand } from "../keybindings";
26
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
27
import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore";
@@ -83,15 +82,6 @@ function ChatRouteGlobalShortcuts() {
82
return;
83
}
84
86
- if (command === "thread.undo") {
87
- if (event.repeat || isModelPickerOpen()) return;
88
- if (undoLatestThreadAction()) {
89
- event.preventDefault();
90
- event.stopPropagation();
91
- }
92
- return;
93
- }
94
-
85
if (event.key === "Escape" && selectedThreadKeysSize > 0) {
86
event.preventDefault();
87
clearSelection();
docs/user/keybindings.md
+3
-2
@@ -150,8 +150,9 @@ shortcut; assign one in **Settings → Keybindings**.
150
bottom of the sidebar, such as unpin, settle, snooze, archive, or discarding a
151
draft. Consecutive
152
actions of the same kind undo together. The notice remains available for five
153
-seconds after the latest action. The default shortcut skips text fields and
154
-terminals so native undo keeps working there.
153
+seconds after the latest action. Right after an action, the shortcut works even
154
+in a text field such as the composer. Once you type or press another key, the
155
+field gets its own undo back. Terminals always keep their native shortcut.
156
157
`navigation.back` (`mod+[` by default) and `navigation.forward` (`mod+]`) move
158
through the pages you have visited, like a browser's back and forward buttons.
docs/user/thread-sidebar.md
+1
-1
@@ -51,7 +51,7 @@ On web and desktop, unpinning, settling, snoozing, and archiving a thread each s
51
a notification with **Undo** for five seconds. Undo restores the thread's previous
52
state, including its pinned position, and reopens an archived thread you were
53
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; see
55
[Keybindings](./keybindings.md#commands-with-special-behavior).
56
57
On web and desktop, you can also drag files from your computer onto any thread row: