MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / useCopyOnSelect

Function useCopyOnSelect

source code/hooks/useCopyOnSelect.ts:28–85  ·  view source on GitHub ↗
(
  selection: Selection,
  isActive: boolean,
  onCopied?: (text: string) => void,
)

Source from the content-addressed store, hash-verified

26 * fullscreen REPL passes showCopiedToast for user feedback.
27 */
28export function useCopyOnSelect(
29 selection: Selection,
30 isActive: boolean,
31 onCopied?: (text: string) => void,
32): void {
33 // Tracks whether the *previous* notification had a visible selection with
34 // isDragging=false (i.e., we already auto-copied it). Without this, the
35 // finish→clear transition would look like a fresh selection-gone-idle
36 // event and we'd toast twice for a single drag.
37 const copiedRef = useRef(false)
38 // onCopied is a fresh closure each render; read through a ref so the
39 // effect doesn't re-subscribe (which would reset copiedRef via unmount).
40 const onCopiedRef = useRef(onCopied)
41 onCopiedRef.current = onCopied
42
43 useEffect(() => {
44 if (!isActive) return
45
46 const unsubscribe = selection.subscribe(() => {
47 const sel = selection.getState()
48 const has = selection.hasSelection()
49 // Drag in progress — wait for finish. Reset copied flag so a new drag
50 // that ends on the same range still triggers a fresh copy.
51 if (sel?.isDragging) {
52 copiedRef.current = false
53 return
54 }
55 // No selection (cleared, or click-without-drag) — reset.
56 if (!has) {
57 copiedRef.current = false
58 return
59 }
60 // Selection settled (drag finished OR multi-click). Already copied
61 // this one — the only way to get here again without going through
62 // isDragging or !has is a spurious notify (shouldn't happen, but safe).
63 if (copiedRef.current) return
64
65 // Default true: macOS users expect cmd+c to work. It can't — the
66 // terminal's Edit > Copy intercepts it before the pty sees it, and
67 // finds no native selection (mouse tracking disabled it). Auto-copy
68 // on mouse-up makes cmd+c a no-op that leaves the clipboard intact
69 // with the right content, so paste works as expected.
70 const enabled = getGlobalConfig().copyOnSelect ?? true
71 if (!enabled) return
72
73 const text = selection.copySelectionNoClear()
74 // Whitespace-only (e.g., blank-line multi-click) — not worth a
75 // clipboard write or toast. Still set copiedRef so we don't retry.
76 if (!text || !text.trim()) {
77 copiedRef.current = true
78 return
79 }
80 copiedRef.current = true
81 onCopiedRef.current?.(text)
82 })
83 return unsubscribe
84 }, [isActive, selection])
85}

Callers 1

ScrollKeybindingHandlerFunction · 0.85

Calls 2

getGlobalConfigFunction · 0.85
copySelectionNoClearMethod · 0.80

Tested by

no test coverage detected