102 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-10-21 13:57:45 -06:00
"use client"
2024-05-08 23:52:08 -07:00
2024-10-21 13:57:45 -06:00
import { Button } from "@/components/ui/button"
import Tab from "@/components/ui/tab"
import { useSocket } from "@/context/SocketContext"
2024-10-21 13:57:45 -06:00
import { useTerminal } from "@/context/TerminalContext"
import { Terminal } from "@xterm/xterm"
import { Loader2, Plus, SquareTerminal, TerminalSquare } from "lucide-react"
import { useEffect } from "react"
import { toast } from "sonner"
import EditorTerminal from "./terminal"
2024-07-23 17:30:35 -04:00
export default function Terminals() {
2024-10-21 13:57:45 -06:00
const { socket } = useSocket()
2024-07-23 17:30:35 -04:00
const {
terminals,
setTerminals,
createNewTerminal,
closeTerminal,
activeTerminalId,
setActiveTerminalId,
creatingTerminal,
2024-10-21 13:57:45 -06:00
} = useTerminal()
2024-05-08 23:52:08 -07:00
2024-10-21 13:57:45 -06:00
const activeTerminal = terminals.find((t) => t.id === activeTerminalId)
2024-05-25 20:13:31 -07:00
2024-07-23 17:30:35 -04:00
// Effect to set the active terminal when a new one is created
useEffect(() => {
if (terminals.length > 0 && !activeTerminalId) {
2024-10-21 13:57:45 -06:00
setActiveTerminalId(terminals[terminals.length - 1].id)
2024-07-23 17:30:35 -04:00
}
2024-10-21 13:57:45 -06:00
}, [terminals, activeTerminalId, setActiveTerminalId])
2024-07-23 17:30:35 -04:00
const handleCreateTerminal = () => {
if (terminals.length >= 4) {
2024-10-21 13:57:45 -06:00
toast.error("You reached the maximum # of terminals.")
return
2024-07-23 17:30:35 -04:00
}
2024-10-21 13:57:45 -06:00
createNewTerminal()
}
2024-07-23 17:30:35 -04:00
2024-05-08 23:52:08 -07:00
return (
<>
<div className="h-10 w-full overflow-auto flex gap-2 shrink-0 tab-scroll">
{terminals.map((term) => (
<Tab
key={term.id}
2024-07-23 20:17:50 -04:00
creating={creatingTerminal}
2024-05-08 23:52:08 -07:00
onClick={() => setActiveTerminalId(term.id)}
2024-07-23 20:17:50 -04:00
onClose={() => closeTerminal(term.id)}
2024-05-08 23:52:08 -07:00
selected={activeTerminalId === term.id}
>
<SquareTerminal className="w-4 h-4 mr-2" />
Shell
</Tab>
))}
<Button
disabled={creatingTerminal}
2024-07-23 17:30:35 -04:00
onClick={handleCreateTerminal}
2024-05-08 23:52:08 -07:00
size="smIcon"
variant={"secondary"}
2024-05-09 22:16:56 -07:00
className={`font-normal shrink-0 select-none text-muted-foreground disabled:opacity-50`}
2024-05-08 23:52:08 -07:00
>
{creatingTerminal ? (
<Loader2 className="animate-spin w-4 h-4" />
) : (
<Plus className="w-4 h-4" />
)}
</Button>
</div>
{socket && activeTerminal ? (
<div className="w-full relative grow h-full overflow-hidden rounded-md bg-secondary">
{terminals.map((term) => (
<EditorTerminal
key={term.id}
socket={socket}
id={term.id}
term={term.terminal}
setTerm={(t: Terminal) => {
setTerminals((prev) =>
2024-07-23 20:17:50 -04:00
prev.map((term) =>
term.id === activeTerminalId
? { ...term, terminal: t }
: term
2024-05-08 23:52:08 -07:00
)
2024-10-21 13:57:45 -06:00
)
2024-05-08 23:52:08 -07:00
}}
visible={activeTerminalId === term.id}
/>
))}
</div>
) : (
<div className="w-full h-full flex items-center justify-center text-lg font-medium text-muted-foreground/50 select-none">
<TerminalSquare className="w-4 h-4 mr-2" />
No terminals open.
</div>
)}
</>
2024-10-21 13:57:45 -06:00
)
}