From 035aa1d3ab44063f3c0fdda10e90bd58b13c744e Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Fri, 2 Oct 2026 21:59:16 -0700 Subject: [PATCH] fix(studio): two undo presses during one save step back two edits, not one twice --- .../hooks/usePersistentEditHistory.test.ts | 46 ++++++++++++++++++- .../src/hooks/usePersistentEditHistory.ts | 30 ++++++++++-- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/packages/studio/src/hooks/usePersistentEditHistory.test.ts b/packages/studio/src/hooks/usePersistentEditHistory.test.ts index 304203a625..cea27a7714 100644 --- a/packages/studio/src/hooks/usePersistentEditHistory.test.ts +++ b/packages/studio/src/hooks/usePersistentEditHistory.test.ts @@ -135,8 +135,15 @@ it("a second undo pressed while the same edit saves undoes the edit before it", }), ); - const first = await act(() => hook().undo({ readFile, claimedAfter: atKey })); - const second = await act(() => hook().undo({ readFile, claimedAfter: atKey })); + let queue: Promise = Promise.resolve(); + const serialize = (_paths: readonly string[], task: () => Promise) => { + const run = queue.then(task); + queue = run.catch(() => undefined); + return run; + }; + const press = () => hook().undo({ readFile, claimedAfter: atKey, serialize }); + + const [first, second] = await act(() => Promise.all([press(), press()])); expect([first, second]).toMatchObject([ { ok: true, label: "Undid: Moved Card" }, @@ -145,6 +152,41 @@ it("a second undo pressed while the same edit saves undoes the edit before it", expect(file()).toBe("A"); }); +it("gives a claim back when its undo fails, so the next press targets that edit again", async () => { + const { hook, save, readFile } = await studio(); + save("B"); + await act(() => + hook().recordEdit({ + label: "Moved Title", + files: { "index.html": { before: "A", after: "B" } }, + }), + ); + await vi.waitFor(() => expect(hook().undoLabel).toBe("Moved Title")); + const atKey = hook().claims(); + save("C"); + await act(() => + hook().recordEdit({ + label: "Moved Card", + files: { "index.html": { before: "B", after: "C" } }, + }), + ); + const posted: string[] = []; + const fetch = globalThis.fetch; + vi.stubGlobal("fetch", (url: string, init?: RequestInit) => { + if (init?.method === "POST") posted.push(url.slice(url.lastIndexOf("/") + 1)); + return fetch(url, init); + }); + + save("edited elsewhere"); + const failed = await act(() => hook().undo({ readFile, claimedAfter: atKey })); + save("C"); + const retried = await act(() => hook().undo({ readFile, claimedAfter: atKey })); + + expect(failed.ok).toBe(false); + expect(retried).toMatchObject({ ok: true, label: "Undid: Moved Card" }); + expect(posted.filter((p) => p === "undo" || p === "step")).toEqual(["undo", "undo"]); +}); + it("a drag's edits under one key undo as one step, even before the drag goes idle", async () => { const { hook, file, save, readFile } = await studio(); const writes: Array<[before: string, after: string]> = [ diff --git a/packages/studio/src/hooks/usePersistentEditHistory.ts b/packages/studio/src/hooks/usePersistentEditHistory.ts index 6eeaad81a4..b08a5e1ac0 100644 --- a/packages/studio/src/hooks/usePersistentEditHistory.ts +++ b/packages/studio/src/hooks/usePersistentEditHistory.ts @@ -81,8 +81,13 @@ function createOwnHistory() { noteClaim: (id: string) => { claimed = { count: claimed.count + 1, id }; }, - releaseUndoneClaim: (id: string | null) => { - if (id && claimed.id === id) claimed = { ...claimed, id: "" }; + takeClaim: (id: string | null) => { + if (!id || claimed.id !== id) return () => {}; + const taken = claimed; + claimed = { ...claimed, id: "" }; + return () => { + if (claimed.count === taken.count) claimed = taken; + }; }, stepped: (entry: { id: string; undoes?: string }) => { const undone = entry.undoes ? own.get(entry.undoes) : undefined; @@ -197,6 +202,20 @@ function redoneAt(view: HistoryView): number | null { } /** Studio's undo over the server's project history: an edit claims what it wrote; Cmd+Z steps the person's. */ +async function givingClaimBackOnFailure( + attempt: () => Promise, + giveBack: () => void, +): Promise { + try { + const result = await attempt(); + if (!result.ok) giveBack(); + return result; + } catch (error) { + giveBack(); + throw error; + } +} + export function usePersistentEditHistory({ projectId }: UsePersistentEditHistoryOptions) { const [view, setView] = useState(EMPTY); const [loaded, setLoaded] = useState(false); @@ -256,6 +275,7 @@ export function usePersistentEditHistory({ projectId }: UsePersistentEditHistory direction === "undo" && callbacks.claimedAfter !== undefined ? own.claimedAfter(callbacks.claimedAfter) : null; + const giveBack = own.takeClaim(target); const next = direction === "undo" ? view.back : view.forward; const stepPaths = target ? Object.keys(own.afterOf(target)) : next?.paths; const paths = unionPaths(stepPaths, heldClaimRef.current?.paths); @@ -279,7 +299,6 @@ export function usePersistentEditHistory({ projectId }: UsePersistentEditHistory } if (!reply.entry) return { ok: false, reason: "empty" }; own.stepped(reply.entry); - own.releaseUndoneClaim(target); const changed = reply.entry.files.map((file) => file.path); return { ok: true, @@ -293,7 +312,10 @@ export function usePersistentEditHistory({ projectId }: UsePersistentEditHistory ), }; }; - return callbacks.serialize ? callbacks.serialize(paths, run) : run(); + return givingClaimBackOnFailure( + () => (callbacks.serialize ? callbacks.serialize(paths, run) : run()), + giveBack, + ); }, [projectId, view, refresh, own], );