Skip to content

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

fix(shortcuts): open bots in sidebar order with Cmd+1..9

Anil Kumarfda5179d592d
diff --git a/src/App.tsx b/src/App.tsx
index 8d2027286b..d4232196f8 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,6 +1,7 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { ChevronLeft, Loader2, Menu } from "lucide-react";
import { CLOUD_LINK_SETTINGS, openBot, StoreProvider, useStore } from "@/state/store";
+import { shortcutBots, shownBotIds } from "@/lib/sidebar-shortcut-order";
import { useShowThreads } from "@/lib/thread-preferences";
import { useWelcomeViewer, WelcomeGate } from "@/components/onboarding/WelcomeGate";
import { MausBotWelcome } from "@/components/MausBotWelcome";
@@ -176,7 +177,7 @@ function Shell({ viewer }: { viewer: WelcomeViewer | null }) {
const mod = e.metaKey || e.ctrlKey;
if (!mod) return;
- const bots = state.bots.filter((b) => !b.hidden);
+ const bots = shortcutBots(state.bots.filter((b) => !b.hidden), shownBotIds(document.querySelector("[data-sidebar-root]")));
if (e.key === "n" && !e.shiftKey) {
e.preventDefault();
dispatch({ type: "newBot" });
diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx
index a63333e14b..36eeb75ecb 100644
--- a/src/components/Sidebar.tsx
+++ b/src/components/Sidebar.tsx
@@ -1244,6 +1244,7 @@ export function Sidebar({ open, onClose, collapseToIcons = false }: {
return (
<aside
ref={sidebarRef}
+ data-sidebar-root=""
tabIndex={-1}
aria-label={t("sidebar.aria")}
data-native-view-overlay
diff --git a/src/lib/sidebar-shortcut-order.test.ts b/src/lib/sidebar-shortcut-order.test.ts
new file mode 100644
index 0000000000..8aa34a5b6b
--- /dev/null
+++ b/src/lib/sidebar-shortcut-order.test.ts
@@ -0,0 +1,55 @@
+// @vitest-environment happy-dom
+import { describe, expect, it } from "vitest";
+
+import { shortcutBots, shownBotIds } from "./sidebar-shortcut-order";
+
+const bot = (id: string) => ({ id });
+
+function sidebar(html: string): HTMLElement {
+ const root = document.createElement("aside");
+ root.innerHTML = html;
+ return root;
+}
+
+describe("shownBotIds", () => {
+ it("reads the bot rows in the order the sidebar renders them", () => {
+ const root = sidebar(`
+ <div data-sidebar-section-id="section:Design"><div data-sidebar-bot-row="d"></div><div data-sidebar-bot-row="b"></div></div>
+ <div data-sidebar-section-id="builtin:bots"><div data-sidebar-bot-row="a"></div></div>
+ `);
+ expect(shownBotIds(root)).toEqual(["d", "b", "a"]);
+ });
+
+ it("leaves out bots of a collapsed section, which renders no rows", () => {
+ const root = sidebar(`
+ <div data-sidebar-section-id="section:Design"></div>
+ <div data-sidebar-section-id="builtin:bots"><div data-sidebar-bot-row="a"></div><div data-sidebar-bot-row="c"></div></div>
+ `);
+ expect(shownBotIds(root)).toEqual(["a", "c"]);
+ });
+
+ it("counts a bot shown twice, as a pinned tile and a row, at its first place", () => {
+ const root = sidebar(`<div data-sidebar-bot-row="p"></div><div data-sidebar-bot-row="a"></div><div data-sidebar-bot-row="p"></div>`);
+ expect(shownBotIds(root)).toEqual(["p", "a"]);
+ });
+
+ it("is empty without a sidebar", () => {
+ expect(shownBotIds(null)).toEqual([]);
+ });
+});
+
+describe("shortcutBots", () => {
+ const bots = [bot("a"), bot("b"), bot("c"), bot("d")];
+
+ it("orders the bots like the sidebar, so ⌘1 opens the first row on screen", () => {
+ expect(shortcutBots(bots, ["d", "b", "a"]).map((b) => b.id)).toEqual(["d", "b", "a"]);
+ });
+
+ it("drops ids that are not among the bots, such as a hidden bot", () => {
+ expect(shortcutBots([bot("a")], ["x", "a"]).map((b) => b.id)).toEqual(["a"]);
+ });
+
+ it("keeps the bots' own order when no sidebar is on screen", () => {
+ expect(shortcutBots(bots, []).map((b) => b.id)).toEqual(["a", "b", "c", "d"]);
+ });
+});
diff --git a/src/lib/sidebar-shortcut-order.ts b/src/lib/sidebar-shortcut-order.ts
new file mode 100644
index 0000000000..fd1a2899a3
--- /dev/null
+++ b/src/lib/sidebar-shortcut-order.ts
@@ -0,0 +1,17 @@
+/**
+ * The bots that ⌘1…⌘9 and ⌘⇧[ / ⌘⇧] step through, in the order the sidebar shows them. Sections, a custom bot
+ * order, collapsed sections, pinned tiles and a search all change that order, so it is read from the rendered rows
+ * instead of being rebuilt from `state.bots`.
+ */
+export function shownBotIds(root: ParentNode | null | undefined): string[] {
+ if (!root) return [];
+ const ids = [...root.querySelectorAll<HTMLElement>("[data-sidebar-bot-row]")].map((row) => row.dataset.sidebarBotRow ?? "");
+ return [...new Set(ids.filter(Boolean))];
+}
+
+/** `bots` in the sidebar's order; with no sidebar on screen, `bots` as they are. */
+export function shortcutBots<T extends { id: string }>(bots: readonly T[], shownIds: readonly string[]): T[] {
+ if (shownIds.length === 0) return [...bots];
+ const byId = new Map(bots.map((bot) => [bot.id, bot]));
+ return shownIds.flatMap((id) => byId.get(id) ?? []);
+}