Skip to content

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

Thread panel: let it be dragged wider beside the open Inbox

AdityaUmaled04ff348ba06
diff --git a/src/components/ForkPanel.tsx b/src/components/ForkPanel.tsx
index e119e3b06d..d6fb66ff8c 100644
--- a/src/components/ForkPanel.tsx
+++ b/src/components/ForkPanel.tsx
@@ -14,9 +14,11 @@ import { useRoomFeedHere } from "./room-feed/use-room-feed";
import { useThreadTurnStart } from "./room-feed/thread-turn-start";
import { useRoomWorkStatuses } from "./room-feed/use-room-work";
import { FEED_MIN_WIDTH } from "./room-feed/feed-fit";
-import { PANEL_MIN_WIDTH, useComputerPanelResize } from "./computer-panel-resize";
+import { PANEL_MIN_WIDTH, readPanelWidth, useComputerPanelResize } from "./computer-panel-resize";
-const BESIDE_INBOX_MAX = 400;
+const PANEL_WIDTH_KEY = "omb-thread-panel-width";
+const PANEL_DEFAULT_WIDTH = 440;
+const BESIDE_INBOX_DEFAULT_MAX = 400;
/** Any thread beside the chat: a fork, and where the server has the room feed, the room thread a feed row or the Inbox
* opened. `full`: that room thread takes the feed's place, because the two don't fit side by side; its back button
@@ -27,9 +29,15 @@ export function ForkPanel({ full = false }: { full?: boolean }) {
const asideRef = useRef<HTMLElement>(null);
const focusedLoad = useRef<string | undefined>(undefined);
const beside = useRoomFeedHere();
- const resize = useComputerPanelResize({ key: "omb-thread-panel-width", defaultWidth: 440, chatMin: panel?.kind === "group" ? FEED_MIN_WIDTH : undefined });
- // Beside the Inbox the chat keeps the 440 it has beside any other side panel.
- const width = useInboxPanel().open ? Math.min(resize.panelWidth, BESIDE_INBOX_MAX) : resize.panelWidth;
+ const inboxOpen = useInboxPanel().open;
+ // Beside the Inbox the thread starts narrower, so the chat keeps the 440 it has beside any other side panel, and a
+ // width dragged there is kept apart from the one without the Inbox.
+ const resize = useComputerPanelResize({
+ key: inboxOpen ? `${PANEL_WIDTH_KEY}-beside-inbox` : PANEL_WIDTH_KEY,
+ defaultWidth: inboxOpen ? Math.min(readPanelWidth(PANEL_WIDTH_KEY, PANEL_DEFAULT_WIDTH), BESIDE_INBOX_DEFAULT_MAX) : PANEL_DEFAULT_WIDTH,
+ chatMin: panel?.kind === "group" ? FEED_MIN_WIDTH : undefined,
+ });
+ const width = resize.panelWidth;
useLayoutEffect(() => {
const aside = asideRef.current;
if (!panel?.focusComposer || !aside) return;
diff --git a/src/components/ForkPanel.width.test.ts b/src/components/ForkPanel.width.test.ts
new file mode 100644
index 0000000000..b7e64e3269
--- /dev/null
+++ b/src/components/ForkPanel.width.test.ts
@@ -0,0 +1,70 @@
+// @vitest-environment happy-dom
+import { act, createElement } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+
+const store = vi.hoisted(() => ({ state: {} as Record<string, unknown>, dispatch: vi.fn() }));
+const inbox = vi.hoisted(() => ({ open: false }));
+vi.mock("@/state/store", () => ({ useStore: () => store, currentTaskBot: (bot: object) => bot }));
+vi.mock("@/lib/default-bot", () => ({ useViewerEmail: () => null }));
+vi.mock("@/state/inbox", async (importOriginal) => ({ ...await importOriginal<object>(), useInboxPanel: () => ({ open: inbox.open }) }));
+vi.mock("./room-feed/use-room-feed", async (importOriginal) => ({ ...await importOriginal<object>(), useRoomFeedHere: () => true }));
+vi.mock("./ChatView", () => ({ ChatView: () => null }));
+vi.mock("./GroupView", () => ({ GroupView: () => null }));
+
+import { ForkPanel } from "./ForkPanel";
+
+(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+let container: HTMLDivElement;
+let root: Root;
+
+const render = () => act(() => root.render(createElement(ForkPanel)));
+const panelWidth = () => container.querySelector("aside")!.style.width;
+const widen = () => act(() => {
+ container.querySelector("[role=separator]")!.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowLeft", bubbles: true }));
+});
+
+beforeEach(() => {
+ localStorage.clear();
+ inbox.open = false;
+ store.state = {
+ forkPanel: { kind: "bot", ownerId: "bot", threadId: "thread", messages: [], loadId: "load" },
+ bots: [{ id: "bot", name: "Bot", tasks: [{ threadId: "thread", title: "Thread" }] }],
+ groups: [],
+ };
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+});
+afterEach(() => {
+ act(() => root.unmount());
+ container.remove();
+ localStorage.clear();
+});
+
+it("widens beside the Inbox when the edge is dragged, and keeps that width for the next time the Inbox is open", () => {
+ inbox.open = true;
+ render();
+ expect(panelWidth()).toBe("400px");
+ widen();
+ widen();
+ expect(panelWidth()).toBe("480px");
+ inbox.open = false;
+ render();
+ expect(panelWidth()).toBe("440px");
+ inbox.open = true;
+ render();
+ expect(panelWidth()).toBe("480px");
+});
+
+it("starts beside the Inbox at the narrower width already chosen without it", () => {
+ localStorage.setItem("omb-thread-panel-width", "360");
+ inbox.open = true;
+ render();
+ expect(panelWidth()).toBe("360px");
+ localStorage.setItem("omb-thread-panel-width", "700");
+ act(() => root.unmount());
+ root = createRoot(container);
+ render();
+ expect(panelWidth()).toBe("400px");
+});
diff --git a/src/components/computer-panel-resize.test.ts b/src/components/computer-panel-resize.test.ts
index 5c8a8845f3..fe23d13829 100644
--- a/src/components/computer-panel-resize.test.ts
+++ b/src/components/computer-panel-resize.test.ts
@@ -15,8 +15,8 @@ let room = 0;
let observers: { notify: () => void; target: HTMLElement; last: number }[] = [];
let handlers: ReturnType<typeof useComputerPanelResize>;
-function Panel() {
- handlers = useComputerPanelResize();
+function Panel({ widthKey }: { widthKey?: string }) {
+ handlers = useComputerPanelResize({ key: widthKey });
const { panelWidth, maxWidth: max, separatorRef, onSeparatorKeyDown } = handlers;
width = panelWidth;
maxWidth = max;
@@ -155,6 +155,21 @@ describe("the computer panel's resize edge", () => {
expect(maxWidth).toBe(3000 - CHAT_MIN_WIDTH);
});
+ it("brings back the width kept under a new key and keeps a later change under that key only", () => {
+ localStorage.setItem("first", "500");
+ localStorage.setItem("second", "800");
+ act(() => root.render(createElement(Panel, { widthKey: "first" })));
+ expect(width).toBe(500);
+ act(() => root.render(createElement(Panel, { widthKey: "second" })));
+ expect(width).toBe(800);
+ press("ArrowLeft");
+ expect(width).toBe(840);
+ expect(localStorage.getItem("second")).toBe("840");
+ expect(localStorage.getItem("first")).toBe("500");
+ act(() => root.render(createElement(Panel, { widthKey: "first" })));
+ expect(width).toBe(500);
+ });
+
it("clamps a stored width wider than the window instead of covering the chat", () => {
room = 1200;
localStorage.setItem("omb-computer-panel-width", "5000");
diff --git a/src/components/computer-panel-resize.ts b/src/components/computer-panel-resize.ts
index 623f75a350..7ced551d35 100644
--- a/src/components/computer-panel-resize.ts
+++ b/src/components/computer-panel-resize.ts
@@ -9,7 +9,7 @@ export const CHAT_MIN_WIDTH = 360;
const PANEL_DEFAULT_WIDTH = 400;
const PANEL_RESIZE_STEP = 40;
-function readPanelWidth(key: string, fallback: number): number {
+export function readPanelWidth(key: string, fallback: number): number {
try {
const stored = Number(localStorage.getItem(key));
if (Number.isFinite(stored) && stored >= PANEL_MIN_WIDTH) return stored;
@@ -27,11 +27,15 @@ function measureMaxWidth(panel: HTMLElement, chat: HTMLElement | null, chatMin =
/** The computer panel's drag edge: a pointer drag or the arrow keys resize the
* panel up to the room the chat leaves, and the width is remembered across opens
- * (under `key`, for a panel that keeps its own). `chatMin` is what the chat beside it keeps at least. */
+ * (under `key`, for a panel that keeps its own; a new `key` brings back the width kept under it). `chatMin` is what the
+ * chat beside it keeps at least. */
export function useComputerPanelResize({ key = PANEL_WIDTH_KEY, defaultWidth = PANEL_DEFAULT_WIDTH, chatMin = CHAT_MIN_WIDTH }: { key?: string; defaultWidth?: number; chatMin?: number } = {}) {
const chatMinRef = useRef(chatMin);
chatMinRef.current = chatMin;
- const [chosenWidth, setChosenWidth] = useState(() => readPanelWidth(key, defaultWidth));
+ const [chosen, setChosen] = useState(() => ({ key, width: readPanelWidth(key, defaultWidth) }));
+ if (chosen.key !== key) setChosen({ key, width: readPanelWidth(key, defaultWidth) });
+ const chosenWidth = chosen.width;
+ const setChosenWidth = (width: number) => setChosen({ key, width });
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));