Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(chat): pinned page keeps its frame on thread switch and shows loading
Bugfixer 2e537a438951a
diff --git a/src/components/WebPanelPage.test.ts b/src/components/WebPanelPage.test.tsindex 39b4c03d24..2e87d8ebfd 100644--- a/src/components/WebPanelPage.test.ts+++ b/src/components/WebPanelPage.test.ts@@ -11,6 +11,8 @@ const fixture = vi.hoisted(() => ({fetchAppDocument: vi.fn(async (_threadId: string, _messageId: string, _signal?: AbortSignal): Promise<unknown> => ({ srcdoc: "<p>hi</p>", can: {} })),state: { webPanelId: "panel-1" } as { webPanelId?: string; bots?: unknown[] },dispatch: vi.fn(),+ /** Each document an AppFrame was shown with, in order: a frame shown twice with the same page appears twice. */+ framed: [] as string[],api: vi.fn(async (_path: string, _init?: unknown): Promise<unknown> => ({ framing: "allowed" })),}));vi.mock("@/lib/web-panel-preferences", () => ({ useWebPanels: () => fixture.panels, setWebPanels: fixture.setWebPanels }));@@ -22,8 +24,13 @@ vi.mock("./app/app-api", () => ({fetchAppDocument: fixture.fetchAppDocument,}));vi.mock("./app/AppFrame", async () => {- const { createElement: h } = await import("react");- return { AppFrame: (props: { botId: string; threadId: string; fill?: boolean }) => h("div", { "data-frame": `${props.botId}/${props.threadId}`, "data-fill": String(Boolean(props.fill)) }) };+ const { createElement: h, useEffect } = await import("react");+ return {+ AppFrame: (props: { doc: { srcdoc: string }; botId: string; threadId: string; fill?: boolean }) => {+ useEffect(() => { fixture.framed.push(props.doc.srcdoc); }, [props.doc]);+ return h("div", { "data-frame": `${props.botId}/${props.threadId}`, "data-fill": String(Boolean(props.fill)) });+ },+ };});vi.mock("@/state/store", () => ({ useStore: () => ({ state: fixture.state, dispatch: fixture.dispatch }) }));vi.mock("@/state/api", () => ({ api: fixture.api }));@@ -137,4 +144,30 @@ describe("a pinned web panel's page", () => {expect(fixture.fetchAppDocument).toHaveBeenCalledTimes(2);fixture.state = { webPanelId: "panel-1" };});++ it("keeps the frame when its chat is unloaded by a thread switch, and never shows the old page again", async () => {+ fixture.panels = [{ id: "panel-1", name: "Sales", page: { botId: "b1", threadId: "t1", messageId: "m1" } }];+ const withRev = (threadId: string, rev: number) => [{ id: "b1", threadId, messages: [{ id: "m1", kind: "app", app: { title: "Sales", source: "html", doc: "d1", rev } }] }];+ fixture.state = { webPanelId: "panel-1", bots: withRev("t1", 1) };+ fixture.fetchAppDocument.mockClear();+ fixture.fetchAppDocument.mockResolvedValueOnce({ srcdoc: "rev 1", can: {} }).mockResolvedValueOnce({ srcdoc: "rev 2", can: {} });+ fixture.framed = [];+ await act(async () => root.render(createElement(WebPanelPage)));+ fixture.state = { webPanelId: "panel-1", bots: withRev("t2", 1) };+ await act(async () => root.render(createElement(WebPanelPage)));+ expect(fixture.fetchAppDocument).toHaveBeenCalledTimes(1);+ fixture.state = { webPanelId: "panel-1", bots: withRev("t1", 2) };+ await act(async () => root.render(createElement(WebPanelPage)));+ expect(fixture.fetchAppDocument).toHaveBeenCalledTimes(2);+ expect(fixture.framed).toEqual(["rev 1", "rev 2"]);+ fixture.state = { webPanelId: "panel-1" };+ });++ it("shows the page is loading while it is fetched", async () => {+ fixture.panels = [{ id: "panel-1", name: "Sales", page: { botId: "b1", threadId: "t1", messageId: "m1" } }];+ fixture.fetchAppDocument.mockReturnValueOnce(new Promise(() => {}));+ await act(async () => root.render(createElement(WebPanelPage)));+ expect(host.querySelector("[data-frame]")).toBeNull();+ expect(host.querySelector("[data-app-placeholder]")).not.toBeNull();+ });});diff --git a/src/components/WebPanelPage.tsx b/src/components/WebPanelPage.tsxindex b5a0bc30ae..6426a0a7b9 100644--- a/src/components/WebPanelPage.tsx+++ b/src/components/WebPanelPage.tsx@@ -15,6 +15,7 @@ import { useStore, type Bot } from "@/state/store";import type { AppDocument } from "../../shared/app";import { AppDocumentError, fetchAppDocument } from "./app/app-api";import { AppFrame, type AppFrameHandle } from "./app/AppFrame";+import { AppPlaceholder } from "./app/AppChrome";const LOAD_TIMEOUT_MS = 10_000;@@ -108,7 +109,10 @@ function storedPageVersion(bots: readonly Bot[] | undefined, page: PinnedChatPagfunction PinnedChatPageView({ panel, page, panels }: { panel: WebPanel; page: PinnedChatPage; panels: readonly WebPanel[] }) {const { state, dispatch } = useStore();- const version = storedPageVersion(state.bots, page);+ // Only a new stored version means the page changed: while its chat is not loaded (switching threads) it stays put.+ const stored = storedPageVersion(state.bots, page);+ const [version, setVersion] = useState(stored);+ if (stored && stored !== version) setVersion(stored);const [reloads, setReloads] = useState(0);const [load, setLoad] = useState<PageLoad | null>(null);const handle = useRef<AppFrameHandle | null>(null);@@ -150,7 +154,6 @@ function PinnedChatPageView({ panel, page, panels }: { panel: WebPanel; page: Pi{load && "doc" in load ? (<div className="relative min-h-0 flex-1"><AppFrame- key={`${reloads}:${version}`}doc={load.doc}botId={page.botId}threadId={page.threadId}@@ -168,7 +171,11 @@ function PinnedChatPageView({ panel, page, panels }: { panel: WebPanel; page: Pi<p className="text-[14px] font-medium text-ink">{t("webPanels.page.pageGone")}</p><p className="max-w-sm text-[13px] text-ink-secondary">{load.failure}</p></div>- ) : null}+ ) : (+ <div className="flex min-h-0 flex-1 items-center justify-center">+ <AppPlaceholder height={120} loading />+ </div>+ )}</main>);}