Skip to content

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

Let the computer panel grow as wide as the window allows

Bugfixer 2e815c4456184
diff --git a/src/components/ComputerPanel.tsx b/src/components/ComputerPanel.tsx
index a10cc697b6..1e0a351706 100644
--- a/src/components/ComputerPanel.tsx
+++ b/src/components/ComputerPanel.tsx
@@ -42,7 +42,7 @@ import { LocalComputerAutoWarning } from "./LocalComputerAutoWarning";
import { busyBoatView, instanceSupportsLocalComputer, localComputerDisabledReason, localComputerSelectable, pageOnRemoteServer, persistedComputerSelectionMatches, isReadyBoatState, resolveBoatPanelAction, shouldPollCloudPreview } from "@/lib/local-computer";
import { moveBetweenTabs, PANEL_TABS, panelTabProps, scrollTabsWithWheel, TAB_PANEL_FOCUS, tabPanelProps, useAndroidTabFallback } from "./computer-panel-tabs";
import { releaseComputerPanelFocus, useComputerPanelEntry } from "@/lib/computer-panel-focus";
-import { PANEL_MAX_WIDTH, PANEL_MIN_WIDTH, useComputerPanelResize } from "./computer-panel-resize";
+import { PANEL_MIN_WIDTH, useComputerPanelResize } from "./computer-panel-resize";
import {
computerPanelTabs,
readComputerPanelView,
@@ -124,7 +124,7 @@ export function ComputerPanel({
onOpenVmWorkspace?: (botId: string) => void;
}) {
const { dragProps } = useCaptionChrome();
- const { panelWidth, separatorRef, separatorWidth, onSeparatorKeyDown, onResizeStart, onResizeMove, onResizeEnd } = useComputerPanelResize();
+ const { panelWidth, maxWidth, separatorRef, separatorWidth, onSeparatorKeyDown, onResizeStart, onResizeMove, onResizeEnd } = useComputerPanelResize();
const { state, dispatch, flushBotPatches } = useStore();
const liveTask = profileBot.tasks?.find((task) => task.threadId === profileBot.threadId);
const livePlace = effectivePlace(profileBot, liveTask);
@@ -1199,7 +1199,7 @@ export function ComputerPanel({
aria-orientation="vertical"
aria-label={t("computer.resizeAria")}
aria-valuemin={PANEL_MIN_WIDTH}
- aria-valuemax={PANEL_MAX_WIDTH}
+ aria-valuemax={maxWidth}
aria-valuenow={separatorWidth ?? undefined}
tabIndex={0}
onKeyDown={onSeparatorKeyDown}
diff --git a/src/components/computer-panel-resize.test.ts b/src/components/computer-panel-resize.test.ts
index feeb474dc5..74f3a9c9d6 100644
--- a/src/components/computer-panel-resize.test.ts
+++ b/src/components/computer-panel-resize.test.ts
@@ -3,27 +3,63 @@ import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { PANEL_MAX_WIDTH, useComputerPanelResize } from "./computer-panel-resize";
+import { CHAT_MIN_WIDTH, PANEL_MAX_WIDTH, useComputerPanelResize } from "./computer-panel-resize";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
let host: HTMLDivElement;
let root: Root;
let width = 0;
+let maxWidth = 0;
+// The room the chat and the panel share in the window's row.
+let room = 0;
+let observers: (() => void)[] = [];
function Panel() {
- const { panelWidth, separatorRef, onSeparatorKeyDown } = useComputerPanelResize();
+ const { panelWidth, maxWidth: max, separatorRef, onSeparatorKeyDown } = useComputerPanelResize();
width = panelWidth;
- return createElement("aside", null, createElement("div", { ref: separatorRef, role: "separator", tabIndex: 0, onKeyDown: onSeparatorKeyDown }));
+ maxWidth = max;
+ return createElement("aside", { style: { width: panelWidth } }, createElement("div", { ref: separatorRef, role: "separator", tabIndex: 0, onKeyDown: onSeparatorKeyDown }));
+}
+
+function ChatAndPanel() {
+ return createElement("div", null, createElement("main"), createElement(Panel));
}
const press = (key: string) => act(() => {
host.querySelector("[role=separator]")!.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true }));
});
+// Like a browser, the observers fire again each time the panel's width changes,
+// until the layout settles.
+const resizeWindow = (next: number) => {
+ room = next;
+ for (let round = 0; round < 10; round++) {
+ const before = width;
+ act(() => {
+ for (const notify of observers) notify();
+ });
+ if (round > 0 && width === before) return;
+ }
+ throw new Error("the panel width never settled");
+};
+
const stored = new Map<string, string>();
beforeEach(() => {
stored.clear();
+ observers = [];
vi.stubGlobal("localStorage", { getItem: (key: string) => stored.get(key) ?? null, setItem: (key: string, value: string) => void stored.set(key, value) });
+ vi.stubGlobal("ResizeObserver", class {
+ constructor(private notify: () => void) {}
+ observe() { observers.push(this.notify); }
+ disconnect() { observers = observers.filter((notify) => notify !== this.notify); }
+ });
+ vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockImplementation(function (this: HTMLElement) {
+ const panel = this.tagName === "ASIDE" ? this : this.parentElement?.querySelector<HTMLElement>(":scope > aside");
+ const panelWidth = Number.parseFloat(panel?.style.width ?? "") || 0;
+ if (this.tagName === "ASIDE") return panelWidth;
+ if (this.tagName === "MAIN") return Math.max(0, room - panelWidth);
+ return 0;
+ });
host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
@@ -32,6 +68,7 @@ afterEach(() => {
act(() => root.unmount());
host.remove();
vi.unstubAllGlobals();
+ vi.restoreAllMocks();
});
describe("the computer panel's resize edge", () => {
@@ -47,9 +84,51 @@ describe("the computer panel's resize edge", () => {
expect(localStorage.getItem("omb-computer-panel-width")).toBe(String(PANEL_MAX_WIDTH - 40));
});
- it("ignores a stored width outside the bounds", () => {
- localStorage.setItem("omb-computer-panel-width", "5000");
+ it("ignores a stored width below the minimum", () => {
+ localStorage.setItem("omb-computer-panel-width", "100");
act(() => root.render(createElement(Panel)));
expect(width).toBe(400);
});
+
+ it("grows past 960 px on a wide window, as far as the chat keeping its minimum width allows", () => {
+ room = 2000;
+ localStorage.setItem("omb-computer-panel-width", "1560");
+ act(() => root.render(createElement(ChatAndPanel)));
+ expect(width).toBe(1560);
+ expect(maxWidth).toBe(2000 - CHAT_MIN_WIDTH);
+ press("ArrowLeft");
+ press("ArrowLeft");
+ expect(width).toBe(2000 - CHAT_MIN_WIDTH);
+ expect(localStorage.getItem("omb-computer-panel-width")).toBe(String(2000 - CHAT_MIN_WIDTH));
+ });
+
+ it("narrows when the window gets smaller and keeps the narrower width after a key press", () => {
+ room = 2000;
+ localStorage.setItem("omb-computer-panel-width", "1500");
+ act(() => root.render(createElement(ChatAndPanel)));
+ expect(width).toBe(1500);
+ resizeWindow(1000);
+ expect(width).toBe(1000 - CHAT_MIN_WIDTH);
+ press("ArrowRight");
+ expect(width).toBe(1000 - CHAT_MIN_WIDTH - 40);
+ resizeWindow(2000);
+ expect(width).toBe(1000 - CHAT_MIN_WIDTH - 40);
+ });
+
+ it("returns to the chosen width when the window grows back", () => {
+ room = 2000;
+ localStorage.setItem("omb-computer-panel-width", "1500");
+ act(() => root.render(createElement(ChatAndPanel)));
+ resizeWindow(1000);
+ expect(width).toBe(1000 - CHAT_MIN_WIDTH);
+ resizeWindow(2000);
+ expect(width).toBe(1500);
+ });
+
+ it("clamps a stored width wider than the window instead of covering the chat", () => {
+ room = 1200;
+ localStorage.setItem("omb-computer-panel-width", "5000");
+ act(() => root.render(createElement(ChatAndPanel)));
+ expect(width).toBe(1200 - CHAT_MIN_WIDTH);
+ });
});
diff --git a/src/components/computer-panel-resize.ts b/src/components/computer-panel-resize.ts
index 98fbb4ac89..68e73d2f7a 100644
--- a/src/components/computer-panel-resize.ts
+++ b/src/components/computer-panel-resize.ts
@@ -1,24 +1,38 @@
-import { useEffect, useRef, useState, type KeyboardEvent, type PointerEvent } from "react";
+import { useLayoutEffect, useRef, useState, type KeyboardEvent, type PointerEvent } from "react";
const PANEL_WIDTH_KEY = "omb-computer-panel-width";
export const PANEL_MIN_WIDTH = 360;
+/** The widest the panel gets when there is no chat beside it to measure. */
export const PANEL_MAX_WIDTH = 960;
+/** The chat beside the panel never gets narrower than this. */
+export const CHAT_MIN_WIDTH = 360;
const PANEL_DEFAULT_WIDTH = 400;
const PANEL_RESIZE_STEP = 40;
function readPanelWidth(): number {
try {
const stored = Number(localStorage.getItem(PANEL_WIDTH_KEY));
- if (Number.isFinite(stored) && stored >= PANEL_MIN_WIDTH && stored <= PANEL_MAX_WIDTH) return stored;
+ if (Number.isFinite(stored) && stored >= PANEL_MIN_WIDTH) return stored;
} catch {
}
return PANEL_DEFAULT_WIDTH;
}
+/** The panel and the chat beside it share the room in the window's row, so the
+ * panel can take everything but the chat's minimum width. */
+function measureMaxWidth(panel: HTMLElement): number {
+ const chat = panel.parentElement?.querySelector<HTMLElement>(":scope > main");
+ if (!chat) return PANEL_MAX_WIDTH;
+ return Math.max(PANEL_MIN_WIDTH, panel.offsetWidth + chat.offsetWidth - CHAT_MIN_WIDTH);
+}
+
/** The computer panel's drag edge: a pointer drag or the arrow keys resize the
- * panel, and the width is remembered across opens. */
+ * panel up to the room the chat leaves, and the width is remembered across opens. */
export function useComputerPanelResize() {
- const [panelWidth, setPanelWidth] = useState(readPanelWidth);
+ const [chosenWidth, setChosenWidth] = useState(readPanelWidth);
+ const [maxWidth, setMaxWidth] = useState(PANEL_MAX_WIDTH);
+ const panelWidth = Math.min(chosenWidth, maxWidth);
+ const clamp = (width: number) => Math.min(maxWidth, Math.max(PANEL_MIN_WIDTH, width));
const resizeFrom = useRef<{ x: number; width: number } | null>(null);
const persistPanelWidth = (width: number) => {
try {
@@ -32,8 +46,7 @@ export function useComputerPanelResize() {
};
const onResizeMove = (event: PointerEvent<HTMLDivElement>) => {
if (!resizeFrom.current) return;
- const next = Math.min(PANEL_MAX_WIDTH, Math.max(PANEL_MIN_WIDTH, resizeFrom.current.width + (resizeFrom.current.x - event.clientX)));
- setPanelWidth(next);
+ setChosenWidth(clamp(resizeFrom.current.width + (resizeFrom.current.x - event.clientX)));
};
const onResizeEnd = (event: PointerEvent<HTMLDivElement>) => {
if (!resizeFrom.current) return;
@@ -42,21 +55,24 @@ export function useComputerPanelResize() {
persistPanelWidth(panelWidth);
};
const onResizeBy = (delta: number) => {
- setPanelWidth((current) => {
- const next = Math.min(PANEL_MAX_WIDTH, Math.max(PANEL_MIN_WIDTH, current + delta));
- persistPanelWidth(next);
- return next;
- });
+ const next = clamp(panelWidth + delta);
+ setChosenWidth(next);
+ persistPanelWidth(next);
};
const separatorRef = useRef<HTMLDivElement>(null);
const [separatorWidth, setSeparatorWidth] = useState<number | null>(null);
- useEffect(() => {
+ useLayoutEffect(() => {
const panel = separatorRef.current?.closest("aside");
if (!panel) return;
- const read = () => setSeparatorWidth(panel.offsetWidth);
+ const read = () => {
+ setSeparatorWidth(panel.offsetWidth);
+ if (panel.offsetWidth > 0) setMaxWidth(measureMaxWidth(panel));
+ };
read();
const observer = new ResizeObserver(read);
observer.observe(panel);
+ const chat = panel.parentElement?.querySelector(":scope > main");
+ if (chat) observer.observe(chat);
return () => observer.disconnect();
}, []);
const onSeparatorKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
@@ -66,5 +82,5 @@ export function useComputerPanelResize() {
event.preventDefault();
onResizeBy(widen);
};
- return { panelWidth, separatorRef, separatorWidth, onSeparatorKeyDown, onResizeStart, onResizeMove, onResizeEnd };
+ return { panelWidth, maxWidth, separatorRef, separatorWidth, onSeparatorKeyDown, onResizeStart, onResizeMove, onResizeEnd };
}
diff --git a/src/components/published-apps/AppPreviewPanel.tsx b/src/components/published-apps/AppPreviewPanel.tsx
index ade0c70b2c..fa9af2f86f 100644
--- a/src/components/published-apps/AppPreviewPanel.tsx
+++ b/src/components/published-apps/AppPreviewPanel.tsx
@@ -6,7 +6,7 @@ import { openExternalLink } from "@/lib/app-links";
import { closeAppPreview, useAppPreview } from "@/lib/app-preview";
import { t } from "@/lib/i18n";
import { useStore } from "@/state/store";
-import { PANEL_MAX_WIDTH, PANEL_MIN_WIDTH, useComputerPanelResize } from "../computer-panel-resize";
+import { PANEL_MIN_WIDTH, useComputerPanelResize } from "../computer-panel-resize";
import { SiteFrame } from "./SiteFrame";
/** The published app a card's Preview or a link in a message opened, beside the conversation where the Computer panel
@@ -26,7 +26,7 @@ export function AppPreviewPanel() {
function PreviewPanel({ url, title, loginOnly }: { url: string; title: string; loginOnly: boolean }) {
const { dragProps } = useCaptionChrome();
- const { panelWidth, separatorRef, separatorWidth, onSeparatorKeyDown, onResizeStart, onResizeMove, onResizeEnd } = useComputerPanelResize();
+ const { panelWidth, maxWidth, separatorRef, separatorWidth, onSeparatorKeyDown, onResizeStart, onResizeMove, onResizeEnd } = useComputerPanelResize();
const [attempt, setAttempt] = useState(0);
const [dragging, setDragging] = useState(false);
return (
@@ -38,7 +38,7 @@ function PreviewPanel({ url, title, loginOnly }: { url: string; title: string; l
aria-orientation="vertical"
aria-label={t("computer.resizeAria")}
aria-valuemin={PANEL_MIN_WIDTH}
- aria-valuemax={PANEL_MAX_WIDTH}
+ aria-valuemax={maxWidth}
aria-valuenow={separatorWidth ?? undefined}
tabIndex={0}
onKeyDown={onSeparatorKeyDown}