@@ -31,23 +31,16 @@ export const useTerminal = (
3131 const { sendJsonMessage, message, isReady } = useWebSocket ( ) ;
3232 const [ terminalInstance , setTerminalInstance ] = useState < any | null > ( null ) ;
3333 const resizeTimeoutRef = useRef < NodeJS . Timeout | undefined > ( undefined ) ;
34- const terminalInstanceRef = useRef < any | null > ( null ) ;
3534
3635 const destroyTerminal = useCallback ( ( ) => {
37- const instance = terminalInstanceRef . current ;
38- if ( instance ) {
39- instance . dispose ( ) ;
40- terminalInstanceRef . current = null ;
36+ if ( terminalInstance ) {
37+ terminalInstance . dispose ( ) ;
4138 setTerminalInstance ( null ) ;
4239 }
43- // Clear the terminal container to remove any stale input
44- if ( terminalRef . current ) {
45- terminalRef . current . innerHTML = '' ;
46- }
4740 if ( resizeTimeoutRef . current ) {
4841 clearTimeout ( resizeTimeoutRef . current ) ;
4942 }
50- } , [ ] ) ;
43+ } , [ terminalInstance , terminalId ] ) ;
5144
5245 useEffect ( ( ) => {
5346 if ( isStopped && terminalInstance ) {
@@ -77,8 +70,6 @@ export const useTerminal = (
7770 if ( parsedMessage . type === OutputType . EXIT ) {
7871 destroyTerminal ( ) ;
7972 } else if ( parsedMessage . data ) {
80- // Write output from backend - this includes echoed input
81- // Using write ensures proper synchronization with terminal state
8273 terminalInstance . write ( parsedMessage . data ) ;
8374 }
8475 }
@@ -87,19 +78,6 @@ export const useTerminal = (
8778 }
8879 } , [ message , terminalInstance , destroyTerminal , terminalId ] ) ;
8980
90- // Cleanup effect: destroy terminal when isTerminalOpen becomes false or component unmounts
91- useEffect ( ( ) => {
92- if ( ! isTerminalOpen && terminalInstanceRef . current ) {
93- destroyTerminal ( ) ;
94- }
95- return ( ) => {
96- // Cleanup on unmount
97- if ( terminalInstanceRef . current ) {
98- destroyTerminal ( ) ;
99- }
100- } ;
101- } , [ isTerminalOpen , destroyTerminal ] ) ;
102-
10381 const initializeTerminal = useCallback ( async ( ) => {
10482 if ( ! terminalRef . current || terminalInstance || ! isReady ) return ;
10583
@@ -180,35 +158,26 @@ export const useTerminal = (
180158 if ( allowInput ) {
181159 term . attachCustomKeyEventHandler ( ( event : KeyboardEvent ) => {
182160 const key = event . key . toLowerCase ( ) ;
183-
184- // Handle Ctrl+J or Cmd+J (toggle terminal shortcut)
185161 if ( key === 'j' && ( event . ctrlKey || event . metaKey ) ) {
186162 return false ;
187- }
188-
189- // Handle Ctrl+C or Cmd+C for copy (when there's a selection)
190- if ( key === 'c' && ( event . ctrlKey || event . metaKey ) && ! event . shiftKey ) {
163+ } else if ( key === 'c' && ( event . ctrlKey || event . metaKey ) && ! event . shiftKey ) {
191164 if ( event . type === 'keydown' ) {
192165 try {
193166 const selection = term . getSelection ( ) ;
194167 if ( selection ) {
195168 navigator . clipboard . writeText ( selection ) . then ( ( ) => {
196- term . clearSelection ( ) ;
169+ term . clearSelection ( ) ; // Clear selection after successful copy
197170 } ) ;
198171 return false ;
199172 }
200173 } catch ( error ) {
201174 console . error ( 'Error in Ctrl+C handler:' , error ) ;
202175 }
203176 }
177+ return false ;
204178 }
205-
206- // Allow xterm to process all other keys normally
207179 return true ;
208180 } ) ;
209-
210- // onData is called when xterm processes input
211- // Send all input to backend - backend echo will handle display
212181 term . onData ( ( data ) => {
213182 sendJsonMessage ( {
214183 action : 'terminal' ,
@@ -229,7 +198,6 @@ export const useTerminal = (
229198 } ) ;
230199 }
231200
232- terminalInstanceRef . current = term ;
233201 setTerminalInstance ( term ) ;
234202 } catch ( error ) {
235203 console . error ( 'Error initializing terminal:' , error ) ;
0 commit comments