Skip to content

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

Sidebar: the hover list of a bot's conversations stays open while the pointer crosses onto it

luishron123d7bfd74e3a77
diff --git a/src/components/SidebarBotThreadsPeek.dom.test.ts b/src/components/SidebarBotThreadsPeek.dom.test.ts
new file mode 100644
index 0000000000..6b5864bf61
--- /dev/null
+++ b/src/components/SidebarBotThreadsPeek.dom.test.ts
@@ -0,0 +1,93 @@
+// @vitest-environment happy-dom
+import { act, createElement } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import type { Action, Task } from "@/state/store";
+import { PEEK_CLOSE_MS, SidebarBotThreadsPeek } from "./SidebarBotThreadsPeek";
+
+(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+const task = (threadId: string, extra: Partial<Task> = {}): Task => ({ threadId, title: threadId, createdAt: 1, ...extra });
+
+let row: HTMLElement;
+let scroller: HTMLElement;
+let chat: HTMLElement;
+let root: Root;
+const dispatch = vi.fn<(action: Action) => void>();
+
+const mount = async (tasks: Task[]) => {
+ scroller = document.createElement("div");
+ chat = document.createElement("div");
+ document.body.append(scroller, chat);
+ root = createRoot(scroller);
+ await act(async () => {
+ root.render(createElement("div", { "data-row": "" },
+ createElement(SidebarBotThreadsPeek, { bot: { id: "atlas", name: "Atlas", threadId: "main", mainThreadId: "main", tasks: [task("main"), ...tasks] }, dispatch })));
+ });
+ row = scroller.querySelector<HTMLElement>("[data-row]")!;
+};
+const chevron = () => row.querySelector<HTMLButtonElement>("[data-bot-threads-peek]")!;
+const sheet = () => document.querySelector<HTMLElement>("[data-bot-threads-sheet]:not([inert])");
+const open = async () => { await act(async () => { chevron().click(); }); };
+const pointer = (type: string, target: Element, relatedTarget: Element) =>
+ target.dispatchEvent(new PointerEvent(type, { pointerType: "mouse", relatedTarget, bubbles: !/enter|leave/.test(type) }));
+// The order a browser reports one crossing in: out and leave on the old element, then over and enter on the new one.
+const cross = async (from: Element, to: Element) => {
+ await act(async () => { for (const [type, target, related] of [["pointerout", from, to], ["pointerleave", from, to], ["pointerover", to, from], ["pointerenter", to, from]] as const) pointer(type, target, related); });
+};
+const wait = async (ms: number) => { await act(async () => { vi.advanceTimersByTime(ms); }); };
+
+beforeEach(() => { vi.useFakeTimers(); dispatch.mockClear(); });
+afterEach(async () => {
+ await act(async () => { root.unmount(); });
+ document.body.innerHTML = "";
+ vi.useRealTimers();
+});
+
+describe("the hover sheet of a bot's other conversations", () => {
+ it("stays open while the pointer goes from the row onto it", async () => {
+ await mount([task("a")]);
+ await open();
+ await cross(row, sheet()!);
+ await wait(PEEK_CLOSE_MS * 5);
+ expect(chevron().getAttribute("aria-expanded")).toBe("true");
+ expect(sheet()).not.toBeNull();
+ });
+
+ it("stays open when the pointer crosses something else on the way, and closes shortly after leaving both", async () => {
+ await mount([task("a")]);
+ await open();
+ await cross(row, chat);
+ await wait(PEEK_CLOSE_MS / 2);
+ await cross(chat, sheet()!);
+ await wait(PEEK_CLOSE_MS * 5);
+ expect(chevron().getAttribute("aria-expanded")).toBe("true");
+
+ await cross(sheet()!, row);
+ await wait(PEEK_CLOSE_MS * 5);
+ expect(chevron().getAttribute("aria-expanded")).toBe("true");
+
+ await cross(row, chat);
+ await wait(PEEK_CLOSE_MS - 1);
+ expect(chevron().getAttribute("aria-expanded")).toBe("true");
+ await wait(1);
+ expect(chevron().getAttribute("aria-expanded")).toBe("false");
+ });
+
+ it("drops the disclosure's tooltip while the sheet is open", async () => {
+ await mount([task("a")]);
+ expect(chevron().title).toBe("Expand Atlas threads");
+ await open();
+ expect(chevron().hasAttribute("title")).toBe(false);
+ expect(chevron().getAttribute("aria-label")).toBe("Expand Atlas threads");
+ });
+
+ it("closes when the list holding its row scrolls, not when anything else does", async () => {
+ await mount([task("a")]);
+ await open();
+ await act(async () => { chat.dispatchEvent(new Event("scroll")); });
+ expect(chevron().getAttribute("aria-expanded")).toBe("true");
+ await act(async () => { scroller.dispatchEvent(new Event("scroll")); });
+ expect(chevron().getAttribute("aria-expanded")).toBe("false");
+ });
+});
diff --git a/src/components/SidebarBotThreadsPeek.tsx b/src/components/SidebarBotThreadsPeek.tsx
index 8accc4dc00..6ed92ee764 100644
--- a/src/components/SidebarBotThreadsPeek.tsx
+++ b/src/components/SidebarBotThreadsPeek.tsx
@@ -67,12 +67,14 @@ export function SidebarBotThreadsPeek({ bot, dispatch }: { bot: PeekBot; dispatc
useEffect(() => cancel, []);
useEffect(() => { if (!has) close(); }, [has]);
+ // React reports entering the sheet while the pointer is still leaving the row, so each side checks where the pointer went.
+ const within = (target: EventTarget | null) => target instanceof Node && Boolean(sheet.current?.contains(target) || anchor.current?.parentElement?.contains(target));
+ const leave = (event: Pick<PointerEvent, "pointerType" | "relatedTarget">) => { if (event.pointerType === "mouse" && !within(event.relatedTarget)) later(() => setPlace(null), PEEK_CLOSE_MS); };
useEffect(() => {
const row = anchor.current?.parentElement;
if (!row || !has) return;
// offsetParent is null while the disclosure is hidden, which it is on a phone-width window.
const enter = (event: PointerEvent) => { if (event.pointerType === "mouse" && event.buttons === 0 && anchor.current?.offsetParent) later(show, PEEK_OPEN_MS); };
- const leave = (event: PointerEvent) => { if (event.pointerType === "mouse") later(() => setPlace(null), PEEK_CLOSE_MS); };
row.addEventListener("pointerenter", enter);
row.addEventListener("pointerleave", leave);
return () => { row.removeEventListener("pointerenter", enter); row.removeEventListener("pointerleave", leave); };
@@ -80,7 +82,7 @@ export function SidebarBotThreadsPeek({ bot, dispatch }: { bot: PeekBot; dispatc
useEffect(() => {
if (!open) return;
if (focusSheet.current) { focusSheet.current = false; sheet.current?.querySelector<HTMLElement>("[role=menuitem]")?.focus(); }
- const away = (event: Event) => { if (!(event.target instanceof Node) || !sheet.current?.contains(event.target)) close(); };
+ const away = (event: Event) => { if (!(event.target instanceof Node) || event.target.contains(anchor.current)) close(); };
window.addEventListener("resize", close);
window.addEventListener("scroll", away, true);
return () => { window.removeEventListener("resize", close); window.removeEventListener("scroll", away, true); };
@@ -117,13 +119,13 @@ export function SidebarBotThreadsPeek({ bot, dispatch }: { bot: PeekBot; dispatc
return (
<>
<button ref={anchor} type="button" data-bot-threads-peek={bot.id} aria-haspopup="menu" aria-expanded={open}
- aria-label={t("task.expandNamed", { name: bot.name })} title={t("task.expandNamed", { name: bot.name })}
+ aria-label={t("task.expandNamed", { name: bot.name })} title={open ? undefined : t("task.expandNamed", { name: bot.name })}
onClick={(event) => { if (open) { close(); return; } focusSheet.current = event.detail === 0; show(); }} className={slot}>
<ChevronRight size={14} className="rtl:-scale-x-100" />
</button>
{motion.shown && shown && createPortal(
<div ref={sheet} role="menu" aria-label={bot.name} data-bot-threads-sheet={bot.id} style={{ left: shown.left, top: shown.top, width: SHEET_WIDTH }}
- onPointerEnter={cancel} onPointerLeave={(event) => { if (event.pointerType === "mouse") later(() => setPlace(null), PEEK_CLOSE_MS); }} onKeyDown={onKeyDown}
+ onPointerEnter={cancel} onPointerLeave={leave} onKeyDown={onKeyDown}
className={cn(MENU_SHEET, "fixed z-[60] gap-0", motion.className)} {...motion.exitProps}>
<button type="button" role="menuitem" className={MENU_ROW} onClick={() => pick({ type: "newTask", botId: bot.id })}>
<span className={MENU_GUTTER}><Plus size={14} className={MENU_ICON} /></span>