Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(chat): handle disabled radial menu actions

vastsa39255266469b
diff --git a/apps/desktop/src/components/RadialMenu.tsx b/apps/desktop/src/components/RadialMenu.tsx
index eae4c8825..34cb09739 100644
--- a/apps/desktop/src/components/RadialMenu.tsx
+++ b/apps/desktop/src/components/RadialMenu.tsx
@@ -92,6 +92,8 @@ export function RadialMenu({
const [activeIndex, setActiveIndex] = useState<number | null>(null);
const [open, setOpen] = useState(false);
+ const firstEnabledIndex = items.findIndex((item) => !item.disabled);
+
// The right-click interrupted whatever owned focus; closing puts it back so
// the ring never strands focus on `<body>`.
useEffect(() => {
@@ -153,11 +155,16 @@ export function RadialMenu({
};
}, [onClose]);
- // The ring owns the pointer, so the first item takes focus for the keyboard
- // path and the hover fill follows whichever slot is active.
+ // Focus the first actionable item; when every slot is empty, focus the menu
+ // itself so keyboard dismissal remains available.
useEffect(() => {
- itemRefs.current[0]?.focus({ preventScroll: true });
- }, []);
+ const firstEnabledItem = itemRefs.current[firstEnabledIndex];
+ if (firstEnabledItem) {
+ firstEnabledItem.focus({ preventScroll: true });
+ } else {
+ rootRef.current?.focus({ preventScroll: true });
+ }
+ }, [firstEnabledIndex]);
useEffect(() => {
if (activeIndex === null) return;
@@ -177,9 +184,21 @@ export function RadialMenu({
: 0;
if (!step) return;
event.preventDefault();
- setActiveIndex((current) =>
- ((current ?? 0) + step + items.length) % items.length,
+ if (!items.length) return;
+ const focusedIndex = itemRefs.current.findIndex(
+ (item) => item === document.activeElement,
);
+ setActiveIndex((current) => {
+ const cursor = current ?? focusedIndex;
+ const startIndex =
+ cursor >= 0 ? cursor : step > 0 ? items.length - 1 : 0;
+ for (let distance = 1; distance <= items.length; distance += 1) {
+ const index =
+ (startIndex + step * distance + items.length) % items.length;
+ if (!items[index]?.disabled) return index;
+ }
+ return current;
+ });
};
const width = OUTER_RADIUS * 2;
@@ -209,6 +228,7 @@ export function RadialMenu({
style={{ top: `${top}px`, left: `${left}px`, width, height: width }}
role="menu"
aria-label={label}
+ tabIndex={-1}
onKeyDown={onKeyDown}
onPointerMove={(event) => {
const rect = event.currentTarget.getBoundingClientRect();
diff --git a/apps/desktop/src/features/chat/transcript/ChatTranscript.tsx b/apps/desktop/src/features/chat/transcript/ChatTranscript.tsx
index 6b5a934dc..fd5bd6113 100644
--- a/apps/desktop/src/features/chat/transcript/ChatTranscript.tsx
+++ b/apps/desktop/src/features/chat/transcript/ChatTranscript.tsx
@@ -42,7 +42,7 @@ import {
useChatTextActions,
useTranscriptMenu,
} from "./TranscriptMenu";
-import { conversationMenuItems } from "./menu-items";
+import { conversationMenuItems, hasConversationToCopy } from "./menu-items";
import { browserPluginTab } from "../../../lib/work-panel-tabs";
import { isRemoteSession } from "../../../lib/remote-session-safety";
import {
@@ -266,6 +266,13 @@ function TranscriptBody({
}
};
+ const canCopyConversation = hasConversationToCopy({
+ sessionId,
+ hasMoreBefore,
+ hasMoreAfter,
+ messages,
+ });
+
/* The newest settled answer, for the ring's optional copy slot. */
const lastAnswer = useMemo(() => {
for (let index = messages.length - 1; index >= 0; index -= 1) {
@@ -289,7 +296,13 @@ function TranscriptBody({
const label = t(RADIAL_MENU_ACTION_LABEL_KEYS[action]);
switch (action) {
case "copy-conversation":
- return { id, label, icon: <IconCopy size={18} />, onSelect: () => void copyConversation() };
+ return {
+ id,
+ label,
+ icon: <IconCopy size={18} />,
+ disabled: !canCopyConversation,
+ onSelect: () => void copyConversation(),
+ };
case "select-conversation":
return {
id,
@@ -376,9 +389,7 @@ function TranscriptBody({
label: t("chat.conversationMenu"),
items: conversationMenuItems({
t,
- canCopy: Boolean(sessionId) && (hasMoreBefore || hasMoreAfter || messages.some(
- (message) => (message.role === "user" || message.role === "assistant") && message.content.trim(),
- )),
+ canCopy: canCopyConversation,
onCopyConversation: () => void copyConversation(),
scrollRef,
contentRef,
diff --git a/apps/desktop/src/features/chat/transcript/menu-items.tsx b/apps/desktop/src/features/chat/transcript/menu-items.tsx
index 122920cf2..6024f0efb 100644
--- a/apps/desktop/src/features/chat/transcript/menu-items.tsx
+++ b/apps/desktop/src/features/chat/transcript/menu-items.tsx
@@ -180,6 +180,30 @@ export function assistantTurnMenuItems({
return items;
}
+/** Whether the current session has dialogue available to copy. */
+export function hasConversationToCopy({
+ sessionId,
+ hasMoreBefore,
+ hasMoreAfter,
+ messages,
+}: {
+ sessionId: string | undefined;
+ hasMoreBefore: boolean;
+ hasMoreAfter: boolean;
+ messages: readonly { role: string; content: string }[];
+}): boolean {
+ return (
+ Boolean(sessionId) &&
+ (hasMoreBefore ||
+ hasMoreAfter ||
+ messages.some(
+ (message) =>
+ (message.role === "user" || message.role === "assistant") &&
+ message.content.trim().length > 0,
+ ))
+ );
+}
+
/**
* The conversation background: the actions that need the whole thread, which no
* single row can offer.
diff --git a/apps/desktop/test/chat-context-menu-items.test.mjs b/apps/desktop/test/chat-context-menu-items.test.mjs
index bf7267418..063fa58f1 100644
--- a/apps/desktop/test/chat-context-menu-items.test.mjs
+++ b/apps/desktop/test/chat-context-menu-items.test.mjs
@@ -38,6 +38,7 @@ const {
userMessageMenuItems,
assistantTurnMenuItems,
conversationMenuItems,
+ hasConversationToCopy,
} = loadMenuItems();
const ids = (items) => items.map((item) => item.id);
@@ -202,3 +203,25 @@ test("copy conversation ignores a live selection", () => {
items.find((item) => item.id === "copy-conversation").onSelect("this line");
assert.deepEqual(copied, [{ text: "You:\nHi", selection: undefined }]);
});
+test("conversation copy requires a session and dialogue or unloaded history", () => {
+ const empty = {
+ sessionId: "session-1",
+ hasMoreBefore: false,
+ hasMoreAfter: false,
+ messages: [],
+ };
+ assert.equal(hasConversationToCopy(empty), false);
+ assert.equal(
+ hasConversationToCopy({ ...empty, sessionId: undefined, messages: [{ role: "user", content: "hello" }] }),
+ false,
+ );
+ assert.equal(
+ hasConversationToCopy({ ...empty, messages: [{ role: "tool", content: "output" }] }),
+ false,
+ );
+ assert.equal(
+ hasConversationToCopy({ ...empty, messages: [{ role: "user", content: " hello " }] }),
+ true,
+ );
+ assert.equal(hasConversationToCopy({ ...empty, hasMoreBefore: true }), true);
+});
diff --git a/scripts/e2e/radial-menu.tsx b/scripts/e2e/radial-menu.tsx
index 930d962c8..3bae48f7e 100644
--- a/scripts/e2e/radial-menu.tsx
+++ b/scripts/e2e/radial-menu.tsx
@@ -24,8 +24,8 @@ type ProbeState = {
const state: ProbeState = { calls: [], closes: 0, disabledCalls: [] };
-const items = (disabledLeft = false): RadialMenuSlot[] => [
- { id: "up", label: "Copy conversation", icon: <span>U</span>, onSelect: () => state.calls.push("up") },
+const items = (disabledLeft = false, disabledUp = false): RadialMenuSlot[] => [
+ { id: "up", label: "Copy conversation", icon: <span>U</span>, disabled: disabledUp, onSelect: () => state.calls.push("up") },
{ id: "right", label: "Select text", icon: <span>R</span>, onSelect: () => state.calls.push("right") },
{ id: "down", label: "Jump to latest", icon: <span>D</span>, onSelect: () => state.calls.push("down") },
{
@@ -37,12 +37,12 @@ const items = (disabledLeft = false): RadialMenuSlot[] => [
},
];
-function Probe({ disabledLeft }: { disabledLeft: boolean }) {
+function Probe({ disabledLeft, disabledUp }: { disabledLeft: boolean; disabledUp: boolean }) {
const [open, setOpen] = React.useState(true);
return open ? (
<RadialMenu
point={{ x: 420, y: 340 }}
- items={items(disabledLeft)}
+ items={items(disabledLeft, disabledUp)}
label="Transcript ring"
onClose={() => {
state.closes += 1;
@@ -54,12 +54,12 @@ function Probe({ disabledLeft }: { disabledLeft: boolean }) {
);
}
-const mount = (disabledLeft = false) => {
+const mount = (disabledLeft = false, disabledUp = false) => {
document.body.replaceChildren();
const host = document.createElement("div");
document.body.append(host);
const root = createRoot(host);
- flushSync(() => root.render(<Probe disabledLeft={disabledLeft} />));
+ flushSync(() => root.render(<Probe disabledLeft={disabledLeft} disabledUp={disabledUp} />));
const menu = document.querySelector<HTMLElement>(".radial-menu");
check(!!menu, "ring did not mount");
return menu as HTMLElement;
@@ -147,7 +147,22 @@ Object.assign(globalThis, {
await settle();
check(state.calls.at(-1) === "right", `Enter path ran ${state.calls.at(-1)}`);
- // 7. Escape closes the ring.
+ // 7. An empty first slot still leaves the keyboard path on an enabled action.
+ menu = mount(false, true);
+ await settle();
+ const firstFocusable = document.activeElement as HTMLButtonElement | null;
+ check(firstFocusable?.getAttribute("aria-label") === "Select text", `disabled first slot kept focus on ${firstFocusable?.tagName}`);
+ flushSync(() =>
+ firstFocusable?.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true })),
+ );
+ await settle();
+ const nextFocusable = document.activeElement as HTMLButtonElement | null;
+ check(nextFocusable?.getAttribute("aria-label") === "Jump to latest", `arrow key did not skip the disabled first slot: ${nextFocusable?.getAttribute("aria-label")}`);
+ flushSync(() => nextFocusable?.click());
+ await settle();
+ check(state.calls.at(-1) === "down", `keyboard action ran ${state.calls.at(-1)}`);
+
+ // 8. Escape closes the ring.
menu = mount();
flushSync(() =>
window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true })),