mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
fix(studio): two undo presses during one save step back two edits, not one twice
This commit is contained in:
@@ -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<unknown> = Promise.resolve();
|
||||
const serialize = <T>(_paths: readonly string[], task: () => Promise<T>) => {
|
||||
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]> = [
|
||||
|
||||
@@ -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<ApplyResult>,
|
||||
giveBack: () => void,
|
||||
): Promise<ApplyResult> {
|
||||
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<HistoryView>(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],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user