-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UI: Enable right click to paste in terminal #34
Conversation
@CodiumAI-Agent /review |
PR Reviewer Guide 🔍
|
@CodiumAI-Agent /ask
attachCustomKeyEventHandler also there. |
Ask❓
Answer:You are correct. Adding an event listener inside a hook without proper cleanup can lead to memory leaks or multiple instances of the same listener if the component re-renders. To address this, we should ensure that the event listener is removed when the component unmounts or when the hook is re-invoked. Here is an updated version of the import { useEffect, useRef } from 'react';
export const useTerminal = (commands: Command[] = []) => {
const terminal = useRef<any>(null);
const fitAddon = useRef<any>(null);
let commandBuffer = '';
useEffect(() => {
const handleContextMenu = (e: MouseEvent) => {
e.preventDefault();
navigator.clipboard.readText().then((text) => {
terminal.current?.write(text);
commandBuffer += text;
});
};
const terminalElement = terminal.current?.element;
if (terminalElement) {
terminalElement.addEventListener('contextmenu', handleContextMenu);
}
return () => {
if (terminalElement) {
terminalElement.removeEventListener('contextmenu', handleContextMenu);
}
};
}, [terminal]);
// Other hook logic...
return {
// Return values...
};
}; In this updated version, we define the |
All-Hands-AI#3162