Skip to content

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

Sidebar: a drag and Alt+Up/Down arrange the rows shown in place, not the ones Unread is listing

luishron1239528330aad4e
diff --git a/src/components/Sidebar.simple-layout.test.ts b/src/components/Sidebar.simple-layout.test.ts
index 0d3b29e5f4..53623f29b6 100644
--- a/src/components/Sidebar.simple-layout.test.ts
+++ b/src/components/Sidebar.simple-layout.test.ts
@@ -47,6 +47,8 @@ import { initialState, reducer } from "@/state/store";
import { BOTS_SECTION_ID, CHANNELS_SECTION_ID, PINNED_SECTION_ID, UNREAD_SECTION_ID, userSectionId } from "@/lib/sidebar-layout";
import { SIDEBAR_COLLAPSED_SECTIONS_KEY, SIDEBAR_DENSITY_KEY, SIDEBAR_SECTION_ORDER_KEY, SIDEBAR_WIDTH_KEY, SIMPLE_SIDEBAR_SECTION_ORDER_KEY, SIMPLE_SIDEBAR_WIDTH_KEY, setSidebarDensity } from "@/lib/sidebar-preferences";
import { Sidebar, TeamMenuItems } from "./Sidebar";
+import { BOT_ROW_DRAG_TYPE } from "@/lib/sidebar-bot-drop";
+import type { BotRowDrag } from "./SidebarBotDrag";
const drawSidebar = (Sidebar as unknown as { type: (props: import("react").ComponentProps<typeof Sidebar>) => import("react").ReactNode }).type;
import { PinnedBotRoster } from "./PinnedCircleThreads";
import { SidebarFooter } from "./SidebarFooter";
@@ -461,6 +463,60 @@ describe("the sidebar's Unread section", () => {
expect(rows.map((row) => [(row.props.bot as Bot).id, row.props.botDrag !== undefined])).toEqual([["cora", false], ["dodo", false], ["bea", false], ["atlas", true]]);
});
+ const rowOf = (tree: ReactElement<Props>[], id: string) => tree.find((node) => (node.props.bot as Bot | undefined)?.id === id)!.props as { botDrag: BotRowDrag };
+ const sectionZone = (tree: ReactElement<Props>[], id: string) => tree.find((node) => node.props["data-sidebar-section-id"] === id)!.props as unknown as Record<"onDragOver" | "onDrop", (event: unknown) => void>;
+ const dragEvent = (types: string[] = []) => ({
+ preventDefault: vi.fn(), stopPropagation: () => {}, clientY: 0, dataTransfer: { types, setData: () => {}, getData: () => "" },
+ currentTarget: { querySelectorAll: () => [], getBoundingClientRect: () => ({ top: 0, height: 40 }) },
+ });
+
+ it.each([["cora", -1], ["atlas", 1]] as const)("moves %s past the row it is shown next to with Alt+arrow, not past one Unread is listing", (id, direction) => {
+ vi.stubGlobal("requestAnimationFrame", () => 0);
+ vi.stubGlobal("fetch", () => new Promise(() => {}));
+ fixture.state = { ...fixture.state, bots: [bot("atlas"), bot("bea", { unread: true }), bot("cora"), bot("dodo", { section: "Work" })] };
+ let tree = render().tree;
+ expect(listedBots(tree)).toEqual(["bea", "atlas", "cora", "dodo"]);
+
+ rowOf(tree, id).botDrag.step(id, direction);
+ expect(fixture.dispatch).toHaveBeenCalledWith({ type: "botsOrdered", botIds: ["cora", "atlas"] });
+ fixture.state = reducer(fixture.state, { type: "botsOrdered", botIds: ["cora", "atlas"] });
+ tree = render().tree;
+ expect(listedBots(tree)).toEqual(["bea", "cora", "atlas", "dodo"]);
+ expect(fixture.state.bots.map((candidate) => candidate.id)).toEqual(["cora", "bea", "atlas", "dodo"]);
+ });
+
+ it("offers the main list to a bot dragged out of its department while every bot of that list is unread", () => {
+ fixture.state = { ...fixture.state, bots: [bot("atlas", { unread: true }), bot("bea", { unread: true }), bot("dodo", { section: "Work" })] };
+ let tree = render().tree;
+ expect(sectionOrder(tree)).toEqual([UNREAD_SECTION_ID, CHANNELS_SECTION_ID, userSectionId("Work")]);
+
+ rowOf(tree, "dodo").botDrag.row("dodo").onDragStart(dragEvent() as never);
+ tree = render().tree;
+ expect(sectionOrder(tree)).toEqual([UNREAD_SECTION_ID, BOTS_SECTION_ID, CHANNELS_SECTION_ID, userSectionId("Work")]);
+ expect(tree.some((node) => node.props["data-bot-drop-slot"] === "")).toBe(true);
+
+ sectionZone(tree, BOTS_SECTION_ID).onDrop(dragEvent([BOT_ROW_DRAG_TYPE]));
+ expect(fixture.dispatch).toHaveBeenCalledWith({ type: "updateBot", botId: "dodo", patch: { section: "" } });
+ });
+
+ it("takes no dragged section and shows no line for one, since it has no place in the order", () => {
+ withUnread();
+ const lines = (tree: ReactElement<Props>[]) => tree.filter((node) => node.props.className === "mx-2 h-0.5 rounded-full bg-accent").length;
+ let tree = render().tree;
+ headers(tree).find((header) => header.props.name === "Work")!.props.onDragStart!(dragEvent() as never);
+ tree = render().tree;
+
+ const overBots = dragEvent();
+ sectionZone(tree, BOTS_SECTION_ID).onDragOver(overBots);
+ expect(overBots.preventDefault).toHaveBeenCalled();
+ expect(lines(render().tree)).toBe(1);
+
+ const overUnread = dragEvent();
+ sectionZone(tree, UNREAD_SECTION_ID).onDragOver(overUnread);
+ expect(overUnread.preventDefault).not.toHaveBeenCalled();
+ expect(lines(render().tree)).toBe(0);
+ });
+
it("leaves pinned bots and the Chief where they are, and sits under Advanced's Pinned heading", () => {
fixture.state = { ...fixture.state, bots: [
bot("chief", { chiefOfStaff: true, unread: true }), bot("atlas", { pinned: true, unread: true }), bot("bea", { unread: true }), bot("cora"),
diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx
index 4c63a3858a..141d1fe6a1 100644
--- a/src/components/Sidebar.tsx
+++ b/src/components/Sidebar.tsx
@@ -49,12 +49,12 @@ import {
PINNED_THREADS_SECTION_ID,
TOOLS_SECTION_ID,
UNREAD_SECTION_ID,
- activeBotsFirst,
partitionSidebarBots,
partitionSidebarGroups,
pinnedCircleThreadListVisible,
+ sectionsInUse,
+ sidebarBotRows,
sidebarSectionLabel,
- unreadBotsApart,
userSectionId,
userSectionName,
withPrimaryTile,
@@ -1099,17 +1099,16 @@ export const Sidebar = memo(function Sidebar({ open, onClose, collapseToIcons =
const viewerEmail = useViewerEmail();
const parts = partitionSidebarBots(botDrag.preview(matchingBots), { universalPins, viewerEmail, welcoming: useWelcomingBot(), searching: Boolean(q) });
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, ...ordered } = { ...saved, ...activeBotsFirst(saved, (bot) => {
+ // Shown order only: drag and Alt+Up/Down work on the saved order of the rows left in place, never on this one.
+ const { primary, unsectionedChief } = saved;
+ const { pinnedBots, filed, unreadBots, sectionChiefs, sectionedBots, unsectionedBots, inPlace } = sidebarBotRows(saved, (bot) => {
const { waiting, working } = sidebarBotStatus(bot, state.pendingQueued);
return waiting || working;
- }) };
+ }, !q);
const { botChats, sectionedRooms, unsectionedRooms } = partitionSidebarGroups(visibleGroups);
- const sectionNames: string[] = [...(state.sections ?? []).filter((name) => !q || name.toLowerCase().includes(q.toLowerCase())), ...botDrag.pending];
- for (const { section } of [...ordered.sectionedBots, ...ordered.sectionChiefs, ...sectionedRooms]) if (!sectionNames.includes(section!)) sectionNames.push(section!);
- const { unreadBots, sectionChiefs, sectionedBots, unsectionedBots } = unreadBotsApart(ordered, !q);
- const drops = botDrag.arrange({ enabled: (sections.interactive || density === "icons") && !phone, withinList: density === "icons", main: saved.unsectionedBots, sectioned: saved.sectionedBots, departments: sectionNames, bots: state.bots, taken: departmentNames(state) });
+ const sectionNames = sectionsInUse([...(state.sections ?? []).filter((name) => !q || name.toLowerCase().includes(q.toLowerCase())), ...botDrag.pending], [...filed, ...sectionedRooms]);
+ const drops = botDrag.arrange({ enabled: (sections.interactive || density === "icons") && !phone, withinList: density === "icons", main: inPlace.unsectionedBots, sectioned: inPlace.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] : []),
@@ -1127,7 +1126,7 @@ export const Sidebar = memo(function Sidebar({ open, onClose, collapseToIcons =
const attention = crossBotAttentionThreads(state.bots, state.pendingQueued, undefined, state.groups);
const pinnedThreads = crossBotPinnedThreads(state.bots, state.groups, state.pendingQueued);
const pendingBotUndo = teamFeedback?.restoreBot;
- const botRow = (bot: Bot, fixed = false) => botRowProps(state, dispatch, bot, { density, query: q, onMenu: setMenu, renameRequest: renameRequest?.botId === bot.id ? renameRequest.nonce : 0, ...(fixed ? {} : drops.row(bot.id)) });
+ const botRow = (bot: Bot) => botRowProps(state, dispatch, bot, { density, query: q, onMenu: setMenu, renameRequest: renameRequest?.botId === bot.id ? renameRequest.nonce : 0, ...drops.row(bot.id) });
const railSearch = () => {
if (collapseToIcons) return openCommandPalette();
setSearchOpen(true);
@@ -1463,7 +1462,7 @@ export const Sidebar = memo(function Sidebar({ open, onClose, collapseToIcons =
row={botRow}
listItem={BotListItem}
/>
- ) : sectionBotItems.map((bot) => <BotListItem key={bot.id} {...botRow(bot, id === UNREAD_SECTION_ID)} />)}
+ ) : sectionBotItems.map((bot) => <BotListItem key={bot.id} {...botRow(bot)} />)}
{!remoteClient && sectionName && layoutInteractive && !drops.naming(sectionName) && (!simple || sectionChiefItems.length + sectionGroupItems.length + sectionBotItems.length === 0) && (
<button onClick={() => setMoveToTeam(sectionName)} aria-label={t(state.bots.some(bot => !bot.hidden && bot.section?.trim() === sectionName) ? "team.manageBotsIn" : "team.addBotsTo", { name: sectionName })}
className="mx-3 my-1 flex items-center gap-1.5 rounded-md px-2 py-1.5 text-[12px] text-ink-secondary hover:bg-raised hover:text-ink">
diff --git a/src/components/SidebarSections.tsx b/src/components/SidebarSections.tsx
index 3e644ecd2c..3cc00ef5a9 100644
--- a/src/components/SidebarSections.tsx
+++ b/src/components/SidebarSections.tsx
@@ -108,6 +108,11 @@ export function useSidebarSections({ simple, rail, density, query, label, sideba
zone: (id: string) => ({
onDragOver: (event: DragEvent<HTMLElement>) => {
if (!interactive || !drag.current.from) return;
+ // A section outside the order being arranged has no place to give: it takes no drop and shows no line.
+ if (!sectionIds.includes(id)) {
+ drag.current.over = null;
+ return setDropTarget(null);
+ }
event.preventDefault();
event.dataTransfer.dropEffect = "move";
const rect = event.currentTarget.getBoundingClientRect();
diff --git a/src/lib/sidebar-layout.test.ts b/src/lib/sidebar-layout.test.ts
index bb11753ea5..b55f870c0c 100644
--- a/src/lib/sidebar-layout.test.ts
+++ b/src/lib/sidebar-layout.test.ts
@@ -21,6 +21,8 @@ import {
sidebarSectionLabel,
simpleOrderedSections,
simpleSidebarSections,
+ sectionsInUse,
+ sidebarBotRows,
unreadBotsApart,
userSectionId,
userSectionName,
@@ -374,6 +376,25 @@ describe("the unread section", () => {
expect(unreadBotsApart(parts, false)).toEqual({ ...parts, unreadBots: [] });
});
+ it("arranges the saved order of the rows left in place, whatever is shown first or listed as unread", () => {
+ const unread = (id: string) => ({ id, unread: true });
+ const saved = { pinnedBots: [read("p1"), read("p2")], sectionChiefs: [unread("lead")], sectionedBots: [read("w1"), unread("w2"), read("w3")], unsectionedBots: [read("a"), unread("u"), read("b")] };
+ const rows = sidebarBotRows(saved, (bot) => ["b", "w3", "p2"].includes(bot.id));
+ const ids = (bots: { id: string }[]) => bots.map((bot) => bot.id);
+ expect([ids(rows.pinnedBots), ids(rows.unsectionedBots), ids(rows.sectionedBots)]).toEqual([["p2", "p1"], ["b", "a"], ["w3", "w1"]]);
+ expect(ids(rows.unreadBots).sort()).toEqual(["lead", "u", "w2"]);
+ expect(ids(rows.filed)).toEqual(["w3", "w1", "w2", "lead"]);
+ expect([ids(rows.inPlace.unsectionedBots), ids(rows.inPlace.sectionedBots)]).toEqual([["a", "b"], ["w1", "w3"]]);
+
+ const searching = sidebarBotRows(saved, () => false, false);
+ expect(searching.unreadBots).toEqual([]);
+ expect(ids(searching.inPlace.unsectionedBots)).toEqual(["a", "u", "b"]);
+ });
+
+ it("lists a department that only a shown bot or room names, after the given ones and once", () => {
+ expect(sectionsInUse(["Work"], [{ section: "Ops" }, { section: "Work" }, { section: "Ops" }, {}, { section: null }])).toEqual(["Work", "Ops"]);
+ });
+
it("goes under the pinned bots when they lead the list, at the top otherwise, and nowhere with nothing unread", () => {
expect(withUnreadSection([PINNED_SECTION_ID, BOTS_SECTION_ID], true)).toEqual([PINNED_SECTION_ID, UNREAD_SECTION_ID, BOTS_SECTION_ID]);
expect(withUnreadSection([BOTS_SECTION_ID, PINNED_SECTION_ID], true)).toEqual([UNREAD_SECTION_ID, BOTS_SECTION_ID, PINNED_SECTION_ID]);
diff --git a/src/lib/sidebar-layout.ts b/src/lib/sidebar-layout.ts
index 1dd355b580..1286cbe108 100644
--- a/src/lib/sidebar-layout.ts
+++ b/src/lib/sidebar-layout.ts
@@ -154,6 +154,13 @@ export function activeBotsFirst<T>(parts: { pinnedBots: T[]; sectionedBots: T[];
return { pinnedBots: first(parts.pinnedBots), sectionedBots: first(parts.sectionedBots), unsectionedBots: first(parts.unsectionedBots) };
}
+/** The departments to list: the given ones, then any other that a bot or a room shown here is filed under. */
+export function sectionsInUse(names: readonly string[], filed: ReadonlyArray<{ section?: string | null }>): string[] {
+ const result = [...names];
+ for (const { section } of filed) if (section && !result.includes(section)) result.push(section);
+ return result;
+}
+
type UnreadListBot = {
unread?: boolean;
tasks?: ReadonlyArray<{ unread?: boolean; routineRunId?: string; archivedAt?: number; updatedAt?: number; createdAt?: number }> | null;
@@ -176,6 +183,19 @@ export function unreadBotsApart<T extends UnreadListBot>(parts: { sectionChiefs:
return { unreadBots, sectionChiefs: read(parts.sectionChiefs), sectionedBots: read(parts.sectionedBots), unsectionedBots: read(parts.unsectionedBots) };
}
+/** The rows of the bot list as shown, busy bots first and unread ones in a list of their own, with `filed`, every
+ * shown bot that belongs to a department. `inPlace` is the saved order of the rows left where they belong: what a drag
+ * or Alt+Up/Down arranges, so neither stores the shown order nor counts a row that Unread is listing. */
+export function sidebarBotRows<T extends UnreadListBot>(saved: { pinnedBots: T[]; sectionChiefs: T[]; sectionedBots: T[]; unsectionedBots: T[] }, busy: (bot: T) => boolean, unreadApart = true) {
+ const ordered = { ...saved, ...activeBotsFirst(saved, busy) };
+ return {
+ pinnedBots: ordered.pinnedBots,
+ filed: [...ordered.sectionedBots, ...ordered.sectionChiefs],
+ ...unreadBotsApart(ordered, unreadApart),
+ inPlace: unreadBotsApart(saved, unreadApart),
+ };
+}
+
/** Unread sits under the pinned bots when they lead the list and above everything else otherwise. */
export function withUnreadSection(ids: SidebarSectionId[], shown: boolean): SidebarSectionId[] {
if (!shown) return ids;