Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(page): keep the page up, with a reload, when a part of it throws (#4057)
Milind Soniee7d52ce66b6
diff --git a/src/components/PageBoundary.test.ts b/src/components/PageBoundary.test.tsnew file mode 100644index 0000000000..d05e91159a--- /dev/null+++ b/src/components/PageBoundary.test.ts@@ -0,0 +1,42 @@+// @vitest-environment happy-dom+import { createElement, useEffect } from "react";+import { flushSync } from "react-dom";+import { createRoot } from "react-dom/client";+import { afterEach, describe, expect, it, vi } from "vitest";++import { PageBoundary } from "./PageBoundary";++function Broken() {+ useEffect(() => {+ throw new TypeError("desktop.shown is not a function");+ }, []);+ return createElement("p", null, "the page");+}++describe("PageBoundary", () => {+ afterEach(() => vi.restoreAllMocks());++ it("keeps the page up, with the error and a reload, when a part of it throws in an effect", async () => {+ vi.spyOn(console, "error").mockImplementation(() => {});+ const host = document.createElement("div");+ document.body.append(host);+ const root = createRoot(host);+ flushSync(() => root.render(createElement(PageBoundary, null, createElement(Broken))));+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(host.textContent).toContain("This page hit an error.");+ expect(host.textContent).toContain("desktop.shown is not a function");+ expect(host.querySelector("button")?.textContent).toBe("Reload");+ root.unmount();+ host.remove();+ });++ it("shows the page itself while nothing throws", () => {+ const host = document.createElement("div");+ document.body.append(host);+ const root = createRoot(host);+ flushSync(() => root.render(createElement(PageBoundary, null, createElement("p", null, "the page"))));+ expect(host.textContent).toBe("the page");+ root.unmount();+ host.remove();+ });+});diff --git a/src/components/PageBoundary.tsx b/src/components/PageBoundary.tsxnew file mode 100644index 0000000000..249221fa31--- /dev/null+++ b/src/components/PageBoundary.tsx@@ -0,0 +1,29 @@+import { Component, type ReactNode } from "react";++import { t } from "@/lib/i18n";++// React unmounts everything below a root that has no error boundary when a render or an effect throws (react.dev,+// "Catching rendering errors with an error boundary"), which left a page showing only the body's background.+export class PageBoundary extends Component<{ children: ReactNode }, { error: Error | null }> {+ state: { error: Error | null } = { error: null };++ static getDerivedStateFromError(error: unknown) {+ return { error: error instanceof Error ? error : new Error(String(error)) };+ }++ render() {+ if (!this.state.error) return this.props.children;+ return (+ <div role="alert" className="flex min-h-screen items-center justify-center p-6 text-center">+ <div className="max-w-md space-y-3">+ <h1 className="text-lg font-semibold">{t("page.broken.title")}</h1>+ <p className="text-sm opacity-75">{t("page.broken.body")}</p>+ <p className="break-words text-xs opacity-60">{this.state.error.message}</p>+ <button type="button" className="rounded-md border px-3 py-1.5 text-sm" onClick={() => location.reload()}>+ {t("page.broken.reload")}+ </button>+ </div>+ </div>+ );+ }+}diff --git a/src/locales/en.json b/src/locales/en.jsonindex f7b06a4fac..c880eb8555 100644--- a/src/locales/en.json+++ b/src/locales/en.json@@ -6994,6 +6994,9 @@"app.info.off": "Always off","app.info.offList": "Camera, microphone, location, clipboard, pop-ups and downloads","app.reload": "Reload app",+ "page.broken.title": "This page hit an error.",+ "page.broken.body": "Reload to carry on where you were.",+ "page.broken.reload": "Reload","app.open": "Open app","app.openAgain": "Open again","app.loading": "Opening the app",diff --git a/src/main.tsx b/src/main.tsxindex 2621c72859..a152ca53ba 100644--- a/src/main.tsx+++ b/src/main.tsx@@ -12,6 +12,7 @@ import { settleAdvancedModeDefault } from "./lib/interface-mode";import { BrowserSignInPage } from "./pair/BrowserSignInPage";import { PairPage } from "./pair/PairPage";import { HomeScreen, SignedOutAtHome } from "./components/HomeScreen";+import { PageBoundary } from "./components/PageBoundary";import { endedBeforeReload, forgetEndedSession, SESSION_ENDED_REASON } from "./lib/session-ended";import "./styles.css";@@ -53,5 +54,5 @@ async function chooseRoot(): Promise<React.ReactNode> {}void Promise.all([bootstrapBrand(), chooseRoot()]).then(([, root]) => {- createRoot(document.getElementById("root")!).render(<StrictMode>{root}</StrictMode>);+ createRoot(document.getElementById("root")!).render(<StrictMode><PageBoundary>{root}</PageBoundary></StrictMode>);});