Skip to content

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

fix(phone): back from calendar and apps page, ignore Forward

AdityaUmale2fc1ff26c785
diff --git a/src/App.tsx b/src/App.tsx
index ba806ef501..d77a06b228 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -80,6 +80,7 @@ import { useBotListDrawer } from "@/hooks/use-bot-list-drawer";
import { usePhoneBack } from "@/hooks/use-phone-back";
import { useRememberedSidePanel } from "@/hooks/use-remembered-side-panel";
import { closeSidePanels } from "@/lib/page-open";
+import { phoneBackStep, phoneBackSteps } from "@/lib/phone-back";
import { useDesktopNotificationOpens, useExternalChatLinks } from "@/hooks/use-external-chat-links"; import { useNewBotFromOtherPlace } from "@/lib/places-together";
import { useUsageSnapshot } from "@/hooks/use-usage-snapshot";
import { useReadShownChatOnFocus } from "@/hooks/use-read-shown-chat";
@@ -192,8 +193,13 @@ function Shell({ viewer }: { viewer: WelcomeViewer | null }) {
const panelOpen = anySidePanel(sidePanels);
// On a phone, back closes the open list or side panel, and from a chat picked in the list goes back to the list.
const phoneLayout = useMediaQuery(PHONE_LAYOUT, false);
- usePhoneBack(phoneLayout, drawerBackSteps + (panelOpen ? 1 : 0), () => {
- if (drawerOpen || !panelOpen) return drawerBack();
+ const calendarFocus = state.activeView === "routines" && state.routinesFocus.view === "calendar";
+ const backPlace = { calendar: calendarFocus, drawerOpen, panelOpen, appsPage: state.pluginsOpen, drawerSteps: drawerBackSteps };
+ usePhoneBack(phoneLayout, phoneBackSteps(backPlace), () => {
+ const step = phoneBackStep(backPlace);
+ if (step === "calendar") return dispatch({ type: "showRoutines", view: "list", botId: state.routinesFocus.botId });
+ if (step === "apps-page") dispatch({ type: "togglePlugins", open: false });
+ if (step !== "panels") return drawerBack();
closeSidePanels(dispatch);
dispatch({ type: "toggleNotifications", open: false });
dispatch({ type: "closeForkPanel" });
@@ -204,7 +210,6 @@ function Shell({ viewer }: { viewer: WelcomeViewer | null }) {
const collapseSidebar = sidebarFoldsForPanel(sidePanels, { wide: sidebarAndPanelFit, narrow: sidebarAndNarrowPanelFit });
// Open beside a panel below 1280, the sidebar keeps to 280 so the chat keeps its 440; Simple's bot panel never moves it.
const sidebarBesidePanel = anySidePanel(panelsMovingSidebar(sidePanels)) && !sidebarAndPanelFit;
- const calendarFocus = state.activeView === "routines" && state.routinesFocus.view === "calendar";
const notificationTray = notificationFeed && !calendarFocus;
const noEngines =
diff --git a/src/hooks/use-bot-list-drawer.dom.test.ts b/src/hooks/use-bot-list-drawer.dom.test.ts
index 5f947df895..f437e82fd5 100644
--- a/src/hooks/use-bot-list-drawer.dom.test.ts
+++ b/src/hooks/use-bot-list-drawer.dom.test.ts
@@ -140,3 +140,12 @@ it("keeps no step when the list is closed without going anywhere", async () => {
await press('[data-row="close"]');
expect(backSteps()).toBe(0);
});
+
+it("comes back from the apps page reached from the list, and stays open as the page closes", async () => {
+ await press('[aria-label="Open bot list"]');
+ await show({ ...START, pluginsOpen: true });
+ expect(backSteps()).toBe(1);
+ await press("[data-back]");
+ await show(START);
+ expect(expanded(), "closing the apps page is not going somewhere").toBe("true");
+});
diff --git a/src/hooks/use-bot-list-drawer.ts b/src/hooks/use-bot-list-drawer.ts
index 099685feab..91be3c3ec5 100644
--- a/src/hooks/use-bot-list-drawer.ts
+++ b/src/hooks/use-bot-list-drawer.ts
@@ -37,7 +37,14 @@ export function useBotListDrawer(place: DrawerPlace) {
const leave = useCallback(() => setState((s) => (s.open ? { open: false, fromList: true, first: false } : s)), []);
useEffect(() => {
leave();
- }, [place.selectedId, place.view, place.pluginsOpen, place.settingsOpen, leave]);
+ }, [place.selectedId, place.view, leave]);
+ // Opening the apps page or a bot's settings goes somewhere; closing one (as back does from it) does not.
+ useEffect(() => {
+ if (place.pluginsOpen) leave();
+ }, [place.pluginsOpen, leave]);
+ useEffect(() => {
+ if (place.settingsOpen) leave();
+ }, [place.settingsOpen, leave]);
useEffect(() => {
if (acted.current) leave();
}, [place.botThread, place.groupThread, leave]);
diff --git a/src/hooks/use-phone-back.dom.test.ts b/src/hooks/use-phone-back.dom.test.ts
index 92df441a9c..c820846642 100644
--- a/src/hooks/use-phone-back.dom.test.ts
+++ b/src/hooks/use-phone-back.dom.test.ts
@@ -22,21 +22,34 @@ function Stack() {
}
const show = (enabled: boolean, steps: number) => act(async () => root.render(h(Shell, { enabled, steps })));
-const pop = () => act(async () => { window.dispatchEvent(new PopStateEvent("popstate")); });
+/** The browser's history: one state per entry, and where the page is now. */
+let entries: unknown[] = [null];
+const pop = () => act(async () => { window.dispatchEvent(new PopStateEvent("popstate", { state: entries[above] ?? null })); });
/** The phone's back button: the browser leaves the top entry, then tells the page. */
const pressBack = async () => {
above = Math.max(0, above - 1);
await pop();
};
+/** The browser's Forward: back onto the entry above, if there is one. */
+const pressForward = async () => {
+ above = Math.min(entries.length - 1, above + 1);
+ await pop();
+};
beforeEach(async () => {
above = 0;
+ entries = [null];
back.mockReset();
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
- vi.spyOn(window.history, "pushState").mockImplementation(() => { above += 1; });
+ vi.spyOn(window.history, "state", "get").mockImplementation(() => entries[above] ?? null);
+ vi.spyOn(window.history, "replaceState").mockImplementation((state: unknown) => { entries[above] = state; });
+ vi.spyOn(window.history, "pushState").mockImplementation((state: unknown) => {
+ above += 1;
+ entries = [...entries.slice(0, above), state];
+ });
vi.spyOn(window.history, "go").mockImplementation((delta = 0) => {
above = Math.max(0, above + delta);
- queueMicrotask(() => window.dispatchEvent(new PopStateEvent("popstate")));
+ queueMicrotask(() => window.dispatchEvent(new PopStateEvent("popstate", { state: entries[above] ?? null })));
});
container = document.createElement("div");
document.body.append(container);
@@ -93,3 +106,24 @@ it("takes every entry away when the window grows past the phone layout", async (
await pop();
expect(back).not.toHaveBeenCalled();
});
+
+it("does not count Forward onto an entry it stepped back from as a back press", async () => {
+ await act(async () => root.render(h(Stack)));
+ await pressBack();
+ expect(container.textContent).toBe("1");
+ await pressForward();
+ expect(back, "Forward is not a step back").toHaveBeenCalledTimes(1);
+ expect(container.textContent).toBe("1");
+ expect(above, "the entry Forward reached is taken away again").toBe(1);
+ await pressBack();
+ expect(back).toHaveBeenCalledTimes(2);
+ expect(container.textContent, "the next back press still steps back").toBe("0");
+});
+
+it("still steps back once onto an entry whose state the app replaced", async () => {
+ await act(async () => root.render(h(Stack)));
+ entries[1] = null;
+ await pressBack();
+ expect(back).toHaveBeenCalledTimes(1);
+ expect(container.textContent).toBe("1");
+});
diff --git a/src/hooks/use-phone-back.ts b/src/hooks/use-phone-back.ts
index 15987f9518..375247e270 100644
--- a/src/hooks/use-phone-back.ts
+++ b/src/hooks/use-phone-back.ts
@@ -1,11 +1,25 @@
import { useEffect, useReducer, useRef } from "react";
import { useStableCallback } from "./use-stable-callback";
+/** Each entry the hook adds keeps its place above the start in its history state, so a press can tell Back from Forward. */
+const ENTRY = "ombPhoneBack";
+
+function entryOf(state: unknown): number | undefined {
+ const entry = typeof state === "object" && state !== null ? (state as Record<string, unknown>)[ENTRY] : undefined;
+ return typeof entry === "number" ? entry : undefined;
+}
+
+function withEntry(entry: number): Record<string, unknown> {
+ const state: unknown = window.history.state;
+ return { ...(typeof state === "object" && state !== null ? state : {}), [ENTRY]: entry };
+}
+
/**
* Lets a phone's back button (Android's, or a phone browser's) step back inside the app instead of leaving it. While
* `enabled`, the page keeps one history entry above where it started for each of the `steps` the person can go back,
* and a back press calls `back` once. A step closed in the app takes its entry away again, so back never does nothing;
- * a press that leaves the steps as they were gets its entry back.
+ * a press that leaves the steps as they were gets its entry back. Forward onto an entry the app has already stepped back
+ * from is not a step: the app stays where it is and the entry is taken away again.
*/
export function usePhoneBack(enabled: boolean, steps: number, back: () => void): void {
const added = useRef(0);
@@ -15,7 +29,8 @@ export function usePhoneBack(enabled: boolean, steps: number, back: () => void):
useEffect(() => {
const wanted = enabled ? steps : 0;
if (wanted > added.current) {
- for (let entry = added.current; entry < wanted; entry += 1) window.history.pushState(window.history.state, "", window.location.href);
+ if (added.current === 0) window.history.replaceState(withEntry(0), "", window.location.href);
+ for (let entry = added.current + 1; entry <= wanted; entry += 1) window.history.pushState(withEntry(entry), "", window.location.href);
} else if (wanted < added.current) {
ownPops.current += 1;
window.history.go(wanted - added.current);
@@ -23,13 +38,20 @@ export function usePhoneBack(enabled: boolean, steps: number, back: () => void):
added.current = wanted;
});
useEffect(() => {
- const onPop = () => {
+ const onPop = (event: PopStateEvent) => {
if (ownPops.current > 0) {
ownPops.current -= 1;
return;
}
if (added.current === 0) return;
- added.current -= 1;
+ const entry = entryOf(event.state);
+ if (entry !== undefined && entry >= added.current) {
+ added.current = entry;
+ redraw();
+ return;
+ }
+ // An entry whose state the app replaced since lost its place; count it as one step back, as before.
+ added.current = entry ?? added.current - 1;
stepBack();
redraw();
};
diff --git a/src/lib/phone-back.test.ts b/src/lib/phone-back.test.ts
new file mode 100644
index 0000000000..f4647bec3b
--- /dev/null
+++ b/src/lib/phone-back.test.ts
@@ -0,0 +1,30 @@
+import { describe, expect, it } from "vitest";
+import { phoneBackStep, phoneBackSteps, type PhoneBackPlace } from "./phone-back";
+
+const CHAT: PhoneBackPlace = { calendar: false, drawerOpen: false, panelOpen: false, appsPage: false, drawerSteps: 0 };
+
+describe("the phone's back button", () => {
+ it("has nothing to step back from on the first chat, so back leaves the app", () => {
+ expect(phoneBackSteps(CHAT)).toBe(0);
+ });
+
+ it("goes from the automations calendar back to the list first, even when the hidden bot list has steps", () => {
+ const calendar = { ...CHAT, calendar: true, drawerSteps: 1 };
+ expect(phoneBackSteps(calendar)).toBe(2);
+ expect(phoneBackStep(calendar)).toBe("calendar");
+ expect(phoneBackStep({ ...calendar, calendar: false })).toBe("drawer");
+ });
+
+ it("closes the apps page reached from the list as the list comes back", () => {
+ const apps = { ...CHAT, appsPage: true, drawerSteps: 1 };
+ expect(phoneBackSteps(apps)).toBe(1);
+ expect(phoneBackStep(apps)).toBe("apps-page");
+ expect(phoneBackStep({ ...apps, drawerOpen: true }), "the list open over it closes first").toBe("drawer");
+ });
+
+ it("closes the open list before a side panel, and a side panel before going back to the list", () => {
+ expect(phoneBackStep({ ...CHAT, drawerOpen: true, panelOpen: true, drawerSteps: 1 })).toBe("drawer");
+ expect(phoneBackStep({ ...CHAT, panelOpen: true, drawerSteps: 1 })).toBe("panels");
+ expect(phoneBackSteps({ ...CHAT, panelOpen: true, drawerSteps: 1 })).toBe(2);
+ });
+});
diff --git a/src/lib/phone-back.ts b/src/lib/phone-back.ts
new file mode 100644
index 0000000000..540608c35f
--- /dev/null
+++ b/src/lib/phone-back.ts
@@ -0,0 +1,28 @@
+/** What is on a phone's screen that the back button can step back from, nearest first. */
+export interface PhoneBackPlace {
+ /** The automations calendar, which covers the list and the menu button. */
+ calendar: boolean;
+ drawerOpen: boolean;
+ panelOpen: boolean;
+ /** The apps page is open; it stays open under the list unless back closes it. */
+ appsPage: boolean;
+ /** Steps the bot list keeps: the open list, and a screen reached from it. */
+ drawerSteps: number;
+}
+
+export type PhoneBackStep = "calendar" | "drawer" | "panels" | "apps-page";
+
+export function phoneBackSteps(place: PhoneBackPlace): number {
+ return (place.calendar ? 1 : 0) + (place.panelOpen ? 1 : 0) + place.drawerSteps;
+}
+
+/**
+ * One back press: the calendar goes back to the automations list, then the open list closes, then the side panels;
+ * the apps page reached from the list closes as the list comes back.
+ */
+export function phoneBackStep(place: PhoneBackPlace): PhoneBackStep {
+ if (place.calendar) return "calendar";
+ if (place.drawerOpen) return "drawer";
+ if (place.panelOpen) return "panels";
+ return place.appsPage && place.drawerSteps > 0 ? "apps-page" : "drawer";
+}