From 8ae166fef418997549ff7d4acaba64c3112b358e Mon Sep 17 00:00:00 2001 From: Akhileshrangani4 Date: Sun, 22 Sep 2024 00:23:12 -0400 Subject: [PATCH] fix: close the terminal opened with run button --- frontend/components/editor/navbar/run.tsx | 85 ++++++++++++----------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/frontend/components/editor/navbar/run.tsx b/frontend/components/editor/navbar/run.tsx index 82f69c9..26e8bbd 100644 --- a/frontend/components/editor/navbar/run.tsx +++ b/frontend/components/editor/navbar/run.tsx @@ -1,5 +1,6 @@ "use client"; +import React, { useEffect, useRef } from 'react'; import { Play, StopCircle } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useTerminal } from "@/context/TerminalContext"; @@ -16,53 +17,57 @@ export default function RunButtonModal({ setIsRunning: (running: boolean) => void; sandboxData: Sandbox; }) { - const { createNewTerminal, terminals, closeTerminal } = useTerminal(); + const { createNewTerminal, closeTerminal, terminals } = useTerminal(); const { setIsPreviewCollapsed, previewPanelRef } = usePreview(); + // Ref to keep track of the last created terminal's ID + const lastCreatedTerminalRef = useRef(null); - const handleRun = () => { - if (isRunning) { - console.log('Stopping sandbox...'); - console.log('Closing Preview Window'); - - terminals.forEach(term => { - if (term.terminal) { - closeTerminal(term.id); - console.log('Closing Terminal', term.id); - } - }); - - setIsPreviewCollapsed(true); - previewPanelRef.current?.collapse(); - } else { - console.log('Running sandbox...'); - console.log('Opening Terminal'); - console.log('Opening Preview Window'); - - if (terminals.length < 4) { - if (sandboxData.type === "streamlit") { - createNewTerminal( - "pip install -r requirements.txt && streamlit run main.py --server.runOnSave true" - ); - } else { - createNewTerminal("yarn install && yarn dev"); - } - } else { - toast.error("You reached the maximum # of terminals."); - console.error("Maximum number of terminals reached."); + // Effect to update the lastCreatedTerminalRef when a new terminal is added + useEffect(() => { + if (terminals.length > 0 && !isRunning) { + const latestTerminal = terminals[terminals.length - 1]; + if (latestTerminal && latestTerminal.id !== lastCreatedTerminalRef.current) { + lastCreatedTerminalRef.current = latestTerminal.id; } - - setIsPreviewCollapsed(false); - previewPanelRef.current?.expand(); } + }, [terminals, isRunning]); + + const handleRun = async () => { + if (isRunning && lastCreatedTerminalRef.current) + { + await closeTerminal(lastCreatedTerminalRef.current); + lastCreatedTerminalRef.current = null; + setIsPreviewCollapsed(true); + previewPanelRef.current?.collapse(); + } + else if (!isRunning && terminals.length < 4) + { + const command = sandboxData.type === "streamlit" + ? "pip install -r requirements.txt && streamlit run main.py --server.runOnSave true" + : "yarn install && yarn dev"; + + try { + // Create a new terminal with the appropriate command + await createNewTerminal(command); + setIsPreviewCollapsed(false); + previewPanelRef.current?.expand(); + } catch (error) { + toast.error("Failed to create new terminal."); + console.error("Error creating new terminal:", error); + return; + } + } else if (!isRunning) { + toast.error("You've reached the maximum number of terminals."); + return; + } + setIsRunning(!isRunning); }; return ( - <> - - + ); } \ No newline at end of file