Skip to content

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

Keep a file preview with its conversation and bring it back on return

AdityaUmale7ac766486659
diff --git a/src/components/FilePreviewPanel.dom.test.ts b/src/components/FilePreviewPanel.dom.test.ts
index 51dd9e06f0..86c21ab80b 100644
--- a/src/components/FilePreviewPanel.dom.test.ts
+++ b/src/components/FilePreviewPanel.dom.test.ts
@@ -99,6 +99,50 @@ describe("a file opened from the chat on a wide window", () => {
expect(pane()).toBeNull();
});
+ it("comes back when the person returns to that conversation, without taking the focus", async () => {
+ await render();
+ await openFile();
+ await render({ threadId: "t2" });
+ const box = host.querySelector<HTMLTextAreaElement>("textarea[aria-label='Message']")!;
+ box.focus();
+ await render({ threadId: "t1" });
+ expect(pane()?.getAttribute("data-file-preview")).toBe(PLAN.path);
+ await vi.waitFor(() => expect(pane()?.textContent).toContain("Bring a red flashlight."));
+ expect(document.activeElement).toBe(box);
+ });
+
+ it("stays closed in a conversation where the person closed it", async () => {
+ await render();
+ await openFile();
+ await act(async () => pane()!.querySelector<HTMLButtonElement>("button[aria-label='Close preview']")!.click());
+ await render({ threadId: "t2" });
+ await render({ threadId: "t1" });
+ expect(pane()).toBeNull();
+ });
+
+ it("stays away on a return while another panel holds that side, and comes back on a later one", async () => {
+ await render();
+ await openFile();
+ await render({ threadId: "t2" });
+ fixture.state = { ...fixture.state, computerOpen: true };
+ await render({ threadId: "t2" });
+ await render({ threadId: "t1" });
+ fixture.state = { ...fixture.state, computerOpen: false };
+ await render({ threadId: "t1" });
+ expect(pane()).toBeNull();
+ await render({ threadId: "t2" });
+ await render({ threadId: "t1" });
+ expect(pane()?.getAttribute("data-file-preview")).toBe(PLAN.path);
+ });
+
+ it("stays closed in a conversation where it was never open", async () => {
+ await render();
+ await openFile();
+ await render({ threadId: "t2" });
+ await render({ threadId: "t3" });
+ expect(pane()).toBeNull();
+ });
+
it("shows a linked file outside the chat in its folder by its real path, asking for it by the link as written", async () => {
const fetch = vi.fn(async (_url: string, _init?: RequestInit) => new Response(JSON.stringify({ error: "the linked file is outside this conversation's workspace", code: "outside_workspace" }), { status: 403, headers: { "content-type": "application/json" } }));
const revealInFolder = vi.fn(async () => "shown");
diff --git a/src/components/FilePreviewPanel.tsx b/src/components/FilePreviewPanel.tsx
index d89b8b45be..20d29594a8 100644
--- a/src/components/FilePreviewPanel.tsx
+++ b/src/components/FilePreviewPanel.tsx
@@ -4,7 +4,7 @@ import { X } from "lucide-react";
import { useCaptionChrome } from "@/components/DesktopCapabilities";
import { useAppPreview } from "@/lib/app-preview";
import { cn } from "@/lib/cn";
-import { closeFilePreview, useFilePreview, type FilePreview } from "@/lib/file-preview";
+import { closeFilePreview, forgetFilePreviews, moveFilePreview, useFilePreview, type FilePreview } from "@/lib/file-preview";
import { t } from "@/lib/i18n";
import { useRoomPanel } from "@/lib/room-panel";
import { useStore } from "@/state/store";
@@ -12,7 +12,8 @@ import { PANEL_MIN_WIDTH, useComputerPanelResize } from "./computer-panel-resize
import { FilePreviewContent } from "./HtmlFilePreview";
/** A file from a message, beside the conversation where the Computer panel sits, so the transcript and the message box
- * stay in reach while it is open. It gives way when another panel takes that side, and closes when the chat changes. */
+ * stay in reach while it is open. It gives way when another panel takes that side, and when the chat changes it goes with
+ * its conversation, back on screen when the person returns there. */
export function FilePreviewPanel({ threadId }: { threadId: string | undefined }) {
const preview = useFilePreview();
const appPreview = useAppPreview();
@@ -24,7 +25,13 @@ export function FilePreviewPanel({ threadId }: { threadId: string | undefined })
if (otherPanel && !hadOtherPanel.current) closeFilePreview();
hadOtherPanel.current = otherPanel;
}, [otherPanel]);
- useEffect(() => closeFilePreview, [threadId]);
+ const shownThread = useRef(threadId);
+ useEffect(() => {
+ if (shownThread.current === threadId) return;
+ moveFilePreview(shownThread.current, threadId, !hadOtherPanel.current);
+ shownThread.current = threadId;
+ }, [threadId]);
+ useEffect(() => forgetFilePreviews, []);
if (!preview || otherPanel || state.activeView !== "chat") return null;
return <FilePane key={`${preview.message.threadId}:${preview.message.messageId}:${preview.path}`} preview={preview} />;
}
@@ -34,7 +41,8 @@ function FilePane({ preview }: { preview: FilePreview }) {
const { panelWidth, maxWidth, separatorRef, separatorWidth, onSeparatorKeyDown, onResizeStart, onResizeMove, onResizeEnd } = useComputerPanelResize();
const closeButton = useRef<HTMLButtonElement>(null);
const [dragging, setDragging] = useState(false);
- useEffect(() => closeButton.current?.focus({ preventScroll: true }), []);
+ // Only a file the person just opened takes the focus; one coming back with its conversation leaves it where it is.
+ useEffect(() => { if (preview.opener) closeButton.current?.focus({ preventScroll: true }); }, [preview.opener]);
// Taking focus back must not scroll the transcript away from where the person left it.
const dismiss = () => {
closeFilePreview();
diff --git a/src/lib/file-preview.ts b/src/lib/file-preview.ts
index 0e854ae01c..69c5f8a955 100644
--- a/src/lib/file-preview.ts
+++ b/src/lib/file-preview.ts
@@ -31,6 +31,27 @@ function set(next: FilePreview | null): void {
export const openFilePreview = (preview: FilePreview): void => set({ ...preview });
export const closeFilePreview = (): void => set(null);
+/** The file each conversation the chat left had open beside it, shown again when the person comes back to it. */
+const leftOpen = new Map<string, FilePreview>();
+
+/** The chat moved from one conversation to another: keeps the open file for the one it left, and shows the one the
+ * other had open when the person left it, if any and if `show`; otherwise that one stays kept for a later return. */
+export function moveFilePreview(from: string | undefined, to: string | undefined, show: boolean): void {
+ if (current && from !== undefined) leftOpen.set(from, { ...current, opener: null });
+ let kept: FilePreview | null = null;
+ if (show && to !== undefined) {
+ kept = leftOpen.get(to) ?? null;
+ leftOpen.delete(to);
+ }
+ set(kept);
+}
+
+/** Closes the pane and forgets the files every conversation had open. */
+export function forgetFilePreviews(): void {
+ leftOpen.clear();
+ set(null);
+}
+
export function useFilePreview(): FilePreview | null {
return useSyncExternalStore((listener) => { listeners.add(listener); return () => { listeners.delete(listener); }; }, () => current, () => null);
}