Skip to content

Commit df6ce75

Browse files
fix(web): make Cmd+Z undo work from the composer and hold the notice on hover
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 2a23c30 commit df6ce75

8 files changed

Lines changed: 149 additions & 23 deletions

File tree

‎apps/web/src/components/sidebar/SidebarThreadUndoNotice.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { useAtomValue } from "@effect/atom-react";
22

3-
import { undoLatestThreadAction, useThreadUndoNotice } from "../../hooks/showThreadUndoNotice";
3+
import {
4+
setThreadUndoNoticeHeld,
5+
undoLatestThreadAction,
6+
useThreadUndoNotice,
7+
} from "../../hooks/showThreadUndoNotice";
48
import { shortcutLabelForCommand } from "../../keybindings";
59
import { primaryServerKeybindingsAtom } from "../../state/server";
610
import { Alert, AlertDescription } from "../ui/alert";
@@ -14,7 +18,12 @@ export function SidebarThreadUndoNotice() {
1418
const shortcut = shortcutLabelForCommand(keybindings, "thread.undo");
1519

1620
return (
17-
<Alert role="status" variant="sidebar">
21+
<Alert
22+
role="status"
23+
variant="sidebar"
24+
onPointerEnter={() => setThreadUndoNoticeHeld(true)}
25+
onPointerLeave={() => setThreadUndoNoticeHeld(false)}
26+
>
1827
<AlertDescription>
1928
{notice.action} {notice.count} thread{notice.count === 1 ? "" : "s"},{" "}
2029
<InlineButton onClick={undoLatestThreadAction}>

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

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

55
import { toastManager } from "../components/ui/toast";
66
import {
7+
setThreadUndoNoticeHeld,
78
showThreadUndoNotice,
89
undoLatestThreadAction,
910
useThreadUndoNotice,
@@ -149,4 +150,29 @@ describe("thread undo notice", () => {
149150
expect(older).toHaveBeenCalledOnce();
150151
expect(undoLatestThreadAction()).toBe(false);
151152
});
153+
154+
it("holds the notice while hovered and restarts the full timer on leave", () => {
155+
const { options } = setup();
156+
showThreadUndoNotice(options);
157+
vi.advanceTimersByTime(4_000);
158+
setThreadUndoNoticeHeld(true);
159+
vi.advanceTimersByTime(60_000);
160+
expect(useThreadUndoNotice.getState().notice).not.toBeNull();
161+
setThreadUndoNoticeHeld(false);
162+
vi.advanceTimersByTime(4_999);
163+
expect(useThreadUndoNotice.getState().notice).not.toBeNull();
164+
vi.advanceTimersByTime(1);
165+
expect(useThreadUndoNotice.getState().notice).toBeNull();
166+
});
167+
168+
it("does not carry a hover hold over to the next notice", () => {
169+
const { options } = setup();
170+
showThreadUndoNotice(options);
171+
setThreadUndoNoticeHeld(true);
172+
void undoLatestThreadAction();
173+
expect(useThreadUndoNotice.getState().notice).toBeNull();
174+
showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/next") });
175+
vi.advanceTimersByTime(5_000);
176+
expect(useThreadUndoNotice.getState().notice).toBeNull();
177+
});
152178
});

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

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,28 @@ export const useThreadUndoNotice = create<{ notice: UndoNotice | null }>(() => (
2727
// same kind share one notice and can be restored together.
2828
let liveUndos: UndoOptions[] = [];
2929
let expiry: ReturnType<typeof setTimeout> | undefined;
30+
let expiryPaused = false;
31+
const UNDO_NOTICE_TTL_MS = 5_000;
32+
33+
function expireUndos() {
34+
const expired = liveUndos;
35+
liveUndos = [];
36+
for (const { claim } of expired) claim.finish();
37+
refreshNotice();
38+
}
39+
40+
function scheduleExpiry() {
41+
clearTimeout(expiry);
42+
if (!expiryPaused) expiry = setTimeout(expireUndos, UNDO_NOTICE_TTL_MS);
43+
}
3044

3145
function refreshNotice() {
3246
liveUndos = liveUndos.filter(({ claim }) => claim.isCurrent());
3347
const latest = liveUndos.at(-1);
3448
if (!latest) {
3549
clearTimeout(expiry);
50+
// The notice unmounts without a pointer-leave, so drop a stale hover hold.
51+
expiryPaused = false;
3652
useThreadUndoNotice.setState({ notice: null });
3753
return;
3854
}
@@ -96,11 +112,11 @@ export function showThreadUndoNotice(options: UndoOptions) {
96112
if (!options.claim.isCurrent()) return;
97113
liveUndos.push(options);
98114
refreshNotice();
99-
clearTimeout(expiry);
100-
expiry = setTimeout(() => {
101-
const expired = liveUndos;
102-
liveUndos = [];
103-
for (const { claim } of expired) claim.finish();
104-
refreshNotice();
105-
}, 5_000);
115+
scheduleExpiry();
116+
}
117+
118+
/** Holds the notice while the pointer is over it; leaving restarts the full timer. */
119+
export function setThreadUndoNoticeHeld(held: boolean) {
120+
expiryPaused = held;
121+
if (liveUndos.length > 0) scheduleExpiry();
106122
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2+
3+
import { editableOwnsUndo } from "./editableFocus";
4+
5+
// The unit project has no DOM; these stand in for the elements the helper inspects.
6+
class FakeElement extends EventTarget {
7+
constructor(
8+
readonly editable: boolean,
9+
readonly textContent = "",
10+
) {
11+
super();
12+
}
13+
closest() {
14+
return this.editable ? this : null;
15+
}
16+
}
17+
class FakeInput extends FakeElement {
18+
constructor(readonly value: string) {
19+
super(true);
20+
}
21+
}
22+
23+
class FakeTextArea extends FakeInput {}
24+
25+
beforeEach(() => {
26+
vi.stubGlobal("Element", FakeElement);
27+
vi.stubGlobal("HTMLInputElement", FakeInput);
28+
vi.stubGlobal("HTMLTextAreaElement", FakeTextArea);
29+
});
30+
afterEach(() => vi.unstubAllGlobals());
31+
32+
describe("editableOwnsUndo", () => {
33+
it("yields to native undo only when the focused field has text", () => {
34+
expect(editableOwnsUndo(new FakeInput(""))).toBe(false);
35+
expect(editableOwnsUndo(new FakeInput("draft"))).toBe(true);
36+
expect(editableOwnsUndo(new FakeElement(true, ""))).toBe(false);
37+
expect(editableOwnsUndo(new FakeElement(true, "draft"))).toBe(true);
38+
});
39+
40+
it("ignores non-editable targets", () => {
41+
expect(editableOwnsUndo(new FakeElement(false, "Undo"))).toBe(false);
42+
expect(editableOwnsUndo(null)).toBe(false);
43+
});
44+
});

‎apps/web/src/lib/editableFocus.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@ const EDITABLE_SELECTOR = [
1515
export function isEditableFocused(target: EventTarget | null = document.activeElement): boolean {
1616
return target instanceof Element && target.closest(EDITABLE_SELECTOR) !== null;
1717
}
18+
19+
/**
20+
* Whether the focused editable has content for native undo to act on. An
21+
* empty composer has nothing to undo, so mod+z can fall through to the app.
22+
*/
23+
export function editableOwnsUndo(target: EventTarget | null = document.activeElement): boolean {
24+
if (!isEditableFocused(target)) return false;
25+
const element = (target as Element).closest(EDITABLE_SELECTOR);
26+
if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) {
27+
return element.value.length > 0;
28+
}
29+
return (element?.textContent ?? "").length > 0;
30+
}

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { useHandleNewThread } from "../hooks/useHandleNewThread";
1616
import { startNewThreadFromContext } from "../lib/chatThreadActions";
1717
import { isPreviewFocused } from "../lib/previewFocus";
1818
import { isTerminalFocused } from "../lib/terminalFocus";
19-
import { isEditableFocused } from "../lib/editableFocus";
19+
import { editableOwnsUndo, isEditableFocused } from "../lib/editableFocus";
2020
import { isModelPickerOpen } from "../modelPickerVisibility";
2121
import { undoLatestThreadAction } from "../hooks/showThreadUndoNotice";
2222
import { resolveShortcutCommand } from "../keybindings";
@@ -60,6 +60,32 @@ function ChatRouteGlobalShortcuts() {
6060
? selectActiveRightPanel(state.byThreadKey, routeThreadRef) === "preview"
6161
: false,
6262
);
63+
// Capture phase: the composer's editor claims mod+z (and marks it handled)
64+
// even when it is empty, which would otherwise starve the thread Undo notice.
65+
useEffect(() => {
66+
const onUndoKeyDown = (event: KeyboardEvent) => {
67+
const command = resolveShortcutCommand(event, keybindings, {
68+
context: {
69+
terminalFocus: isTerminalFocused(),
70+
terminalOpen,
71+
previewFocus: isPreviewFocused(),
72+
previewOpen,
73+
editableFocus: editableOwnsUndo(event.target),
74+
modelPickerOpen: isModelPickerOpen(),
75+
},
76+
});
77+
if (command !== "thread.undo") return;
78+
if (event.repeat || isCommandPaletteOpen() || isModelPickerOpen()) return;
79+
if (undoLatestThreadAction()) {
80+
event.preventDefault();
81+
event.stopPropagation();
82+
}
83+
};
84+
85+
window.addEventListener("keydown", onUndoKeyDown, true);
86+
return () => window.removeEventListener("keydown", onUndoKeyDown, true);
87+
}, [keybindings, previewOpen, terminalOpen]);
88+
6389
useEffect(() => {
6490
const onWindowKeyDown = (event: KeyboardEvent) => {
6591
if (event.defaultPrevented) return;
@@ -78,15 +104,6 @@ function ChatRouteGlobalShortcuts() {
78104
return;
79105
}
80106

81-
if (command === "thread.undo") {
82-
if (event.repeat || isModelPickerOpen()) return;
83-
if (undoLatestThreadAction()) {
84-
event.preventDefault();
85-
event.stopPropagation();
86-
}
87-
return;
88-
}
89-
90107
if (event.key === "Escape" && selectedThreadKeysSize > 0) {
91108
event.preventDefault();
92109
clearSelection();

‎docs/user/keybindings.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ shortcut; assign one in **Settings → Keybindings**.
114114
`thread.undo` (`mod+z` by default) reverses the actions shown in the notice at the
115115
bottom of the sidebar, such as unpin, settle, snooze, or archive. Consecutive
116116
actions of the same kind undo together. The notice remains available for five
117-
seconds after the latest action. The default shortcut skips text fields and
118-
terminals so native undo keeps working there.
117+
seconds after the latest action, and stays up while the pointer is over it. The
118+
default shortcut skips text fields that contain text, and terminals, so native
119+
undo keeps working there; an empty composer does not block it.
119120

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

‎docs/user/thread-sidebar.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ and worktree while you stay in the new thread composer. This requires a Git proj
3030
Pin a thread from its menu to keep it above your active work.
3131

3232
On web and desktop, unpinning, settling, snoozing, and archiving a thread each show
33-
a notification with **Undo** for five seconds. Undo restores the thread's previous
33+
a notification with **Undo** for five seconds (it stays while the pointer is over it). Undo restores the thread's previous
3434
state, including its pinned position, and reopens an archived thread you were
35-
viewing. `mod+z` triggers the most recent Undo when no text field is focused; see
35+
viewing. `mod+z` triggers the most recent Undo unless the focused text field has text to undo; see
3636
[Keybindings](./keybindings.md#commands-with-special-behavior).
3737

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

0 commit comments

Comments
 (0)