Mayank2027 commited on
Commit
1da4b71
·
verified ·
1 Parent(s): 9a385c4

Update frontend/src/components/Terminal.tsx

Browse files
frontend/src/components/Terminal.tsx CHANGED
@@ -1,7 +1,35 @@
1
- import React from 'react';
2
- const Terminal: React.FC = () => (
3
- <div style={{ flex: 1, background: '#000', color: '#0f0', padding: 10 }}>
4
- <pre>$ _</pre>
5
- </div>
6
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  export default Terminal;
 
1
+ import React, { useEffect, useRef } from 'react';
2
+ import { Terminal as XTerm } from 'xterm';
3
+ import { FitAddon } from 'xterm-addon-fit';
4
+ import 'xterm/css/xterm.css';
5
+
6
+ const Terminal: React.FC = () => {
7
+ const termRef = useRef<HTMLDivElement>(null);
8
+ const xterm = useRef<XTerm>();
9
+ const ws = useRef<WebSocket>();
10
+
11
+ useEffect(() => {
12
+ const term = new XTerm({ cursorBlink: true, theme: { background: '#1e1e1e' } });
13
+ const fit = new FitAddon();
14
+ term.loadAddon(fit);
15
+ if (termRef.current) {
16
+ term.open(termRef.current);
17
+ fit.fit();
18
+ xterm.current = term;
19
+ }
20
+ // Connect WebSocket to /ws/terminal
21
+ const socket = new WebSocket(`wss://${window.location.host}/ws/terminal`);
22
+ ws.current = socket;
23
+ socket.onmessage = (event) => {
24
+ term.write(event.data);
25
+ };
26
+ term.onData(data => {
27
+ socket.send(`exec:${data}`);
28
+ });
29
+ return () => { socket.close(); term.dispose(); };
30
+ }, []);
31
+
32
+ return <div ref={termRef} style={{ height: '100%', width: '100%' }} />;
33
+ };
34
+
35
  export default Terminal;