The best way to control Claude Code, Codex and more from any computer or phone
Add terminal selection copy action (#2904)
Taras committed
Jul 18, 2026 at 22:05 UTC
1735e27d9e5106bbb35d5b1dd10363604a54b69e
1 file changed
+43
-14
apps/web/src/components/ThreadTerminalDrawer.tsx
+43
-14
@@ -31,6 +31,7 @@ import {
31
useState,
32
} from "react";
33
import { Popover, PopoverPopup, PopoverTrigger } from "~/components/ui/popover";
34
+import { writeTextToClipboard } from "~/hooks/useCopyToClipboard";
35
import { cn } from "~/lib/utils";
36
import { type TerminalContextSelection } from "~/lib/terminalContext";
37
import { useOpenInPreferredEditor } from "../editorPreferences";
@@ -330,7 +331,7 @@ export function TerminalViewport({
331
const selectionPointerRef = useRef<{ x: number; y: number } | null>(null);
332
const selectionGestureActiveRef = useRef(false);
333
const selectionActionRequestIdRef = useRef(0);
333
- const selectionActionOpenRef = useRef(false);
334
+ const selectionActionMenuOpenRef = useRef(false);
335
const selectionActionTimerRef = useRef<number | null>(null);
336
const keybindingsRef = useRef(keybindings);
337
const runtimeEnvKey = useMemo(() => runtimeEnvSignature(runtimeEnv), [runtimeEnv]);
@@ -417,6 +418,7 @@ export function TerminalViewport({
418
419
const readSelectionAction = (): {
420
position: { x: number; y: number };
421
+ clipboardText: string;
422
selection: TerminalContextSelection;
423
} | null => {
424
const activeTerminal = terminalRef.current;
@@ -445,6 +447,7 @@ export function TerminalViewport({
447
});
448
return {
449
position,
450
+ clipboardText: selectionText,
451
selection: {
452
terminalId,
453
terminalLabel: readTerminalLabel(),
@@ -460,7 +463,7 @@ export function TerminalViewport({
463
clearSelectionAction();
464
return;
465
}
463
- if (selectionActionOpenRef.current) {
466
+ if (selectionActionMenuOpenRef.current) {
467
return;
468
}
469
const nextAction = readSelectionAction();
@@ -469,20 +472,46 @@ export function TerminalViewport({
472
return;
473
}
474
const requestId = ++selectionActionRequestIdRef.current;
472
- selectionActionOpenRef.current = true;
473
- try {
474
- const clicked = await localApi.contextMenu.show(
475
- [{ id: "add-to-chat", label: "Add to chat" }],
475
+ selectionActionMenuOpenRef.current = true;
476
+ const clicked = await localApi.contextMenu
477
+ .show(
478
+ [
479
+ { id: "add-to-chat", label: "Add to chat" },
480
+ { id: "copy", label: "Copy" },
481
+ ],
482
nextAction.position,
477
- );
478
- if (requestId !== selectionActionRequestIdRef.current || clicked !== "add-to-chat") {
483
+ )
484
+ .finally(() => {
485
+ selectionActionMenuOpenRef.current = false;
486
+ });
487
+ if (requestId !== selectionActionRequestIdRef.current || clicked === null) {
488
+ return;
489
+ }
490
+ switch (clicked) {
491
+ case "add-to-chat":
492
+ handleAddTerminalContext(nextAction.selection);
493
+ terminalRef.current?.clearSelection();
494
+ terminalRef.current?.focus();
495
+ return;
496
+ case "copy":
497
+ try {
498
+ await writeTextToClipboard(nextAction.clipboardText, "terminal selection");
499
+ } catch (error) {
500
+ if (requestId !== selectionActionRequestIdRef.current) {
501
+ return;
502
+ }
503
+ const activeTerminal = terminalRef.current;
504
+ if (activeTerminal) {
505
+ writeSystemMessage(
506
+ activeTerminal,
507
+ error instanceof Error ? error.message : "Unable to copy terminal selection",
508
+ );
509
+ }
510
+ }
511
+ if (requestId === selectionActionRequestIdRef.current) {
512
+ terminalRef.current?.focus();
513
+ }
514
return;
480
- }
481
- handleAddTerminalContext(nextAction.selection);
482
- terminalRef.current?.clearSelection();
483
- terminalRef.current?.focus();
484
- } finally {
485
- selectionActionOpenRef.current = false;
515
}
516
};
517