Skip to content

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

Sidebar: typing letters in the hover list jumps to the conversation that starts with them

luishron123a8bc9865bec7
diff --git a/src/components/SidebarBotThreadsPeek.dom.test.ts b/src/components/SidebarBotThreadsPeek.dom.test.ts
index 8a8d2f358e..24399e37e8 100644
--- a/src/components/SidebarBotThreadsPeek.dom.test.ts
+++ b/src/components/SidebarBotThreadsPeek.dom.test.ts
@@ -2,6 +2,7 @@
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { TYPEAHEAD_RESET_MS } from "@/lib/menu-keys";
import type { Action, Task } from "@/state/store";
import { PEEK_CLOSE_MS, SidebarBotThreadsPeek } from "./SidebarBotThreadsPeek";
@@ -138,4 +139,29 @@ describe("the hover sheet of a bot's other conversations", () => {
await key("ArrowDown");
expect(document.activeElement).toBe(thread("b"));
});
+
+ it("jumps to the conversation whose title starts with the letters typed", async () => {
+ await mount([task("a", { title: "Pricing review" }), task("b", { title: "Supplier follow-up" }), task("c", { title: "Price list" })]);
+ await open();
+ const type = async (text: string) => {
+ for (const key of text) {
+ const event = new KeyboardEvent("keydown", { key, bubbles: true });
+ Object.defineProperty(event, "timeStamp", { value: Date.now() });
+ await act(async () => { document.activeElement!.dispatchEvent(event); });
+ }
+ };
+ const thread = (id: string) => sheet()!.querySelector<HTMLElement>(`[data-peek-thread="${id}"]`)!;
+ sheet()!.querySelector<HTMLElement>("[role=menuitem]")!.focus();
+ await type("s");
+ expect(document.activeElement).toBe(thread("b"));
+ await wait(TYPEAHEAD_RESET_MS);
+ await type("pric");
+ expect(document.activeElement).toBe(thread("c"));
+ await type("i");
+ expect(document.activeElement).toBe(thread("a"));
+ action("a", "archive").focus();
+ await wait(TYPEAHEAD_RESET_MS);
+ await type("n");
+ expect(document.activeElement?.textContent).toBe("New thread");
+ });
});
diff --git a/src/components/SidebarBotThreadsPeek.tsx b/src/components/SidebarBotThreadsPeek.tsx
index b633f1386e..52944a4c87 100644
--- a/src/components/SidebarBotThreadsPeek.tsx
+++ b/src/components/SidebarBotThreadsPeek.tsx
@@ -3,6 +3,7 @@ import { useEffect, useId, useMemo, useRef, useState, type Dispatch, type Keyboa
import { createPortal } from "react-dom";
import { cn } from "@/lib/cn";
import { t } from "@/lib/i18n";
+import { isTypeaheadKey, typeaheadMatch, typeaheadText, type Typeahead } from "@/lib/menu-keys";
import { localizedTaskTitle } from "@/lib/task-title";
import { usePopoverDismiss } from "@/hooks/use-popover-dismiss";
import type { Action, Bot, Task } from "@/state/store";
@@ -58,6 +59,7 @@ export function SidebarBotThreadsPeek({ bot, dispatch }: { bot: PeekBot; dispatc
const timer = useRef<number | undefined>(undefined);
const focusSheet = useRef(false);
const focusAnchor = useRef(false);
+ const typed = useRef<Typeahead | null>(null);
const [deleting, setDeleting] = useState<Task | null>(null);
const labels = useId();
const has = threads.length > 0;
@@ -142,6 +144,13 @@ export function SidebarBotThreadsPeek({ bot, dispatch }: { bot: PeekBot; dispatc
} else if (event.key === "Tab") {
anchor.current?.focus();
close();
+ } else if (isTypeaheadKey(event)) {
+ typed.current = typeaheadText(typed.current, event.key, event.timeStamp);
+ const match = typeaheadMatch(items.map((item) => item.textContent ?? ""), at, typed.current.text);
+ if (match !== null) {
+ event.preventDefault();
+ items[match]?.focus();
+ }
}
};
const now = Date.now();