"use client"; import { Button } from "@/components/ui/button"; import Tab from "@/components/ui/tab"; import { Terminal } from "@xterm/xterm"; import { Loader2, Plus, SquareTerminal, TerminalSquare } from "lucide-react"; import { toast } from "sonner"; import EditorTerminal from "./terminal"; import { useTerminal } from "@/context/TerminalContext"; import { useEffect } from "react"; export default function Terminals() { const { terminals, setTerminals, socket, createNewTerminal, closeTerminal, activeTerminalId, setActiveTerminalId, creatingTerminal, } = useTerminal(); const activeTerminal = terminals.find((t) => t.id === activeTerminalId); // Effect to set the active terminal when a new one is created useEffect(() => { if (terminals.length > 0 && !activeTerminalId) { setActiveTerminalId(terminals[terminals.length - 1].id); } }, [terminals, activeTerminalId, setActiveTerminalId]); const handleCreateTerminal = () => { if (terminals.length >= 4) { toast.error("You reached the maximum # of terminals."); return; } 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 ( <>