import { useRef, useCallback, useEffect } from "preact/hooks"; interface ClipboardProps { connected: boolean; sendClipboard: (text: string) => void; clipboardText: string; onClipboardChange: (text: string) => void; collapsed: boolean; onToggle: () => void; } export function Clipboard({ connected, sendClipboard, clipboardText, onClipboardChange, collapsed, onToggle }: ClipboardProps) { const textareaRef = useRef(null); const debounceRef = useRef | null>(null); const ignoreRef = useRef(false); // Sync external clipboard text to textarea useEffect(() => { if (textareaRef.current && !ignoreRef.current) { const ta = textareaRef.current; const start = ta.selectionStart; const end = ta.selectionEnd; ta.value = clipboardText; ta.selectionStart = start; ta.selectionEnd = end; } }, [clipboardText]); const handleInput = useCallback(() => { if (ignoreRef.current) return; if (debounceRef.current) clearTimeout(debounceRef.current); debounceRef.current = setTimeout(() => { const text = textareaRef.current?.value || ""; onClipboardChange(text); sendClipboard(text); }, 150); }, [sendClipboard, onClipboardChange]); const handleKeyDown = useCallback((e: KeyboardEvent) => { if (e.key === "Escape") { e.preventDefault(); textareaRef.current?.blur(); } }, []); return (