Skip to content

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

fix(sidebar): working or waiting bots move to the top of their list (#4377)

Aditya Umale4fe6439cf875
diff --git a/src/components/Sidebar.departments.test.ts b/src/components/Sidebar.departments.test.ts
index 87e4f872d5..4e7c06ab18 100644
--- a/src/components/Sidebar.departments.test.ts
+++ b/src/components/Sidebar.departments.test.ts
@@ -1,6 +1,6 @@
import { Children, isValidElement, type EffectCallback, type ReactElement, type ReactNode } from "react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import type { Action, AppState, Bot, Group } from "@/state/store";
+import type { Action, AppState, Bot, Group, Task } from "@/state/store";
const fixture = vi.hoisted(() => ({
values: [] as unknown[], index: 0, advanced: false, phone: false, admin: null as boolean | null,
@@ -293,6 +293,41 @@ describe("dragging a bot", () => {
});
});
+describe("bots that are working or waiting on the person", () => {
+ const busy = (id: string, extra: Partial<Bot>) => {
+ fixture.state = { ...fixture.state, bots: fixture.state.bots.map((item) => (item.id === id ? { ...item, ...extra } : item)) };
+ };
+
+ it("move to the top of their own list, in the person's order, and go back when done", () => {
+ busy("cleo", { busy: true });
+ busy("bea", { activity: "waiting-on-you" });
+ busy("echo", { busy: true });
+ expect(listed()).toEqual(["bea", "cleo", "atlas", "echo", "dodo"]);
+ busy("cleo", { busy: false });
+ busy("bea", { activity: "idle" });
+ busy("echo", { busy: false });
+ expect(listed()).toEqual(["atlas", "bea", "cleo", "dodo", "echo"]);
+ });
+
+ it("count a working thread, but not a wait on a teammate", () => {
+ busy("cleo", { tasks: [{ threadId: "c1", title: "Run", createdAt: 1, busy: true } as Task] });
+ busy("bea", { waitingForTeammates: true });
+ expect(listed()).toEqual(["cleo", "atlas", "bea", "dodo", "echo"]);
+ });
+
+ it("never store the shown order: Alt+Up/Down and drops use the person's own order", () => {
+ busy("cleo", { busy: true });
+ row("atlas").botDrag!.step("atlas", 1);
+ expect(actions()).toEqual([{ type: "botsOrdered", botIds: ["bea", "atlas", "cleo"] }]);
+ fixture.dispatch.mockClear();
+ // A drop on the list itself finds its row in the shown order (cleo first) and places it by the person's own order.
+ drag("atlas");
+ laidOut("cleo", "atlas", "bea");
+ section(BOTS_SECTION_ID).onDrop(at(190) as never);
+ expect(actions()).toEqual([{ type: "botsOrdered", botIds: ["bea", "atlas", "cleo"] }]);
+ });
+});
+
describe("who may drag a bot: whoever the bot menu offers Move to department", () => {
it("gives a Team Cloud member without the admin scope no row to drag and no Alt+Up/Down, and starts no drag", () => {
const drawnEarlier = row("atlas").botDrag!;
diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx
index a63333e14b..b0d86a83ec 100644
--- a/src/components/Sidebar.tsx
+++ b/src/components/Sidebar.tsx
@@ -49,6 +49,7 @@ import {
PINNED_SECTION_ID,
PINNED_THREADS_SECTION_ID,
TOOLS_SECTION_ID,
+ activeBotsFirst,
partitionSidebarBots,
partitionSidebarGroups,
pinnedCircleThreadListVisible,
@@ -1107,14 +1108,12 @@ export function Sidebar({ open, onClose, collapseToIcons = false }: {
const visibleGroups = state.groups.filter((g) => !q || g.name.toLowerCase().includes(q) || g.tasks?.some((task) => task.title.toLowerCase().includes(q)));
const viewerEmail = useViewerEmail();
const parts = partitionSidebarBots(botDrag.preview(matchingBots), { universalPins, viewerEmail, welcoming: useWelcomingBot() });
- const {
- primary,
- unsectionedChief,
- pinnedBots,
- sectionChiefs,
- sectionedBots,
- unsectionedBots,
- } = simpleDesk ? withPrimaryTile(parts, myDefaultBot(state.bots, viewerEmail)?.id) : { ...parts, primary: null };
+ const saved = simpleDesk ? withPrimaryTile(parts, myDefaultBot(state.bots, viewerEmail)?.id) : { ...parts, primary: null };
+ // Shown order only: drag and Alt+Up/Down keep working on the saved order, so a reorder never stores this one.
+ const { primary, unsectionedChief, pinnedBots, sectionChiefs, sectionedBots, unsectionedBots } = { ...saved, ...activeBotsFirst(saved, (bot) => {
+ const { waiting, working } = sidebarBotStatus(bot, state.pendingQueued);
+ return waiting || working;
+ }) };
const { botChats, sectionedRooms, unsectionedRooms } = partitionSidebarGroups(visibleGroups);
const sectionNames: string[] = [...(state.sections ?? []).filter((name) => !q || name.toLowerCase().includes(q.toLowerCase())), ...botDrag.pending];
@@ -1127,7 +1126,7 @@ export function Sidebar({ open, onClose, collapseToIcons = false }: {
for (const group of sectionedRooms) {
if (!sectionNames.includes(group.section!)) sectionNames.push(group.section!);
}
- const drops = botDrag.arrange({ enabled: sections.interactive && !phone, main: unsectionedBots, sectioned: sectionedBots, departments: sectionNames, bots: state.bots, taken: departmentNames(state) });
+ const drops = botDrag.arrange({ enabled: sections.interactive && !phone, main: saved.unsectionedBots, sectioned: saved.sectionedBots, departments: sectionNames, bots: state.bots, taken: departmentNames(state) });
const naturalSectionIds = [
...(pinnedBots.length > 0 && !universalPins && !simpleDesk ? [PINNED_SECTION_ID] : []),
...(unsectionedRooms.length > 0 ? [CHANNELS_SECTION_ID] : []),
diff --git a/src/lib/sidebar-layout.test.ts b/src/lib/sidebar-layout.test.ts
index c98b64fe28..14849653a6 100644
--- a/src/lib/sidebar-layout.test.ts
+++ b/src/lib/sidebar-layout.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import {
+ activeBotsFirst,
BOT_CHATS_SECTION_ID,
BOTS_SECTION_ID,
CHANNELS_SECTION_ID,
@@ -304,3 +305,18 @@ describe("the primary tile", () => {
expect(withPrimaryTile(parts, undefined).primary).toBeNull();
});
});
+
+describe("active bots first", () => {
+ it("moves active bots up inside each list, keeps everyone's order, and touches nothing else", () => {
+ const parts = { pinnedBots: ["p1", "p2", "p3"], sectionedBots: ["w1", "h1", "w2"], unsectionedBots: ["a", "b", "c"], other: "kept" };
+ const active = new Set(["p3", "w2", "h1", "c", "b"]);
+ expect(activeBotsFirst(parts, (id) => active.has(id))).toEqual({
+ pinnedBots: ["p3", "p1", "p2"],
+ sectionedBots: ["h1", "w2", "w1"],
+ unsectionedBots: ["b", "c", "a"],
+ });
+ expect(parts.unsectionedBots).toEqual(["a", "b", "c"]);
+ expect(parts.pinnedBots).toEqual(["p1", "p2", "p3"]);
+ expect(parts.sectionedBots).toEqual(["w1", "h1", "w2"]);
+ });
+});
diff --git a/src/lib/sidebar-layout.ts b/src/lib/sidebar-layout.ts
index 37d4389ec8..38d34cfdea 100644
--- a/src/lib/sidebar-layout.ts
+++ b/src/lib/sidebar-layout.ts
@@ -136,6 +136,16 @@ export function withPrimaryTile<T extends SidebarBot>(parts: ReturnType<typeof p
return { ...parts, primary, unsectionedChief: null, pinnedBots: others(parts.pinnedBots), unsectionedBots: others(parts.unsectionedBots) };
}
+/** Bots that are working or waiting on the person move to the top of their own list (pinned, main, each department),
+ * keeping the person's order among them; the rest follow in that same order. Only how the list is shown changes. */
+export function activeBotsFirst<T>(parts: { pinnedBots: T[]; sectionedBots: T[]; unsectionedBots: T[] }, active: (bot: T) => boolean) {
+ const first = (bots: T[]) => {
+ const flags = bots.map(active);
+ return [...bots.filter((_, index) => flags[index]), ...bots.filter((_, index) => !flags[index])];
+ };
+ return { pinnedBots: first(parts.pinnedBots), sectionedBots: first(parts.sectionedBots), unsectionedBots: first(parts.unsectionedBots) };
+}
+
export function partitionSidebarGroups<T extends SidebarGroup>(groups: T[]) {
const botChats = groups.filter((group) => Boolean(group.dm));
const rooms = groups.filter((group) => !group.dm);