Skip to content

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

Sidebar threads peek: list the main conversation first and mark the one on screen

luishron123e5963c006a57
diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx
index 8e77bbcc2b..f4306446d2 100644
--- a/src/components/Sidebar.tsx
+++ b/src/components/Sidebar.tsx
@@ -905,7 +905,7 @@ export const BotListItem = memo(function BotListItem(props: BotRowProps) {
<button type="button" data-bot-actions={bot.id} aria-label={t("sidebar.bot.actions", { name: bot.name })} title={t("sidebar.bot.actions", { name: bot.name })} aria-haspopup="menu" onClick={(event) => { const rect = event.currentTarget.getBoundingClientRect(); onMenu({ botId: bot.id, x: rect.left, y: rect.bottom }); }}
className={cn("absolute top-1/2 flex size-7 -translate-y-1/2 items-center justify-center rounded text-ink-secondary hover:bg-raised hover:text-ink", simpleRow ? "end-[3px] top-1.5 size-6 translate-y-0 rounded-lg" : "right-1",
selected && !simpleRow ? "pointer-events-auto opacity-100" : "pointer-events-none opacity-0 group-hover:pointer-events-auto group-hover:opacity-100 group-focus-within:pointer-events-auto group-focus-within:opacity-100 max-md:pointer-events-auto max-md:opacity-70 touch:pointer-events-auto touch:opacity-70")}><MoreHorizontal size={15} /></button>
- </>}{!renaming && !deleting && simpleRow && <SidebarBotThreadsPeek bot={bot} dispatch={dispatch} />}
+ </>}{!renaming && !deleting && simpleRow && <SidebarBotThreadsPeek bot={bot} onScreen={selected} dispatch={dispatch} />}
{deleting && iconOnly && (
<span className="pointer-events-none absolute bottom-1 right-1 rounded-full bg-card p-1 text-ink-secondary">
<Loader2 size={12} className="animate-spin" />
diff --git a/src/components/SidebarBotThreadsPeek.dom.test.ts b/src/components/SidebarBotThreadsPeek.dom.test.ts
index 3483dace93..06f8f29456 100644
--- a/src/components/SidebarBotThreadsPeek.dom.test.ts
+++ b/src/components/SidebarBotThreadsPeek.dom.test.ts
@@ -16,18 +16,18 @@ let chat: HTMLElement;
let root: Root;
const dispatch = vi.fn<(action: Action) => void>();
-const show = async (tasks: Task[]) => {
+const show = async (tasks: Task[], shown: { threadId?: string; onScreen?: boolean } = {}) => {
await act(async () => {
root.render(createElement("div", { "data-row": "" },
- createElement(SidebarBotThreadsPeek, { bot: { id: "atlas", name: "Atlas", threadId: "main", mainThreadId: "main", tasks: [task("main"), ...tasks] }, dispatch })));
+ createElement(SidebarBotThreadsPeek, { bot: { id: "atlas", name: "Atlas", threadId: shown.threadId ?? "main", mainThreadId: "main", tasks: [task("main"), ...tasks] }, onScreen: shown.onScreen, dispatch })));
});
};
-const mount = async (tasks: Task[]) => {
+const mount = async (tasks: Task[], shown: { threadId?: string; onScreen?: boolean } = {}) => {
scroller = document.createElement("div");
chat = document.createElement("div");
document.body.append(scroller, chat);
root = createRoot(scroller);
- await show(tasks);
+ await show(tasks, shown);
row = scroller.querySelector<HTMLElement>("[data-row]")!;
};
const chevron = () => row.querySelector<HTMLButtonElement>("[data-bot-threads-peek]")!;
@@ -96,6 +96,36 @@ describe("the hover sheet of a bot's other conversations", () => {
expect(chevron().getAttribute("aria-expanded")).toBe("false");
});
+ it("lists the main conversation first, with no way to archive or delete it, and opens it", async () => {
+ await mount([task("a")], { threadId: "a", onScreen: true });
+ await open();
+ const rows = [...sheet()!.querySelectorAll<HTMLElement>("[data-peek-thread]")];
+ expect(rows.map((entry) => entry.dataset.peekThread)).toEqual(["main", "a"]);
+ expect(rows[0].querySelector("[role=img]")?.getAttribute("aria-label")).toBe("Main thread");
+ expect(rows[0].parentElement!.querySelector("[data-peek-action]")).toBeNull();
+ await act(async () => { rows[0].click(); });
+ expect(dispatch.mock.calls).toEqual([[{ type: "switchTask", botId: "atlas", threadId: "main" }]]);
+ });
+
+ it("marks the conversation on screen, and none for a bot that is not on screen", async () => {
+ const marked = () => [...sheet()!.querySelectorAll<HTMLElement>("[data-peek-thread]")].filter((entry) => entry.getAttribute("aria-current") === "page" && entry.querySelector("svg.lucide-check")).map((entry) => entry.dataset.peekThread);
+ await mount([task("a"), task("b")], { threadId: "a", onScreen: true });
+ await open();
+ expect(marked()).toEqual(["a"]);
+ expect(sheet()!.querySelectorAll("svg.lucide-check")).toHaveLength(1);
+ await act(async () => { root.unmount(); });
+ await mount([task("a"), task("b")], { threadId: "a" });
+ await open();
+ expect(sheet()!.querySelector("[aria-current], svg.lucide-check")).toBeNull();
+ });
+
+ it("keeps the conversation on screen in the list when newer ones fill it", async () => {
+ await mount([task("old", { updatedAt: 1 }), task("b", { updatedAt: 4 }), task("c", { updatedAt: 3 }), task("d", { updatedAt: 2 })], { threadId: "old", onScreen: true });
+ await open();
+ expect([...sheet()!.querySelectorAll<HTMLElement>("[data-peek-thread]")].map((entry) => entry.dataset.peekThread)).toEqual(["main", "b", "c", "d", "old"]);
+ expect(sheet()!.querySelector('[data-peek-thread="old"]')!.getAttribute("aria-current")).toBe("page");
+ });
+
it("archives a conversation from its row without opening it or closing the sheet", async () => {
await mount([task("a"), task("b")]);
await open();
diff --git a/src/components/SidebarBotThreadsPeek.test.ts b/src/components/SidebarBotThreadsPeek.test.ts
index 00b0da7e7a..927afc5f6e 100644
--- a/src/components/SidebarBotThreadsPeek.test.ts
+++ b/src/components/SidebarBotThreadsPeek.test.ts
@@ -8,18 +8,26 @@ const task = (threadId: string, extra: Partial<Task> = {}): Task => ({ threadId,
const bot = (tasks: Task[], threadId = "main") => ({ threadId, mainThreadId: "main", tasks: [task("main"), ...tasks] });
describe("peekThreads", () => {
- it("is empty for a bot with only the conversation its row opens", () => {
- expect(peekThreads(bot([]))).toEqual([]);
+ const ids = (listed: Task[]) => listed.map((entry) => entry.threadId);
+ const four = [task("a", { updatedAt: 10 }), task("b", { updatedAt: 40 }), task("c", { updatedAt: 30 }), task("d", { updatedAt: 20 })];
+
+ it("is only the main conversation for a bot with nothing else open", () => {
+ expect(ids(peekThreads(bot([])))).toEqual(["main"]);
+ });
+
+ it("lists the main conversation first, then the newest open ones up to the limit", () => {
+ const listed = peekThreads(bot([task("new", { updatedAt: 50 }), ...four]));
+ expect(ids(listed)).toEqual(["main", "new", "b", "c"]);
+ expect(listed).toHaveLength(PEEK_LIMIT + 1);
});
- it("lists the newest open conversations, capped, without the main one", () => {
- const listed = peekThreads(bot([task("a", { updatedAt: 10 }), task("b", { updatedAt: 40 }), task("c", { updatedAt: 30 }), task("d", { updatedAt: 20 })]));
- expect(listed.map((entry) => entry.threadId)).toEqual(["b", "c", "d"]);
- expect(listed).toHaveLength(PEEK_LIMIT);
+ it("lists the main conversation first while another one is on screen", () => {
+ expect(ids(peekThreads(bot([task("a", { updatedAt: 10 })], "a"), "a"))).toEqual(["main", "a"]);
});
- it("keeps the conversation on screen when it is not the main one", () => {
- expect(peekThreads(bot([task("a")], "a")).map((entry) => entry.threadId)).toEqual(["a"]);
+ it("keeps the conversation on screen when newer ones fill the limit", () => {
+ expect(ids(peekThreads(bot(four, "a"), "a"))).toEqual(["main", "b", "c", "d", "a"]);
+ expect(ids(peekThreads(bot(four, "a")))).toEqual(["main", "b", "c", "d"]);
});
it("leaves out routine runs, burst helpers and closed, archived or snoozed conversations", () => {
@@ -30,11 +38,11 @@ describe("peekThreads", () => {
task("archived", { archivedAt: 5 }),
task("snoozed", { snoozedUntil: Date.now() + 60_000 }),
];
- expect(peekThreads(bot([...closed, task("open")])).map((entry) => entry.threadId)).toEqual(["open"]);
+ expect(ids(peekThreads(bot([...closed, task("open")])))).toEqual(["main", "open"]);
});
- it("excludes the conversation in view for a bot without a main one", () => {
- expect(peekThreads({ threadId: "a", tasks: [task("a"), task("b")] }).map((entry) => entry.threadId)).toEqual(["b"]);
+ it("leads with the conversation in view for a bot without a main one", () => {
+ expect(ids(peekThreads({ threadId: "a", tasks: [task("a"), task("b")] }))).toEqual(["a", "b"]);
});
});
diff --git a/src/components/SidebarBotThreadsPeek.tsx b/src/components/SidebarBotThreadsPeek.tsx
index 33beaa2b79..56dbd36df7 100644
--- a/src/components/SidebarBotThreadsPeek.tsx
+++ b/src/components/SidebarBotThreadsPeek.tsx
@@ -1,4 +1,4 @@
-import { Archive, ChevronRight, Plus, Trash2 } from "lucide-react";
+import { Archive, Check, ChevronRight, Plus, Star, Trash2 } from "lucide-react";
import { useEffect, useId, useLayoutEffect, useMemo, useRef, useState, type Dispatch, type KeyboardEvent as ReactKeyboardEvent, type MouseEvent as ReactMouseEvent } from "react";
import { createPortal } from "react-dom";
import { cn } from "@/lib/cn";
@@ -20,14 +20,17 @@ export const PEEK_CLOSE_MS = 200;
const SHEET_WIDTH = 240;
const EDGE = 8;
-type PeekBot = Pick<Bot, "id" | "name" | "threadId" | "mainThreadId" | "tasks">;
+type PeekBot = Pick<Bot, "id" | "name" | "threadId" | "switchingTo" | "mainThreadId" | "tasks">;
-export function peekThreads(bot: Pick<PeekBot, "threadId" | "mainThreadId" | "tasks">): Task[] {
- const rowThread = botOpensTo(bot, false);
- return (bot.tasks ?? [])
- .filter((task) => task.threadId !== rowThread && !task.routineRunId && !task.openedBy?.burstId && !task.closedBy && !isArchived(task) && !isSnoozed(task))
- .sort((a, b) => threadRecency(b) - threadRecency(a))
- .slice(0, PEEK_LIMIT);
+export function peekThreads(bot: Pick<PeekBot, "threadId" | "mainThreadId" | "tasks">, current?: string): Task[] {
+ const main = botOpensTo(bot, false);
+ const others = (bot.tasks ?? [])
+ .filter((task) => task.threadId !== main && !task.routineRunId && !task.openedBy?.burstId && !task.closedBy && !isArchived(task) && !isSnoozed(task))
+ .sort((a, b) => threadRecency(b) - threadRecency(a));
+ return [
+ ...(bot.tasks ?? []).filter((task) => task.threadId === main),
+ ...others.filter((task, index) => index < PEEK_LIMIT || task.threadId === current),
+ ];
}
type Place = { left: number; top: number };
@@ -45,10 +48,13 @@ const SLOT = "pointer-events-none absolute end-[30px] top-1.5 flex size-6 items-
const ROW_ACTIONS = "pointer-events-none absolute end-2 top-1/2 flex -translate-y-1/2 items-center gap-1 opacity-0 transition-opacity group-hover/peek:pointer-events-auto group-hover/peek:opacity-100 group-focus-within/peek:pointer-events-auto group-focus-within/peek:opacity-100 touch:pointer-events-auto touch:opacity-100 motion-reduce:transition-none";
const ROW_ACTION = "flex size-6 items-center justify-center rounded-md text-ink-secondary hover:bg-ink/[0.06] hover:text-ink focus-visible:bg-ink/[0.06] focus-visible:text-ink active:bg-ink/[0.08] disabled:cursor-default disabled:opacity-50 disabled:hover:bg-transparent disabled:hover:text-ink-secondary touch:size-10";
-const ROW_TIME = "min-w-13 text-end tabular-nums transition-opacity group-hover/peek:opacity-0 group-focus-within/peek:opacity-0 touch:min-w-21 touch:opacity-0 motion-reduce:transition-none";
+const ROW_TIME = "min-w-13 text-end tabular-nums";
+const ROW_TIME_YIELDS = "transition-opacity group-hover/peek:opacity-0 group-focus-within/peek:opacity-0 touch:min-w-21 touch:opacity-0 motion-reduce:transition-none";
-export function SidebarBotThreadsPeek({ bot, dispatch }: { bot: PeekBot; dispatch: Dispatch<Action> }) {
- const threads = useMemo(() => peekThreads(bot), [bot.tasks, bot.mainThreadId, bot.threadId]);
+export function SidebarBotThreadsPeek({ bot, onScreen = false, dispatch }: { bot: PeekBot; onScreen?: boolean; dispatch: Dispatch<Action> }) {
+ const main = botOpensTo(bot, false), current = onScreen ? bot.switchingTo ?? bot.threadId : undefined;
+ const threads = useMemo(() => peekThreads(bot, current), [bot.tasks, bot.mainThreadId, bot.threadId, current]);
+ const others = threads.filter((task) => task.threadId !== main).length;
const [place, setPlace] = useState<Place | null>(null);
const motion = useHeldMenuMotion(place);
const anchor = useRef<HTMLButtonElement>(null);
@@ -60,7 +66,7 @@ export function SidebarBotThreadsPeek({ bot, dispatch }: { bot: PeekBot; dispatc
const [deletingId, setDeletingId] = useState<string | null>(null);
const deleting = deletingId ? bot.tasks?.find((task) => task.threadId === deletingId) : undefined;
const labels = useId();
- const has = threads.length > 0;
+ const has = others > 0;
const open = place !== null;
const cancel = () => window.clearTimeout(timer.current);
@@ -125,7 +131,7 @@ export function SidebarBotThreadsPeek({ bot, dispatch }: { bot: PeekBot; dispatc
const pick = (action: Action) => { close(); dispatch(action); };
const archive = (task: Task, event: ReactMouseEvent) => {
if (event.detail === 0) {
- if (threads.length === 1) focusAnchor.current = true;
+ if (others === 1) focusAnchor.current = true;
else sheet.current?.querySelector<HTMLElement>("[role=menuitem]")?.focus();
}
actions.onArchive(task, Date.now());
@@ -179,22 +185,27 @@ export function SidebarBotThreadsPeek({ bot, dispatch }: { bot: PeekBot; dispatc
{threads.map((task) => {
const waiting = task.activity === "waiting-on-you";
const mark = waiting ? t("sidebar.preview.waiting") : task.unread ? t("task.unread") : undefined;
+ const isMain = task.threadId === main, isCurrent = task.threadId === current;
const working = isWorking(task);
const titleId = `${labels}${task.threadId}`;
return (
<div key={task.threadId} role="none" data-peek-row="" className="group/peek relative shrink-0 rounded-md hover:bg-ink/[0.06] focus-within:bg-ink/[0.06]">
- <button type="button" role="menuitem" data-peek-thread={task.threadId} aria-current={task.threadId === bot.threadId ? "page" : undefined}
+ <button type="button" role="menuitem" data-peek-thread={task.threadId} aria-current={isCurrent ? "page" : undefined}
className={cn(MENU_ROW, "hover:bg-transparent focus:bg-transparent")} onClick={() => pick({ type: "switchTask", botId: bot.id, threadId: task.threadId })}>
- <span className={MENU_GUTTER}>{mark && <span role="img" aria-label={mark} title={mark} className={cn("size-1.5 rounded-full", waiting ? "bg-warning" : "bg-accent")} />}</span>
+ <span className={MENU_GUTTER}>
+ {isCurrent ? <Check size={14} className="text-accent" aria-hidden="true" />
+ : mark ? <span role="img" aria-label={mark} title={mark} className={cn("size-1.5 rounded-full", waiting ? "bg-warning" : "bg-accent")} />
+ : isMain && <span role="img" aria-label={t("sidebar.bot.simple.mainThread")} title={t("sidebar.bot.simple.mainThread")}><Star size={14} className={MENU_ICON} aria-hidden="true" /></span>}
+ </span>
<span id={titleId} className="min-w-0 flex-1 truncate">{localizedTaskTitle(task.title) || t("task.untitledConversation")}</span>
- <span className={cn(MENU_META, ROW_TIME)}>{threadUpdatedLabel(threadRecency(task), now)}</span>
+ <span className={cn(MENU_META, ROW_TIME, !isMain && ROW_TIME_YIELDS)}>{threadUpdatedLabel(threadRecency(task), now)}</span>
</button>
- <span className={ROW_ACTIONS}>
+ {!isMain && <span className={ROW_ACTIONS}>
<button type="button" role="menuitem" data-peek-action="archive" id={`${titleId}a`} aria-label={t("task.archive")} aria-labelledby={`${titleId}a ${titleId}`} title={t("task.archive")}
disabled={working} className={ROW_ACTION} onClick={(event) => archive(task, event)}><Archive size={14} aria-hidden="true" /></button>
<button type="button" role="menuitem" data-peek-action="delete" id={`${titleId}d`} aria-label={t("task.deleteAria")} aria-labelledby={`${titleId}d ${titleId}`} title={t("task.deleteAria")}
disabled={working} className={cn(ROW_ACTION, "hover:text-danger focus-visible:text-danger")} onClick={() => { close(); setDeletingId(task.threadId); }}><Trash2 size={14} aria-hidden="true" /></button>
- </span>
+ </span>}
</div>
);
})}
diff --git a/src/locales/en.json b/src/locales/en.json
index acee464452..fff8b1bc6b 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -2411,6 +2411,7 @@
"sidebar.bot.simple.viewThreads": "View threads",
"sidebar.bot.simple.setMainThread": "Set as main thread",
"sidebar.bot.simple.isMainThread": "This is already the main thread",
+ "sidebar.bot.simple.mainThread": "Main thread",
"sidebar.bot.duplicate": "Duplicate",
"sidebar.bot.archive": "Archive",
"sidebar.bot.archiveAria": "Archive {name}",
diff --git a/src/locales/pt-br.json b/src/locales/pt-br.json
index 9fc8856145..d4810161f6 100644
--- a/src/locales/pt-br.json
+++ b/src/locales/pt-br.json
@@ -2243,6 +2243,7 @@
"sidebar.bot.simple.viewThreads": "Ver conversas",
"sidebar.bot.simple.setMainThread": "Definir como conversa principal",
"sidebar.bot.simple.isMainThread": "Esta já é a conversa principal",
+ "sidebar.bot.simple.mainThread": "Conversa principal",
"sidebar.bot.duplicate": "Duplicar",
"sidebar.bot.archive": "Arquivar",
"sidebar.bot.archiveAria": "Arquivar {name}",