Repository navigation
fix(web): show thread undo notice in the sidebar #12972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb67000
fix(web): show thread undo notice in the sidebar
juliusmarminge 346c67a
fix(web): use the shared button for thread undo
juliusmarminge 2d02e78
fix(web): preserve context for thread undo shortcuts
juliusmarminge 2c2548e
fix(web): contain the sidebar undo notice
juliusmarminge ff103d8
fix(web): use inline button for the undo notice
juliusmarminge 0649299
fix(web): add a sidebar variant to the alert primitive
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
apps/web/src/components/sidebar/SidebarThreadUndoNotice.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { useEffect } from "react"; | ||
| import { useAtomValue } from "@effect/atom-react"; | ||
|
|
||
| import { undoLatestThreadAction, useThreadUndoNotice } from "../../hooks/showThreadUndoNotice"; | ||
| import { resolveShortcutCommand, shortcutLabelForCommand } from "../../keybindings"; | ||
| import { isCommandPaletteOpen } from "../../commandPaletteBus"; | ||
| import { isEditableFocused } from "../../lib/editableFocus"; | ||
| import { isTerminalFocused } from "../../lib/terminalFocus"; | ||
| import { isModelPickerOpen } from "../../modelPickerVisibility"; | ||
| import { primaryServerKeybindingsAtom } from "../../state/server"; | ||
| import { Button } from "../ui/button"; | ||
|
|
||
| export function SidebarThreadUndoNotice() { | ||
| const notice = useThreadUndoNotice((state) => state.notice); | ||
| const keybindings = useAtomValue(primaryServerKeybindingsAtom); | ||
| useEffect(() => { | ||
| const onKeyDown = (event: KeyboardEvent) => { | ||
| if (event.defaultPrevented || event.repeat || isCommandPaletteOpen()) return; | ||
| const command = resolveShortcutCommand(event, keybindings, { | ||
| context: { | ||
|
juliusmarminge marked this conversation as resolved.
Outdated
|
||
| terminalFocus: isTerminalFocused(), | ||
| editableFocus: isEditableFocused(event.target), | ||
| modelPickerOpen: isModelPickerOpen(), | ||
| }, | ||
| }); | ||
| if (command === "thread.undo" && undoLatestThreadAction()) { | ||
| event.preventDefault(); | ||
| event.stopPropagation(); | ||
| } | ||
| }; | ||
| window.addEventListener("keydown", onKeyDown); | ||
| return () => window.removeEventListener("keydown", onKeyDown); | ||
| }, [keybindings]); | ||
|
|
||
| if (!notice) return null; | ||
| const shortcut = shortcutLabelForCommand(keybindings, "thread.undo"); | ||
|
|
||
| return ( | ||
| <div role="status" className="px-2 py-1.5 text-[11px] text-sidebar-muted-foreground"> | ||
|
juliusmarminge marked this conversation as resolved.
Outdated
|
||
| {notice.action} {notice.count} thread{notice.count === 1 ? "" : "s"},{" "} | ||
| <Button | ||
| variant="link" | ||
| size="micro" | ||
| onClick={undoLatestThreadAction} | ||
|
macroscopeapp[bot] marked this conversation as resolved.
Outdated
|
||
| className="h-auto border-0 p-0 font-normal hover:text-sidebar-foreground" | ||
| > | ||
| {shortcut ? `${shortcut} to undo` : "Undo"} | ||
| </Button> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { AsyncResult } from "effect/unstable/reactivity"; | ||
| import * as Cause from "effect/Cause"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import { toastManager } from "../components/ui/toast"; | ||
| import { | ||
| showThreadUndoNotice, | ||
| undoLatestThreadAction, | ||
| useThreadUndoNotice, | ||
| } from "./showThreadUndoNotice"; | ||
| import * as ThreadUndo from "./threadUndo"; | ||
|
|
||
| beforeEach(() => vi.useFakeTimers()); | ||
| afterEach(() => { | ||
| vi.runAllTimers(); | ||
| vi.useRealTimers(); | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| function setup() { | ||
| const add = vi.spyOn(toastManager, "add").mockReturnValue("error-toast"); | ||
| const undo = vi.fn(async () => AsyncResult.success(undefined)); | ||
| const claim = ThreadUndo.begin("pin", "env/thread"); | ||
| const options = { action: "Unpinned" as const, failureTitle: "Restore failed", undo, claim }; | ||
| return { add, undo, claim, options }; | ||
| } | ||
|
|
||
| function notice() { | ||
| const value = useThreadUndoNotice.getState().notice; | ||
| if (!value) throw new Error("Undo notice is missing"); | ||
| return value; | ||
| } | ||
|
|
||
| describe("thread undo notice", () => { | ||
| it("aggregates consecutive actions without success toasts and restores the group once", async () => { | ||
| const { add, undo, options } = setup(); | ||
| showThreadUndoNotice({ | ||
| ...options, | ||
| action: "Settled", | ||
| claim: ThreadUndo.begin("settle", "env/a"), | ||
| }); | ||
| showThreadUndoNotice({ | ||
| ...options, | ||
| action: "Settled", | ||
| claim: ThreadUndo.begin("settle", "env/b"), | ||
| }); | ||
| expect(notice()).toMatchObject({ action: "Settled", count: 2 }); | ||
| expect(add).not.toHaveBeenCalled(); | ||
| const group = notice(); | ||
| await group.undo(); | ||
| await group.undo(); | ||
| expect(undo).toHaveBeenCalledTimes(2); | ||
| expect(undoLatestThreadAction()).toBe(false); | ||
| }); | ||
|
|
||
| it("drops invalidated claims immediately and rejects a captured stale undo", async () => { | ||
| const { undo, options } = setup(); | ||
| showThreadUndoNotice(options); | ||
| const stale = notice(); | ||
| ThreadUndo.invalidate("pin", "env/thread"); | ||
| expect(useThreadUndoNotice.getState().notice).toBeNull(); | ||
| showThreadUndoNotice({ ...options, claim: ThreadUndo.begin("pin", "env/thread") }); | ||
| await stale.undo(); | ||
| expect(undo).not.toHaveBeenCalled(); | ||
| await notice().undo(); | ||
| expect(undo).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it("does not show a notice for a late completion after a newer action", () => { | ||
| const { add, options } = setup(); | ||
| ThreadUndo.invalidate("pin", "env/thread"); | ||
| showThreadUndoNotice(options); | ||
| expect(useThreadUndoNotice.getState().notice).toBeNull(); | ||
| expect(add).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("keeps the group available until five seconds after the latest action", async () => { | ||
| const { undo, claim, options } = setup(); | ||
| showThreadUndoNotice(options); | ||
| vi.advanceTimersByTime(4_000); | ||
| const second = ThreadUndo.begin("pin", "env/second"); | ||
| showThreadUndoNotice({ ...options, claim: second }); | ||
| const group = notice(); | ||
| vi.advanceTimersByTime(4_999); | ||
| expect(notice().count).toBe(2); | ||
| vi.advanceTimersByTime(1); | ||
| expect(useThreadUndoNotice.getState().notice).toBeNull(); | ||
| expect(claim.isCurrent()).toBe(false); | ||
| expect(second.isCurrent()).toBe(false); | ||
| await group.undo(); | ||
| expect(undo).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("reports a failed restore and releases its claim", async () => { | ||
| const { add, claim, options } = setup(); | ||
| showThreadUndoNotice({ | ||
| ...options, | ||
| undo: async () => AsyncResult.failure(Cause.fail(new Error("offline"))), | ||
| }); | ||
| await notice().undo(); | ||
| expect(add).toHaveBeenLastCalledWith( | ||
| expect.objectContaining({ type: "error", title: "Restore failed", description: "offline" }), | ||
| ); | ||
| expect(claim.isCurrent()).toBe(false); | ||
| }); | ||
|
|
||
| it("reports a rejected restore promise", async () => { | ||
| const { add, options } = setup(); | ||
| showThreadUndoNotice({ | ||
| ...options, | ||
| undo: async () => { | ||
| throw new Error("disconnected"); | ||
| }, | ||
| }); | ||
| await notice().undo(); | ||
| expect(add).toHaveBeenLastCalledWith( | ||
| expect.objectContaining({ type: "error", description: "disconnected" }), | ||
| ); | ||
| }); | ||
|
|
||
| it("does not report interrupted restores as errors", async () => { | ||
| const { add, options } = setup(); | ||
| showThreadUndoNotice({ ...options, undo: async () => AsyncResult.failure(Cause.interrupt()) }); | ||
| await notice().undo(); | ||
| expect(add).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("undoes the latest kind first, then reveals the preceding group", async () => { | ||
| const { options } = setup(); | ||
| const older = vi.fn(async () => AsyncResult.success(undefined)); | ||
| const newer = vi.fn(async () => AsyncResult.success(undefined)); | ||
| showThreadUndoNotice({ | ||
| ...options, | ||
| action: "Settled", | ||
| undo: older, | ||
| claim: ThreadUndo.begin("settle", "env/a"), | ||
| }); | ||
| showThreadUndoNotice({ | ||
| ...options, | ||
| action: "Snoozed", | ||
| undo: newer, | ||
| claim: ThreadUndo.begin("snooze", "env/b"), | ||
| }); | ||
| expect(undoLatestThreadAction()).toBe(true); | ||
| expect(newer).toHaveBeenCalledOnce(); | ||
| expect(older).not.toHaveBeenCalled(); | ||
| expect(notice().action).toBe("Settled"); | ||
| await notice().undo(); | ||
| expect(older).toHaveBeenCalledOnce(); | ||
| expect(undoLatestThreadAction()).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.