Skip to content

Commit 21da306

Browse files
t3dotggishaankoclaude
authored
fix(web): ⌘Z undoes a settle even when the composer has focus (#22607)
Co-authored-by: ishaanko <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 6490a9e commit 21da306

6 files changed

Lines changed: 129 additions & 24 deletions

File tree

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

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ import { useAtomValue } from "@effect/atom-react";
22
import * as Schema from "effect/Schema";
33
import {
44
useEffect,
5+
useRef,
56
useState,
67
useSyncExternalStore,
78
type CSSProperties,
89
type ReactNode,
910
} from "react";
1011
import { useLocation, useNavigate } from "@tanstack/react-router";
1112

13+
import { isCommandPaletteOpen } from "../commandPaletteBus";
1214
import { isElectron } from "../env";
15+
import {
16+
releaseThreadUndoShortcut,
17+
threadUndoClaimsShortcut,
18+
undoLatestThreadAction,
19+
} from "../hooks/showThreadUndoNotice";
1320
import { getLocalStorageItem, removeLocalStorageItem } from "../hooks/useLocalStorage";
1421
import {
1522
isRichTextBoldShortcut,
@@ -199,6 +206,19 @@ function SidebarControl() {
199206
);
200207
}
201208

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+
202222
function NavigationHistoryShortcuts() {
203223
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
204224
const routeThreadRef = useThreadRouteRef();
@@ -215,17 +235,8 @@ function NavigationHistoryShortcuts() {
215235
const command = resolveShortcutCommand(event, keybindings, {
216236
context: {
217237
terminalFocus: isTerminalFocused(),
218-
terminalOpen: routeThreadRef
219-
? selectThreadTerminalUiState(
220-
useTerminalUiStateStore.getState().terminalUiStateByThreadKey,
221-
routeThreadRef,
222-
).terminalOpen
223-
: false,
224238
previewFocus: isPreviewFocused(),
225-
previewOpen: routeThreadRef
226-
? selectActiveRightPanel(useRightPanelStore.getState().byThreadKey, routeThreadRef) ===
227-
"preview"
228-
: false,
239+
...routePanelsOpen(routeThreadRef),
229240
editableFocus: isEditableFocused(event.target),
230241
modelPickerOpen: isModelPickerOpen(),
231242
},
@@ -245,6 +256,67 @@ function NavigationHistoryShortcuts() {
245256
return null;
246257
}
247258

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+
248320
// Settings swaps the thread sidebar out of the tree. Keep the lightweight
249321
// project projection subscribed so returning to a draft never renders the
250322
// zero-project state while the environment snapshot reconnects.
@@ -357,6 +429,7 @@ export function AppSidebarLayout({ children }: { children: ReactNode }) {
357429
{children}
358430
<SidebarControl />
359431
<NavigationHistoryShortcuts />
432+
{isOnSettings ? null : <ThreadUndoShortcut />}
360433
<MainAppLocationTracker />
361434
</SidebarProvider>
362435
</PanelAnimationSuppressionProvider>

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { AsyncResult } from "effect/reactivity";
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
33

44
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";
611
import * as ThreadUndo from "./threadUndo";
712

813
beforeEach(() => vi.useFakeTimers());
@@ -39,4 +44,25 @@ describe("thread undo notice", () => {
3944
await notice().undo();
4045
expect(undo).toHaveBeenCalledOnce();
4146
});
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+
});
4268
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export const useThreadUndoNotice = create<{ notice: UndoNotice | null }>(() => (
2626

2727
let liveUndos: UndoOptions[] = [];
2828
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;
2932

3033
function refreshNotice() {
3134
const stale = liveUndos.filter(({ claim }) => !claim.isCurrent());
@@ -91,9 +94,21 @@ export function undoLatestThreadAction(): boolean {
9194
return true;
9295
}
9396

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. */
94108
export function showThreadUndoNotice(options: UndoOptions) {
95109
if (!options.claim.isCurrent()) return;
96110
liveUndos.push(options);
111+
shortcutClaimed = true;
97112
refreshNotice();
98113
clearTimeout(expiry);
99114
expiry = setTimeout(() => {

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { isPreviewFocused } from "../lib/previewFocus";
2222
import { isTerminalFocused } from "../lib/terminalFocus";
2323
import { isEditableFocused } from "../lib/editableFocus";
2424
import { isModelPickerOpen } from "../modelPickerVisibility";
25-
import { undoLatestThreadAction } from "../hooks/showThreadUndoNotice";
2625
import { resolveShortcutCommand } from "../keybindings";
2726
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
2827
import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore";
@@ -83,15 +82,6 @@ function ChatRouteGlobalShortcuts() {
8382
return;
8483
}
8584

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-
9585
if (event.key === "Escape" && selectedThreadKeysSize > 0) {
9686
event.preventDefault();
9787
clearSelection();

‎docs/user/keybindings.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ shortcut; assign one in **Settings → Keybindings**.
150150
bottom of the sidebar, such as unpin, settle, snooze, archive, or discarding a
151151
draft. Consecutive
152152
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.
155156

156157
`navigation.back` (`mod+[` by default) and `navigation.forward` (`mod+]`) move
157158
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; 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)