Skip to content

Commit 9a609a4

Browse files
fix(web): show thread undo notice in the sidebar (pingdotgg#12972)
1 parent 0141bc2 commit 9a609a4

14 files changed

Lines changed: 378 additions & 340 deletions

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ import {
230230
import { BranchToolbar, type BranchToolbarHandle } from "./BranchToolbar";
231231
import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings";
232232
import { isEditableFocused } from "../lib/editableFocus";
233-
import { undoLatestThreadAction } from "../hooks/showUndoToast";
234233
import ThreadTerminalDrawer from "./ThreadTerminalDrawer";
235234
import {
236235
AlarmClockIcon,
@@ -6731,17 +6730,6 @@ export default function ChatView(props: ChatViewProps) {
67316730
return;
67326731
}
67336732

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-
67456733
if (command === "thread.pin") {
67466734
event.preventDefault();
67476735
event.stopPropagation();

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

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3077,9 +3077,7 @@ export default function Sidebar() {
30773077
settlingThreadKeysRef.current.add(threadKey);
30783078
try {
30793079
const navigateAfterSettle = planForwardNavigation(threadKey, opts.coSettlingKeys);
3080-
const result = await settleThread(threadRef, {
3081-
undoToast: opts.coSettlingKeys === undefined,
3082-
});
3080+
const result = await settleThread(threadRef);
30833081
if (result._tag === "Failure") {
30843082
// Never navigate away from a thread that did not settle.
30853083
if (!isAtomCommandInterrupted(result)) {
@@ -3731,9 +3729,7 @@ export default function Sidebar() {
37313729
// Snoozing the open thread moves you forward, same as settle —
37323730
// both park the thread you're done with for now.
37333731
const navigateAfterSnooze = planForwardNavigation(threadKey, opts.coSnoozingKeys);
3734-
const result = await snoozeThread(threadRef, preset.snoozedUntil, {
3735-
undoToast: opts.coSnoozingKeys === undefined,
3736-
});
3732+
const result = await snoozeThread(threadRef, preset.snoozedUntil);
37373733
if (result._tag === "Failure") {
37383734
// Never navigate away from a thread that did not snooze.
37393735
return isAtomCommandInterrupted(result)
@@ -3887,35 +3883,15 @@ export default function Sidebar() {
38873883
outcome.status === "failure" ? [outcome.error] : [],
38883884
);
38893885

3890-
if (snoozedThreadRefs.length > 0) {
3891-
const snoozedCount = snoozedThreadRefs.length;
3892-
const failedCount = failures.length;
3893-
toastManager.add(
3894-
stackedThreadToast({
3895-
type: failedCount > 0 ? "warning" : "success",
3896-
title:
3897-
failedCount > 0
3898-
? `Snoozed ${snoozedCount} of ${selectedThreads.length} threads`
3899-
: `Snoozed ${snoozedCount} thread${snoozedCount === 1 ? "" : "s"}`,
3900-
description:
3901-
failedCount > 0
3902-
? `${failedCount} thread${failedCount === 1 ? "" : "s"} couldn't be snoozed.`
3903-
: undefined,
3904-
timeout: 5_000,
3905-
actionProps: {
3906-
children: "Undo",
3907-
onClick: () => {
3908-
for (const threadRef of snoozedThreadRefs) attemptUnsnooze(threadRef);
3909-
},
3910-
},
3911-
}),
3912-
);
3913-
} else if (failures.length > 0) {
3886+
if (failures.length > 0) {
39143887
const firstError = failures[0];
39153888
toastManager.add(
39163889
stackedThreadToast({
39173890
type: "error",
3918-
title: "Failed to snooze threads",
3891+
title:
3892+
snoozedThreadRefs.length > 0
3893+
? `Failed to snooze ${failures.length} thread${failures.length === 1 ? "" : "s"}`
3894+
: "Failed to snooze threads",
39193895
description:
39203896
firstError instanceof Error ? firstError.message : "An error occurred.",
39213897
}),
@@ -4019,7 +3995,6 @@ export default function Sidebar() {
40193995
},
40203996
[
40213997
attemptSettle,
4022-
attemptSnooze,
40233998
attemptUnpin,
40243999
clearSelection,
40254000
confirmThreadDelete,
@@ -4028,7 +4003,6 @@ export default function Sidebar() {
40284003
performSnooze,
40294004
removeFromSelection,
40304005
serverConfigs,
4031-
attemptUnsnooze,
40324006
updateThreadMetadata,
40334007
timestampFormat,
40344008
],

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "../ui/sidebar";
2727
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
2828
import { readPullRequestListPreferences } from "../pullRequest/pullRequestListPreferences";
29+
import { SidebarThreadUndoNotice } from "./SidebarThreadUndoNotice";
2930
import { SidebarProviderUpdatePill } from "./SidebarProviderUpdatePill";
3031
import { SidebarUpdateArchitectureWarning, SidebarUpdatePill } from "./SidebarUpdatePill";
3132
import { PullRequestGlyph } from "~/components/pullRequest/pullRequestIcons";
@@ -221,6 +222,7 @@ export const SidebarUtilityMenu = memo(function SidebarUtilityMenu() {
221222
export const SidebarChromeFooter = memo(function SidebarChromeFooter() {
222223
return (
223224
<SidebarFooter className="px-[var(--sidebar-content-inset)] py-1">
225+
<SidebarThreadUndoNotice />
224226
<SidebarProviderUpdatePill />
225227
<SidebarUpdateArchitectureWarning />
226228
<SidebarUtilityMenu />
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useAtomValue } from "@effect/atom-react";
2+
3+
import { undoLatestThreadAction, useThreadUndoNotice } from "../../hooks/showThreadUndoNotice";
4+
import { shortcutLabelForCommand } from "../../keybindings";
5+
import { primaryServerKeybindingsAtom } from "../../state/server";
6+
import { Alert, AlertDescription } from "../ui/alert";
7+
import { InlineButton } from "../ui/button";
8+
9+
export function SidebarThreadUndoNotice() {
10+
const notice = useThreadUndoNotice((state) => state.notice);
11+
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
12+
13+
if (!notice) return null;
14+
const shortcut = shortcutLabelForCommand(keybindings, "thread.undo");
15+
16+
return (
17+
<Alert role="status" variant="sidebar">
18+
<AlertDescription>
19+
{notice.action} {notice.count} thread{notice.count === 1 ? "" : "s"},{" "}
20+
<InlineButton
21+
underline
22+
onClick={undoLatestThreadAction}
23+
className="hover:text-sidebar-foreground"
24+
>
25+
{shortcut ? `${shortcut} to undo` : "Undo"}
26+
</InlineButton>
27+
</AlertDescription>
28+
</Alert>
29+
);
30+
}

‎apps/web/src/components/ui/alert.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const alertVariants = cva("relative rounded-xl border px-3.5 py-3 text-card-fore
1111
variants: {
1212
variant: {
1313
default: "bg-transparent dark:bg-input/32 [&_svg]:text-muted-foreground",
14+
sidebar:
15+
"rounded-lg border-sidebar-border bg-sidebar-control-surface px-2 py-1.5 text-[11px] leading-4 [&_[data-slot=alert-description]]:block [&_[data-slot=alert-description]]:text-sidebar-muted-foreground",
1416
error:
1517
"border-error/32 bg-error-surface text-error-foreground [&_[data-slot=alert-description]]:text-error-foreground/80 [&_svg]:text-error",
1618
info: "border-info/32 bg-info/4 [&_svg]:text-info",
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import { AsyncResult } from "effect/unstable/reactivity";
2+
import * as Cause from "effect/Cause";
3+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4+
5+
import { toastManager } from "../components/ui/toast";
6+
import {
7+
showThreadUndoNotice,
8+
undoLatestThreadAction,
9+
useThreadUndoNotice,
10+
} from "./showThreadUndoNotice";
11+
import * as ThreadUndo from "./threadUndo";
12+
13+
beforeEach(() => vi.useFakeTimers());
14+
afterEach(() => {
15+
vi.runAllTimers();
16+
vi.useRealTimers();
17+
vi.restoreAllMocks();
18+
});
19+
20+
function setup() {
21+
const add = vi.spyOn(toastManager, "add").mockReturnValue("error-toast");
22+
const undo = vi.fn(async () => AsyncResult.success(undefined));
23+
const claim = ThreadUndo.begin("pin", "env/thread");
24+
const options = { action: "Unpinned" as const, failureTitle: "Restore failed", undo, claim };
25+
return { add, undo, claim, options };
26+
}
27+
28+
function notice() {
29+
const value = useThreadUndoNotice.getState().notice;
30+
if (!value) throw new Error("Undo notice is missing");
31+
return value;
32+
}
33+
34+
describe("thread undo notice", () => {
35+
it("aggregates consecutive actions without success toasts and restores the group once", async () => {
36+
const { add, undo, options } = setup();
37+
showThreadUndoNotice({
38+
...options,
39+
action: "Settled",
40+
claim: ThreadUndo.begin("settle", "env/a"),
41+
});
42+
showThreadUndoNotice({
43+
...options,
44+
action: "Settled",
45+
claim: ThreadUndo.begin("settle", "env/b"),
46+
});
47+
expect(notice()).toMatchObject({ action: "Settled", count: 2 });
48+
expect(add).not.toHaveBeenCalled();
49+
const group = notice();
50+
await group.undo();
51+
await group.undo();
52+
expect(undo).toHaveBeenCalledTimes(2);
53+
expect(undoLatestThreadAction()).toBe(false);
54+
});
55+
56+
it("drops invalidated claims immediately and rejects a captured stale undo", async () => {
57+
const { undo, options } = setup();
58+
showThreadUndoNotice(options);
59+
const stale = notice();
60+
ThreadUndo.invalidate("pin", "env/thread");
61+
expect(useThreadUndoNotice.getState().notice).toBeNull();
62+
showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/thread") });
63+
await stale.undo();
64+
expect(undo).not.toHaveBeenCalled();
65+
await notice().undo();
66+
expect(undo).toHaveBeenCalledOnce();
67+
});
68+
69+
it("does not show a notice for a late completion after a newer action", () => {
70+
const { add, options } = setup();
71+
ThreadUndo.invalidate("pin", "env/thread");
72+
showThreadUndoNotice(options);
73+
expect(useThreadUndoNotice.getState().notice).toBeNull();
74+
expect(add).not.toHaveBeenCalled();
75+
});
76+
77+
it("keeps the group available until five seconds after the latest action", async () => {
78+
const { undo, claim, options } = setup();
79+
showThreadUndoNotice(options);
80+
vi.advanceTimersByTime(4_000);
81+
const second = ThreadUndo.begin("pin", "env/second");
82+
showThreadUndoNotice({ ...options, claim: second });
83+
const group = notice();
84+
vi.advanceTimersByTime(4_999);
85+
expect(notice().count).toBe(2);
86+
vi.advanceTimersByTime(1);
87+
expect(useThreadUndoNotice.getState().notice).toBeNull();
88+
expect(claim.isCurrent()).toBe(false);
89+
expect(second.isCurrent()).toBe(false);
90+
await group.undo();
91+
expect(undo).not.toHaveBeenCalled();
92+
});
93+
94+
it("reports a failed restore and releases its claim", async () => {
95+
const { add, claim, options } = setup();
96+
showThreadUndoNotice({
97+
...options,
98+
undo: async () => AsyncResult.failure(Cause.fail(new Error("offline"))),
99+
});
100+
await notice().undo();
101+
expect(add).toHaveBeenLastCalledWith(
102+
expect.objectContaining({ type: "error", title: "Restore failed", description: "offline" }),
103+
);
104+
expect(claim.isCurrent()).toBe(false);
105+
});
106+
107+
it("reports a rejected restore promise", async () => {
108+
const { add, options } = setup();
109+
showThreadUndoNotice({
110+
...options,
111+
undo: async () => {
112+
throw new Error("disconnected");
113+
},
114+
});
115+
await notice().undo();
116+
expect(add).toHaveBeenLastCalledWith(
117+
expect.objectContaining({ type: "error", description: "disconnected" }),
118+
);
119+
});
120+
121+
it("does not report interrupted restores as errors", async () => {
122+
const { add, options } = setup();
123+
showThreadUndoNotice({ ...options, undo: async () => AsyncResult.failure(Cause.interrupt()) });
124+
await notice().undo();
125+
expect(add).not.toHaveBeenCalled();
126+
});
127+
128+
it("undoes the latest kind first, then reveals the preceding group", async () => {
129+
const { options } = setup();
130+
const older = vi.fn(async () => AsyncResult.success(undefined));
131+
const newer = vi.fn(async () => AsyncResult.success(undefined));
132+
showThreadUndoNotice({
133+
...options,
134+
action: "Settled",
135+
undo: older,
136+
claim: ThreadUndo.begin("settle", "env/a"),
137+
});
138+
showThreadUndoNotice({
139+
...options,
140+
action: "Snoozed",
141+
undo: newer,
142+
claim: ThreadUndo.begin("snooze", "env/b"),
143+
});
144+
expect(undoLatestThreadAction()).toBe(true);
145+
expect(newer).toHaveBeenCalledOnce();
146+
expect(older).not.toHaveBeenCalled();
147+
expect(notice().action).toBe("Settled");
148+
await notice().undo();
149+
expect(older).toHaveBeenCalledOnce();
150+
expect(undoLatestThreadAction()).toBe(false);
151+
});
152+
});

0 commit comments

Comments
 (0)