Skip to content

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

fix(shortcuts): step from the list's ends when the open bot is not shown

Anil Kumarf74c0c49a2fd
diff --git a/src/App.tsx b/src/App.tsx
index d4232196f8..d1fa879ea0 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -1,7 +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 { nextShortcutBot, 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";
@@ -188,8 +188,7 @@ function Shell({ viewer }: { viewer: WelcomeViewer | null }) {
openBot(dispatch, target, showThreads);
}
} else if (e.shiftKey && (e.key === "[" || e.key === "]")) {
- const idx = bots.findIndex((b) => b.id === state.selectedId);
- const next = bots[(idx + (e.key === "]" ? 1 : -1) + bots.length) % bots.length];
+ const next = nextShortcutBot(bots, state.selectedId, e.key === "]" ? 1 : -1);
if (next) {
e.preventDefault();
openBot(dispatch, next, showThreads);
diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx
index 36eeb75ecb..369c157abf 100644
--- a/src/components/Sidebar.tsx
+++ b/src/components/Sidebar.tsx
@@ -1244,7 +1244,7 @@ export function Sidebar({ open, onClose, collapseToIcons = false }: {
return (
<aside
ref={sidebarRef}
- data-sidebar-root=""
+ data-sidebar-root={!phone || open ? "" : undefined}
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
index 8aa34a5b6b..07fa7aca2f 100644
--- a/src/lib/sidebar-shortcut-order.test.ts
+++ b/src/lib/sidebar-shortcut-order.test.ts
@@ -1,7 +1,7 @@
// @vitest-environment happy-dom
import { describe, expect, it } from "vitest";
-import { shortcutBots, shownBotIds } from "./sidebar-shortcut-order";
+import { nextShortcutBot, shortcutBots, shownBotIds } from "./sidebar-shortcut-order";
const bot = (id: string) => ({ id });
@@ -53,3 +53,23 @@ describe("shortcutBots", () => {
expect(shortcutBots(bots, []).map((b) => b.id)).toEqual(["a", "b", "c", "d"]);
});
});
+
+describe("nextShortcutBot", () => {
+ const bots = [bot("a"), bot("b"), bot("c")];
+
+ it("steps forward and back through the list, wrapping at both ends", () => {
+ expect(nextShortcutBot(bots, "b", 1)?.id).toBe("c");
+ expect(nextShortcutBot(bots, "c", 1)?.id).toBe("a");
+ expect(nextShortcutBot(bots, "a", -1)?.id).toBe("c");
+ });
+
+ it("starts from either end when the selected bot is not shown, as in a collapsed section", () => {
+ expect(nextShortcutBot(bots, "hidden", 1)?.id).toBe("a");
+ expect(nextShortcutBot(bots, "hidden", -1)?.id).toBe("c");
+ expect(nextShortcutBot(bots, null, -1)?.id).toBe("c");
+ });
+
+ it("opens nothing without bots", () => {
+ expect(nextShortcutBot([], "a", 1)).toBeUndefined();
+ });
+});
diff --git a/src/lib/sidebar-shortcut-order.ts b/src/lib/sidebar-shortcut-order.ts
index fd1a2899a3..ceda895bba 100644
--- a/src/lib/sidebar-shortcut-order.ts
+++ b/src/lib/sidebar-shortcut-order.ts
@@ -15,3 +15,14 @@ export function shortcutBots<T extends { id: string }>(bots: readonly T[], shown
const byId = new Map(bots.map((bot) => [bot.id, bot]));
return shownIds.flatMap((id) => byId.get(id) ?? []);
}
+
+/**
+ * The bot ⌘⇧] (`step` 1) or ⌘⇧[ (`step` -1) opens. A selected bot that is not in `bots`, such as one in a
+ * collapsed section, counts as standing just outside the list: ] opens the first bot and [ the last.
+ */
+export function nextShortcutBot<T extends { id: string }>(bots: readonly T[], selectedId: string | null | undefined, step: 1 | -1): T | undefined {
+ if (bots.length === 0) return undefined;
+ const index = bots.findIndex((bot) => bot.id === selectedId);
+ if (index === -1) return step > 0 ? bots[0] : bots.at(-1);
+ return bots[(index + step + bots.length) % bots.length];
+}