From 74a43523231e9f1018a9c42f9b69fb0f2848bdf4 Mon Sep 17 00:00:00 2001 From: Akhilesh Rangani Date: Tue, 23 Jul 2024 20:17:50 -0400 Subject: [PATCH] fix: added terminal response handling --- .../components/editor/terminals/index.tsx | 23 +++++-------------- .../components/editor/terminals/terminal.tsx | 14 +++++++++++ frontend/context/TerminalContext.tsx | 2 +- 3 files changed, 21 insertions(+), 18 deletions(-) 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 ( <>