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

Function useClipboard

cli/src/hooks/use-clipboard.ts:21–127  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19}
20
21export const useClipboard = () => {
22 const renderer = useRenderer()
23 const [statusMessage, setStatusMessage] = useState<string | null>(null)
24 const [hasSelection, setHasSelection] = useState(false)
25 const pendingCopyTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(
26 null,
27 )
28 const pendingSelectionRef = useRef<string | null>(null)
29 const lastCopiedRef = useRef<string | null>(null)
30
31 useEffect(() => {
32 return subscribeClipboardMessages(setStatusMessage)
33 }, [])
34
35 // Register the renderer globally so all copyTextToClipboard callers
36 // can use the renderer's OSC 52 method when available.
37 useEffect(() => {
38 if (renderer) {
39 registerClipboardRenderer(renderer as unknown as Record<string, unknown>)
40 return () => {
41 unregisterClipboardRenderer()
42 }
43 }
44 return undefined
45 }, [renderer])
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) {

Callers 2

ChatFunction · 0.90
LoginModalFunction · 0.90

Calls 4

onMethod · 0.65

Tested by

no test coverage detected