Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(phone): re-add the reopened step's entry when its own back lands below it

AdityaUmalee89c279ae08a
diff --git a/src/hooks/use-phone-back.dom.test.ts b/src/hooks/use-phone-back.dom.test.ts
index cf172a65a3..0b80516687 100644
--- a/src/hooks/use-phone-back.dom.test.ts
+++ b/src/hooks/use-phone-back.dom.test.ts
@@ -172,3 +172,22 @@ it("leaves the entry its own step back lands on marked for the step opened again
await pressBack();
expect(back).toHaveBeenCalledTimes(1);
});
+
+it("adds the reopened step's entry again when its own step back lands below it", async () => {
+ const settle: Array<() => void> = [];
+ vi.mocked(window.history.go).mockImplementation((delta = 0) => {
+ // The browser picks where the traversal lands when it starts, before the reopened step's entry is added.
+ const target = Math.max(0, above + delta);
+ settle.push(() => {
+ above = target;
+ window.dispatchEvent(new PopStateEvent("popstate", { state: entries[above] ?? null }));
+ });
+ });
+ await show(true, 1);
+ await show(true, 0);
+ await show(true, 1);
+ await act(async () => { for (const step of settle.splice(0)) step(); });
+ expect(entries[above], "the page is on the open step's entry again").toEqual({ ombPhoneBack: 1 });
+ await pressBack();
+ expect(back, "back closes the open step instead of leaving the app").toHaveBeenCalledTimes(1);
+});
diff --git a/src/hooks/use-phone-back.ts b/src/hooks/use-phone-back.ts
index f4f1088af2..078f9410d2 100644
--- a/src/hooks/use-phone-back.ts
+++ b/src/hooks/use-phone-back.ts
@@ -44,6 +44,13 @@ export function usePhoneBack(enabled: boolean, steps: number, back: () => void):
const onPop = (event: PopStateEvent) => {
if (ownPops.current > 0) {
ownPops.current -= 1;
+ // A step opened again while the app's own step back was on its way may have been added above the entry it left;
+ // count from the entry the page landed on so the effect adds back what is missing.
+ const landed = entryOf(event.state);
+ if (ownPops.current === 0 && landed !== undefined && landed < added.current) {
+ added.current = landed;
+ redraw();
+ }
return;
}
if (added.current === 0) return;