Skip to content

Commit 5781b52

Browse files
feat(web): undo settle, snooze and archive, with a mod+z shortcut (#12848)
Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 6b0a04a commit 5781b52

14 files changed

Lines changed: 520 additions & 159 deletions

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ import {
229229
} from "@t3tools/client-runtime/state/subagentRuntime";
230230
import { BranchToolbar, type BranchToolbarHandle } from "./BranchToolbar";
231231
import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings";
232+
import { isEditableFocused } from "../lib/editableFocus";
233+
import { undoLatestThreadAction } from "../hooks/showUndoToast";
232234
import ThreadTerminalDrawer from "./ThreadTerminalDrawer";
233235
import {
234236
AlarmClockIcon,
@@ -6647,11 +6649,12 @@ export default function ChatView(props: ChatViewProps) {
66476649
}, [activeThreadKey, focusComposer, terminalUiState.terminalOpen]);
66486650

66496651
const getShortcutContext = useCallback(
6650-
() => ({
6652+
(eventTarget: EventTarget | null = document.activeElement) => ({
66516653
terminalFocus: getTerminalFocusOwner() !== null,
66526654
terminalOpen: Boolean(terminalUiState.terminalOpen),
66536655
previewFocus: isPreviewFocused(),
66546656
previewOpen: previewPanelOpen,
6657+
editableFocus: isEditableFocused(eventTarget),
66556658
modelPickerOpen: composerRef.current?.isModelPickerOpen() ?? false,
66566659
isWeb: !isElectron,
66576660
isDesktop: isElectron,
@@ -6679,7 +6682,7 @@ export default function ChatView(props: ChatViewProps) {
66796682
if (event.defaultPrevented && terminalFocusOwner === null) {
66806683
return;
66816684
}
6682-
const shortcutContext = getShortcutContext();
6685+
const shortcutContext = getShortcutContext(event.target);
66836686

66846687
if (
66856688
!shortcutContext.terminalFocus &&
@@ -6728,6 +6731,17 @@ export default function ChatView(props: ChatViewProps) {
67286731
return;
67296732
}
67306733

6734+
if (command === "thread.undo") {
6735+
// Only claim the chord when there is an Undo to run; otherwise the
6736+
// page keeps its native behavior for the key.
6737+
if (event.repeat) return;
6738+
if (undoLatestThreadAction()) {
6739+
event.preventDefault();
6740+
event.stopPropagation();
6741+
}
6742+
return;
6743+
}
6744+
67316745
if (command === "thread.pin") {
67326746
event.preventDefault();
67336747
event.stopPropagation();

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

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,7 @@ import {
208208
type TerminalStatusIndicator,
209209
useLinkedThreadPullRequest,
210210
} from "./ThreadStatusIndicators";
211-
import {
212-
resolveSnoozePresets,
213-
snoozeWakeDescription,
214-
snoozeWakeLabel,
215-
type SnoozePreset,
216-
} from "./Sidebar.snooze";
211+
import { resolveSnoozePresets, snoozeWakeLabel, type SnoozePreset } from "./Sidebar.snooze";
217212
import { ProjectFavicon, type ProjectFaviconProject } from "./ProjectFavicon";
218213
import { ThreadSearchMatchExcerpt } from "./ThreadSearchMatch";
219214
import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
@@ -3082,7 +3077,9 @@ export default function Sidebar() {
30823077
settlingThreadKeysRef.current.add(threadKey);
30833078
try {
30843079
const navigateAfterSettle = planForwardNavigation(threadKey, opts.coSettlingKeys);
3085-
const result = await settleThread(threadRef);
3080+
const result = await settleThread(threadRef, {
3081+
undoToast: opts.coSettlingKeys === undefined,
3082+
});
30863083
if (result._tag === "Failure") {
30873084
// Never navigate away from a thread that did not settle.
30883085
if (!isAtomCommandInterrupted(result)) {
@@ -3734,7 +3731,9 @@ export default function Sidebar() {
37343731
// Snoozing the open thread moves you forward, same as settle —
37353732
// both park the thread you're done with for now.
37363733
const navigateAfterSnooze = planForwardNavigation(threadKey, opts.coSnoozingKeys);
3737-
const result = await snoozeThread(threadRef, preset.snoozedUntil);
3734+
const result = await snoozeThread(threadRef, preset.snoozedUntil, {
3735+
undoToast: opts.coSnoozingKeys === undefined,
3736+
});
37383737
if (result._tag === "Failure") {
37393738
// Never navigate away from a thread that did not snooze.
37403739
return isAtomCommandInterrupted(result)
@@ -3780,23 +3779,9 @@ export default function Sidebar() {
37803779
);
37813780
return;
37823781
}
3783-
if (outcome.status !== "success") return;
3784-
// Snooze hides the row, so the toast is the only confirmation —
3785-
// and the Undo is the escape hatch for a mis-click.
3786-
toastManager.add(
3787-
stackedThreadToast({
3788-
type: "success",
3789-
title: `Snoozed until ${snoozeWakeDescription(preset.snoozedUntil, new Date(), timestampFormat)}`,
3790-
timeout: 5_000,
3791-
actionProps: {
3792-
children: "Undo",
3793-
onClick: () => attemptUnsnooze(threadRef),
3794-
},
3795-
}),
3796-
);
37973782
})();
37983783
},
3799-
[attemptUnsnooze, performSnooze, timestampFormat],
3784+
[performSnooze],
38003785
);
38013786

38023787
const removeFromSelection = useThreadSelectionStore((s) => s.removeFromSelection);

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as Cause from "effect/Cause";
33
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
44

55
import { toastManager } from "../components/ui/toast";
6-
import { showUndoToast } from "./showUndoToast";
6+
import { showUndoToast, undoLatestThreadAction } from "./showUndoToast";
77
import * as ThreadUndo from "./threadUndo";
88

99
afterEach(() => vi.restoreAllMocks());
@@ -93,3 +93,35 @@ describe("showUndoToast", () => {
9393
expect(add).toHaveBeenCalledOnce();
9494
});
9595
});
96+
97+
describe("undoLatestThreadAction", () => {
98+
it("runs the newest live Undo once and then reports nothing to undo", () => {
99+
const { options } = setup();
100+
const older = vi.fn(async () => AsyncResult.success(undefined));
101+
const newer = vi.fn(async () => AsyncResult.success(undefined));
102+
showUndoToast({ ...options, undo: older, claim: ThreadUndo.begin("settle", "env/a") });
103+
showUndoToast({ ...options, undo: newer, claim: ThreadUndo.begin("snooze", "env/b") });
104+
expect(undoLatestThreadAction()).toBe(true);
105+
expect(newer).toHaveBeenCalledOnce();
106+
expect(older).not.toHaveBeenCalled();
107+
expect(undoLatestThreadAction()).toBe(true);
108+
expect(older).toHaveBeenCalledOnce();
109+
expect(undoLatestThreadAction()).toBe(false);
110+
});
111+
112+
it("skips a superseded toast and a closed toast", () => {
113+
const { add, options } = setup();
114+
const superseded = vi.fn(async () => AsyncResult.success(undefined));
115+
const closed = vi.fn(async () => AsyncResult.success(undefined));
116+
const live = vi.fn(async () => AsyncResult.success(undefined));
117+
showUndoToast({ ...options, undo: live, claim: ThreadUndo.begin("archive", "env/live") });
118+
showUndoToast({ ...options, undo: closed, claim: ThreadUndo.begin("archive", "env/closed") });
119+
add.mock.calls[1]?.[0].onClose?.();
120+
showUndoToast({ ...options, undo: superseded, claim: ThreadUndo.begin("pin", "env/stale") });
121+
ThreadUndo.invalidate("pin", "env/stale");
122+
expect(undoLatestThreadAction()).toBe(true);
123+
expect(superseded).not.toHaveBeenCalled();
124+
expect(closed).not.toHaveBeenCalled();
125+
expect(live).toHaveBeenCalledOnce();
126+
});
127+
});

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import {
77
import { stackedThreadToast, toastManager } from "../components/ui/toast";
88
import type * as ThreadUndo from "./threadUndo";
99

10+
// Undo toasts still on screen, oldest first, so the `thread.undo` shortcut
11+
// mirrors the newest toast's button without knowing which action it was.
12+
const liveUndos: Array<() => Promise<void> | null> = [];
13+
14+
/** Runs the newest Undo whose claim still holds; false when nothing is left to undo. */
15+
export function undoLatestThreadAction(): boolean {
16+
// A superseded entry drops itself when tried, so keep going until one
17+
// runs or the list is empty.
18+
while (liveUndos.length > 0) {
19+
if (liveUndos[liveUndos.length - 1]?.() !== null) return true;
20+
}
21+
return false;
22+
}
23+
1024
/** Shows a single-use Undo while its thread action still owns the claim. */
1125
export function showUndoToast({
1226
title,
@@ -23,6 +37,7 @@ export function showUndoToast({
2337
}) {
2438
if (!claim.isCurrent()) return;
2539
let undoStarted = false;
40+
let toastId: string | undefined;
2641
const reportFailure = (error: unknown) => {
2742
toastManager.add(
2843
stackedThreadToast({
@@ -32,7 +47,26 @@ export function showUndoToast({
3247
}),
3348
);
3449
};
35-
const toastId = toastManager.add({
50+
const forget = () => {
51+
const index = liveUndos.indexOf(run);
52+
if (index !== -1) liveUndos.splice(index, 1);
53+
};
54+
const run = () => {
55+
forget();
56+
if (undoStarted || !claim.isCurrent()) return null;
57+
undoStarted = true;
58+
claim.finish();
59+
if (toastId !== undefined) toastManager.close(toastId);
60+
return undo()
61+
.then((result) => {
62+
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
63+
reportFailure(squashAtomCommandFailure(result));
64+
}
65+
})
66+
.catch(reportFailure);
67+
};
68+
liveUndos.push(run);
69+
toastId = toastManager.add({
3670
...stackedThreadToast({
3771
type: "success",
3872
title,
@@ -41,21 +75,13 @@ export function showUndoToast({
4175
actionProps: {
4276
children: "Undo",
4377
onClick: async () => {
44-
if (undoStarted || !claim.isCurrent()) return;
45-
undoStarted = true;
46-
claim.finish();
47-
toastManager.close(toastId);
48-
try {
49-
const result = await undo();
50-
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
51-
reportFailure(squashAtomCommandFailure(result));
52-
}
53-
} catch (error) {
54-
reportFailure(error);
55-
}
78+
await run();
5679
},
5780
},
5881
}),
59-
onClose: claim.finish,
82+
onClose: () => {
83+
claim.finish();
84+
forget();
85+
},
6086
});
6187
}

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

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { ScopedThreadRef, ThreadId } from "@t3tools/contracts";
1111
import { useRouter } from "@tanstack/react-router";
1212
import { useCallback, useMemo } from "react";
1313

14-
import { resolveSnoozePresets, snoozeWakeDescription } from "../components/Sidebar.snooze";
14+
import { resolveSnoozePresets } from "../components/Sidebar.snooze";
1515
import {
1616
buildThreadActionMenuItems,
1717
type ThreadActionMenuId,
@@ -162,29 +162,9 @@ export function useThreadActionMenu(input: {
162162
: snoozePresets.find((candidate) => `snooze:${candidate.id}` === action);
163163
if (!preset) return;
164164
const result = await snoozeThread(threadRef, preset.snoozedUntil);
165-
if (result._tag === "Failure") {
166-
if (!isAtomCommandInterrupted(result)) {
167-
failureToast("Failed to snooze thread", squashAtomCommandFailure(result));
168-
}
169-
return;
165+
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
166+
failureToast("Failed to snooze thread", squashAtomCommandFailure(result));
170167
}
171-
toastManager.add(
172-
stackedThreadToast({
173-
type: "success",
174-
title: `Snoozed until ${snoozeWakeDescription(preset.snoozedUntil, new Date(), timestampFormat)}`,
175-
timeout: 5_000,
176-
actionProps: {
177-
children: "Undo",
178-
onClick: () => {
179-
void unsnoozeThread(threadRef).then((undone) => {
180-
if (undone._tag === "Failure" && !isAtomCommandInterrupted(undone)) {
181-
failureToast("Failed to wake thread", squashAtomCommandFailure(undone));
182-
}
183-
});
184-
},
185-
},
186-
}),
187-
);
188168
return;
189169
}
190170
const reportFailure = async (

0 commit comments

Comments
 (0)