Skip to content

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

Chat: right-click a message bubble to open its actions menu

Krishna Dobhalef7c222b7f0e
diff --git a/docs/verification/evidence/message-context-menu/advanced-bot-rightclick.png b/docs/verification/evidence/message-context-menu/advanced-bot-rightclick.png
new file mode 100644
index 0000000000..9287793043
Binary files /dev/null and b/docs/verification/evidence/message-context-menu/advanced-bot-rightclick.png differ
diff --git a/docs/verification/evidence/message-context-menu/advanced-user-rightclick.png b/docs/verification/evidence/message-context-menu/advanced-user-rightclick.png
new file mode 100644
index 0000000000..5c72fdee19
Binary files /dev/null and b/docs/verification/evidence/message-context-menu/advanced-user-rightclick.png differ
diff --git a/docs/verification/evidence/message-context-menu/simple-bot-rightclick.png b/docs/verification/evidence/message-context-menu/simple-bot-rightclick.png
new file mode 100644
index 0000000000..cfbaedd826
Binary files /dev/null and b/docs/verification/evidence/message-context-menu/simple-bot-rightclick.png differ
diff --git a/docs/verification/evidence/message-context-menu/simple-user-rightclick.png b/docs/verification/evidence/message-context-menu/simple-user-rightclick.png
new file mode 100644
index 0000000000..996d2b723e
Binary files /dev/null and b/docs/verification/evidence/message-context-menu/simple-user-rightclick.png differ
diff --git a/src/components/ChatView.tsx b/src/components/ChatView.tsx
index 2b4a032f18..de17114880 100644
--- a/src/components/ChatView.tsx
+++ b/src/components/ChatView.tsx
@@ -408,7 +408,8 @@ const Bubble = memo(function Bubble({
const togglePin = () =>
dispatch({ type: "updateTask", botId, threadId, patch: { pinnedMessageId: pinned ? "" : message.id } });
const botActions = !user && (
- <MessageActions side="bot" forceOpen={viewRaw || speaking} menu={simple ? [...replyAndCopy(() => onReply(message), text), ...botMenu, ...forkAction] : [...botMenu, ...forkAction]}>
+ <MessageActions side="bot" forceOpen={viewRaw || speaking} menu={simple ? [...replyAndCopy(() => onReply(message), text), ...botMenu, ...forkAction] : [...botMenu, ...forkAction]}
+ contextItems={simple ? undefined : replyAndCopy(() => onReply(message), text)}>
{!simple && <ReplyAction onReply={() => onReply(message)} />}
{canReact && !peer && <ReactAction threadId={threadId} message={message} />}
{!simple && text && <CopyButton text={text} className={messageActionClass} />}
@@ -425,6 +426,7 @@ const Bubble = memo(function Bubble({
{user && (
<MessageActions
side="user"
+ contextItems={simple ? undefined : replyAndCopy(() => onReply(message), visibleText)}
menu={[
...(simple ? replyAndCopy(() => onReply(message), visibleText) : []),
...userMessageMenu({
diff --git a/src/components/GroupView.tsx b/src/components/GroupView.tsx
index b17747b302..6dab7b5511 100644
--- a/src/components/GroupView.tsx
+++ b/src/components/GroupView.tsx
@@ -38,7 +38,7 @@ import { useAdvancedMode } from "@/lib/interface-mode";
import { CopyButton, MESSAGE_ROW, MessageActions, messageActionClass, ReplyAction } from "./MessageActions";
import { ReactAction, ReactionChips, useCanReact } from "./Reactions";
import { ClusterLabel } from "./RoomClusterLabel";
-import { roomPin, useBotMessageMenu, userMessageMenu } from "./message-menu";
+import { replyAndCopy, roomPin, useBotMessageMenu, userMessageMenu } from "./message-menu";
import { forkMessageItem, ForkLinks } from "./ForkMessage";
import { PHONE_LAYOUT, useMediaQuery } from "@/lib/use-media-query";
import { RawMarkdownView, RawToggleAction } from "./RawMarkdownToggle";
@@ -193,7 +193,7 @@ function RoomTextMessage({
const blocks = useMemo(() => simple && !user && !viewRaw && botText.trim() ? splitReplyBlocks(botText) : null, [simple, user, viewRaw, botText]);
const tabled = Boolean(blocks?.some((block) => block.kind === "table"));
const botActions = !user && (
- <MessageActions side="bot" forceOpen={viewRaw || speaking} menu={[...botMenu, ...forkAction]}>
+ <MessageActions side="bot" forceOpen={viewRaw || speaking} menu={[...botMenu, ...forkAction]} contextItems={replyAndCopy(() => onReply(m), botText)}>
<ReplyAction onReply={() => onReply(m)} />
{canReact && m.from && <ReactAction threadId={group.threadId} message={m} />}
{botText && <CopyButton text={botText} className={messageActionClass} />}
@@ -206,7 +206,7 @@ function RoomTextMessage({
{speaker && <div className="mb-0.5 px-1 text-right text-[13px] [overflow-wrap:anywhere] font-semibold text-ink-secondary">{speaker}</div>}
<div className={cn("flex w-full items-end gap-1.5", user ? "justify-end" : "justify-start")}>
{user && (
- <MessageActions side="user" menu={[...userMessageMenu({ messageId: m.id, pin }), ...forkAction]}>
+ <MessageActions side="user" menu={[...userMessageMenu({ messageId: m.id, pin }), ...forkAction]} contextItems={replyAndCopy(() => onReply(m), display)}>
<ReplyAction onReply={() => onReply(m)} />
{Boolean(display.trim()) && <CopyButton text={display} className={messageActionClass} />}
</MessageActions>
diff --git a/src/components/MessageActions.toggle.test.ts b/src/components/MessageActions.toggle.test.ts
index 09640b26d2..456d41f7d9 100644
--- a/src/components/MessageActions.toggle.test.ts
+++ b/src/components/MessageActions.toggle.test.ts
@@ -26,10 +26,12 @@ async function render(forceOpen = false) {
await act(async () => {
root.render(
createElement("div", { [MESSAGE_ROW]: "" },
- createElement("p", null, "an answer"),
+ createElement("div", { "data-chat-bubble": "" }, createElement("p", null, "an answer")),
+ createElement("span", { "data-outside": "" }, "gap"),
createElement(MessageActions, {
side: "bot",
forceOpen,
+ contextItems: [{ key: "reply", icon: null, label: "Reply", onSelect: () => picked.push("reply") }],
menu: [
{ key: "pin", icon: null, label: "Pin message", onSelect: () => picked.push("pin") },
{ key: "raw", icon: null, label: "Show raw markdown", onSelect: () => picked.push("raw") },
@@ -124,6 +126,31 @@ describe("message actions row", () => {
expect(items()).toHaveLength(0);
});
+ const rightClick = async (target: Element) => {
+ const event = new MouseEvent("contextmenu", { bubbles: true, cancelable: true, clientX: 40, clientY: 20 });
+ await act(async () => { target.dispatchEvent(event); });
+ return event.defaultPrevented;
+ };
+
+ it("opens the menu with the inline actions first on a right-click of the bubble", async () => {
+ await render();
+ expect(await rightClick(body())).toBe(true);
+ expect(items().map((item) => item.textContent)).toEqual(["Reply", "Pin message", "Show raw markdown"]);
+ await act(async () => items()[0]!.click());
+ expect(picked).toEqual(["reply"]);
+ await act(async () => more().click());
+ expect(items().map((item) => item.textContent)).toEqual(["Pin message", "Show raw markdown"]);
+ });
+
+ it("leaves the native menu for a selection or a right-click beside the bubble", async () => {
+ await render();
+ expect(await rightClick(host.querySelector("[data-outside]")!)).toBe(false);
+ window.getSelection()!.selectAllChildren(body());
+ expect(await rightClick(body())).toBe(false);
+ window.getSelection()!.removeAllRanges();
+ expect(items()).toHaveLength(0);
+ });
+
it("keeps a row that must stay reachable out", async () => {
await render(true);
expect(shown()).toBe(true);
diff --git a/src/components/MessageActions.tsx b/src/components/MessageActions.tsx
index 3f8e03f0f8..652ad5f8f0 100644
--- a/src/components/MessageActions.tsx
+++ b/src/components/MessageActions.tsx
@@ -45,12 +45,15 @@ export function MessageActions({
forceOpen = false,
className,
menu: items = [],
+ contextItems = [],
children,
}: {
side: "user" | "bot";
forceOpen?: boolean;
className?: string;
menu?: MessageMenuItem[];
+ /** Added before `menu` when a right-click on the bubble opens it, for the actions that are inline buttons otherwise. */
+ contextItems?: MessageMenuItem[];
children?: ReactNode;
}) {
const rootRef = useRef<HTMLDivElement>(null);
@@ -59,9 +62,11 @@ export function MessageActions({
const menuRef = useRef<HTMLDivElement>(null);
const menuId = useId();
const [menuOpen, setMenuOpen] = useState(false);
+ const [pointAt, setPointAt] = useState<{ x: number; y: number; clientX: number; clientY: number } | null>(null);
+ const offered = pointAt ? [...contextItems, ...items] : items;
// use() may run conditionally: a closed menu has no reason to follow the turn.
- const busy = menuOpen && items.some((item) => item.idleOnly) ? use(ChatBusyContext) : false;
- const menu = busy ? items.filter((item) => !item.idleOnly) : items;
+ const busy = menuOpen && offered.some((item) => item.idleOnly) ? use(ChatBusyContext) : false;
+ const menu = busy ? offered.filter((item) => !item.idleOnly) : offered;
const [hovered, setHovered] = useState(false);
const [keyboardFocus, setKeyboardFocus] = useState(false);
const [tapped, setTapped] = useState(false);
@@ -70,6 +75,8 @@ export function MessageActions({
const mirrored = side === "user";
const streaming = use(StreamingReplyContext);
const shown = !streaming && (forceOpen || menuOpen || hovered || keyboardFocus || tapped);
+ const canPointRef = useRef(false);
+ canPointRef.current = !streaming && items.length + contextItems.length > 0;
useEffect(() => {
const row = rootRef.current?.closest<HTMLElement>(`[${MESSAGE_ROW}]`) ?? rootRef.current;
@@ -109,6 +116,17 @@ export function MessageActions({
if (window.getSelection()?.toString()) return;
setTapped((value) => !value);
};
+ const context = (event: MouseEvent) => {
+ if (!canPointRef.current || (event as Partial<PointerEvent>).pointerType === "touch") return;
+ const target = event.target;
+ if (!(target instanceof Element) || !target.closest("[data-chat-bubble]") || target.closest(INTERACTIVE)) return;
+ if (window.getSelection()?.toString().trim()) return;
+ const box = rootRef.current?.getBoundingClientRect();
+ if (!box) return;
+ event.preventDefault();
+ setPointAt({ x: event.clientX - box.left, y: event.clientY - box.top, clientX: event.clientX, clientY: event.clientY });
+ setMenuOpen(true);
+ };
row.addEventListener("pointerenter", enter);
row.addEventListener("pointerleave", leave);
row.addEventListener("focusin", focusIn);
@@ -117,6 +135,7 @@ export function MessageActions({
row.addEventListener("pointermove", move);
row.addEventListener("pointerup", up);
row.addEventListener("pointercancel", cancel);
+ row.addEventListener("contextmenu", context);
return () => {
if (press) clearTimeout(press.timer);
row.removeEventListener("pointerenter", enter);
@@ -127,6 +146,7 @@ export function MessageActions({
row.removeEventListener("pointermove", move);
row.removeEventListener("pointerup", up);
row.removeEventListener("pointercancel", cancel);
+ row.removeEventListener("contextmenu", context);
};
}, []);
@@ -139,6 +159,12 @@ export function MessageActions({
useLayoutEffect(() => {
if (!menuOpen) return;
const menuBox = menuRef.current?.getBoundingClientRect();
+ if (pointAt && menuBox) {
+ setMenuAbove(pointAt.clientY + menuBox.height > window.innerHeight - 8 && pointAt.clientY - menuBox.height > 8);
+ setMenuFlipped(pointAt.clientX + menuBox.width > window.innerWidth - 8 && pointAt.clientX - menuBox.width >= 8);
+ menuRef.current?.querySelector<HTMLButtonElement>("[role=menuitem]:not(:disabled)")?.focus();
+ return;
+ }
const more = moreRef.current?.getBoundingClientRect();
if (menuBox && more) setMenuAbove(more.bottom + 4 + menuBox.height > window.innerHeight - 8 && more.top - 4 - menuBox.height > 8);
const rowBox = rootRef.current?.getBoundingClientRect();
@@ -151,7 +177,7 @@ export function MessageActions({
setMenuFlipped(mirrored ? !fitsEnd && fitsStart : !fitsStart && fitsEnd);
}
menuRef.current?.querySelector<HTMLButtonElement>("[role=menuitem]:not(:disabled)")?.focus();
- }, [menuOpen]);
+ }, [menuOpen, pointAt]);
const moveFocus = (event: KeyboardEvent<HTMLDivElement>) => {
const items = [...(menuRef.current?.querySelectorAll<HTMLButtonElement>("[role=menuitem]:not(:disabled)") ?? [])];
@@ -184,11 +210,14 @@ export function MessageActions({
}}
>
{children}
- {menu.length > 0 && (
+ {items.length > 0 && (
<button
ref={moreRef}
type="button"
- onClick={() => setMenuOpen((value) => !value)}
+ onClick={() => {
+ setPointAt(null);
+ setMenuOpen((value) => !value);
+ }}
aria-label={t("chat.messageActions")}
title={t("chat.messageActions")}
aria-haspopup="menu"
@@ -206,10 +235,11 @@ export function MessageActions({
role="menu" data-look="menu"
aria-label={t("chat.messageActions")}
onKeyDown={moveFocus}
+ style={pointAt ? { left: pointAt.x, top: pointAt.y, translate: `${menuFlipped ? "-100%" : "0"} ${menuAbove ? "-100%" : "0"}` } : undefined}
className={cn(
"absolute z-40 min-w-52 max-w-[calc(100vw-2rem)] overflow-hidden", MENU_SHEET,
- menuAbove ? "bottom-full mb-1" : "top-full mt-1",
- mirrored !== menuFlipped ? "end-0" : "start-0",
+ !pointAt && (menuAbove ? "bottom-full mb-1" : "top-full mt-1"),
+ !pointAt && (mirrored !== menuFlipped ? "end-0" : "start-0"),
)}
>
{menu.map((item) => (