"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"; import { useSocket } from "@/context/SocketContext" export default function Terminals() { const { socket } = useSocket(); const { terminals, setTerminals, 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(); }; return ( <>
{terminals.map((term) => ( setActiveTerminalId(term.id)} onClose={() => closeTerminal(term.id)} selected={activeTerminalId === term.id} > Shell ))}
{socket && activeTerminal ? (
{terminals.map((term) => ( { setTerminals((prev) => prev.map((term) => term.id === activeTerminalId ? { ...term, terminal: t } : term ) ); }} visible={activeTerminalId === term.id} /> ))}
) : (
No terminals open.
)} ); }