MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / handleSelection

Function handleSelection

cli/src/hooks/use-clipboard.ts:48–103  ·  view source on GitHub ↗
(selectionEvent: any)

Source from the content-addressed store, hash-verified

46
47 useEffect(() => {
48 const handleSelection = (selectionEvent: any) => {
49 const selectionObj = selectionEvent ?? (renderer as any)?.getSelection?.()
50 const rawText: string | null = selectionObj?.getSelectedText
51 ? selectionObj.getSelectedText()
52 : typeof selectionObj === 'string'
53 ? selectionObj
54 : null
55
56 // Filter out cursor character from selected text
57 const cleanedText = rawText?.replace(new RegExp(CURSOR_CHAR, 'g'), '') ?? null
58
59 if (!cleanedText || cleanedText.trim().length === 0) {
60 pendingSelectionRef.current = null
61 setHasSelection(false)
62 if (pendingCopyTimeoutRef.current) {
63 clearTimeout(pendingCopyTimeoutRef.current)
64 pendingCopyTimeoutRef.current = null
65 }
66 return
67 }
68
69 if (cleanedText === pendingSelectionRef.current) {
70 return
71 }
72
73 // Track that there's an active selection for visual feedback
74 setHasSelection(true)
75
76 pendingSelectionRef.current = cleanedText
77
78 if (pendingCopyTimeoutRef.current) {
79 clearTimeout(pendingCopyTimeoutRef.current)
80 }
81
82 pendingCopyTimeoutRef.current = setTimeout(() => {
83 pendingCopyTimeoutRef.current = null
84 const pending = pendingSelectionRef.current
85 if (!pending || pending === lastCopiedRef.current) {
86 return
87 }
88
89 lastCopiedRef.current = pending
90 const successMessage = formatDefaultClipboardMessage(pending)
91 void copyTextToClipboard(pending, {
92 successMessage,
93 durationMs: 3000,
94 })
95 .then(() => {
96 // Clear selection visual state after successful copy
97 setHasSelection(false)
98 })
99 .catch(() => {
100 // Errors are logged within copyTextToClipboard
101 })
102 }, 250)
103 }
104
105 if (renderer?.on) {

Callers

nothing calls this directly

Calls 3

copyTextToClipboardFunction · 0.90
setTimeoutFunction · 0.85

Tested by

no test coverage detected