diff --git a/frontend/components/editor/terminals/index.tsx b/frontend/components/editor/terminals/index.tsx
index 3381b33..076f121 100644
--- a/frontend/components/editor/terminals/index.tsx
+++ b/frontend/components/editor/terminals/index.tsx
@@ -38,26 +38,15 @@ export default function Terminals() {
createNewTerminal();
};
- const handleCloseTerminal = (termId: string) => {
- closeTerminal(termId);
- if (activeTerminalId === termId) {
- const remainingTerminals = terminals.filter(t => t.id !== termId);
- if (remainingTerminals.length > 0) {
- setActiveTerminalId(remainingTerminals[0].id);
- } else {
- setActiveTerminalId("");
- }
- }
- };
-
return (
<>
{terminals.map((term) => (
setActiveTerminalId(term.id)}
- onClose={() => handleCloseTerminal(term.id)}
+ onClose={() => closeTerminal(term.id)}
selected={activeTerminalId === term.id}
>
@@ -88,10 +77,10 @@ export default function Terminals() {
term={term.terminal}
setTerm={(t: Terminal) => {
setTerminals((prev) =>
- prev.map((prevTerm) =>
- prevTerm.id === term.id
- ? { ...prevTerm, terminal: t }
- : prevTerm
+ prev.map((term) =>
+ term.id === activeTerminalId
+ ? { ...term, terminal: t }
+ : term
)
);
}}
diff --git a/frontend/components/editor/terminals/terminal.tsx b/frontend/components/editor/terminals/terminal.tsx
index 3acbe23..3399159 100644
--- a/frontend/components/editor/terminals/terminal.tsx
+++ b/frontend/components/editor/terminals/terminal.tsx
@@ -74,6 +74,20 @@ export default function EditorTerminal({
};
}, [term, terminalRef.current]);
+ useEffect(() => {
+ if (!term) return;
+ const handleTerminalResponse = (response: { id: string; data: string }) => {
+ if (response.id === id) {
+ term.write(response.data);
+ }
+ };
+ socket.on("terminalResponse", handleTerminalResponse);
+
+ return () => {
+ socket.off("terminalResponse", handleTerminalResponse);
+ };
+ }, [term, id, socket]);
+
return (
<>