191 lines
4.7 KiB
TypeScript
Raw Normal View History

"use client"
import { FitAddon } from "@xterm/addon-fit"
2024-10-21 13:57:17 -06:00
import { Terminal } from "@xterm/xterm"
import "./xterm.css"
import { debounce } from "@/lib/utils"
2024-10-21 13:57:17 -06:00
import { Loader2 } from "lucide-react"
2024-10-23 11:55:38 +01:00
import { useTheme } from "next-themes"
2024-10-21 13:57:17 -06:00
import { ElementRef, useEffect, useRef } from "react"
import { Socket } from "socket.io-client"
export default function EditorTerminal({
socket,
2024-05-06 23:34:45 -07:00
id,
term,
setTerm,
2024-05-08 01:09:01 -07:00
visible,
}: {
socket: Socket
id: string
term: Terminal | null
setTerm: (term: Terminal) => void
visible: boolean
}) {
2024-10-23 11:55:38 +01:00
const { theme } = useTheme()
const terminalContainerRef = useRef<ElementRef<"div">>(null)
const fitAddonRef = useRef<FitAddon | null>(null)
2024-04-28 20:06:47 -04:00
useEffect(() => {
2024-10-23 11:55:38 +01:00
if (!terminalContainerRef.current) return
2024-05-08 01:09:01 -07:00
// console.log("new terminal", id, term ? "reusing" : "creating");
2024-04-28 20:06:47 -04:00
const terminal = new Terminal({
2024-04-29 21:36:33 -04:00
cursorBlink: true,
2024-10-23 11:55:38 +01:00
theme: theme === "light" ? lightTheme : darkTheme,
2024-05-02 15:25:24 -07:00
fontFamily: "var(--font-geist-mono)",
fontSize: 14,
2024-05-08 01:04:03 -07:00
lineHeight: 1.5,
letterSpacing: 0,
})
2024-04-28 20:06:47 -04:00
setTerm(terminal)
const dispose = () => {
terminal.dispose()
}
return dispose
}, [])
2024-04-28 20:06:47 -04:00
2024-10-23 11:55:38 +01:00
useEffect(() => {
if (term) {
term.options.theme = theme === "light" ? lightTheme : darkTheme
}
}, [theme])
2024-04-28 20:06:47 -04:00
useEffect(() => {
if (!term) return
2024-04-28 20:06:47 -04:00
2024-10-23 11:55:38 +01:00
if (!terminalContainerRef.current) return
if (!fitAddonRef.current) {
const fitAddon = new FitAddon()
term.loadAddon(fitAddon)
2024-10-23 11:55:38 +01:00
term.open(terminalContainerRef.current)
fitAddon.fit()
fitAddonRef.current = fitAddon
}
2024-05-09 22:16:56 -07:00
const disposableOnData = term.onData((data) => {
socket.emit("terminalData", { id, data })
})
2024-04-28 20:06:47 -04:00
2024-05-09 22:16:56 -07:00
const disposableOnResize = term.onResize((dimensions) => {
fitAddonRef.current?.fit()
socket.emit("terminalResize", { dimensions })
})
const resizeObserver = new ResizeObserver(
debounce((entries) => {
2024-10-23 11:55:38 +01:00
if (!fitAddonRef.current || !terminalContainerRef.current) return
const entry = entries[0]
if (!entry) return
2024-05-09 22:16:56 -07:00
const { width, height } = entry.contentRect
// Only call fit if the size has actually changed
if (
2024-10-23 11:55:38 +01:00
width !== terminalContainerRef.current.offsetWidth ||
height !== terminalContainerRef.current.offsetHeight
) {
try {
fitAddonRef.current.fit()
} catch (err) {
console.error("Error during fit:", err)
}
}
}, 50) // Debounce for 50ms
)
// start observing for resize
2024-10-23 11:55:38 +01:00
resizeObserver.observe(terminalContainerRef.current)
2024-04-28 20:06:47 -04:00
return () => {
disposableOnData.dispose()
disposableOnResize.dispose()
resizeObserver.disconnect()
}
2024-10-23 11:55:38 +01:00
}, [term, terminalContainerRef.current])
2024-04-28 20:06:47 -04:00
2024-07-23 20:17:50 -04:00
useEffect(() => {
if (!term) return
2024-07-23 20:17:50 -04:00
const handleTerminalResponse = (response: { id: string; data: string }) => {
if (response.id === id) {
term.write(response.data)
2024-07-23 20:17:50 -04:00
}
}
socket.on("terminalResponse", handleTerminalResponse)
2024-07-23 20:17:50 -04:00
return () => {
socket.off("terminalResponse", handleTerminalResponse)
}
}, [term, id, socket])
2024-07-23 20:17:50 -04:00
2024-04-30 22:48:36 -04:00
return (
2024-05-06 23:34:45 -07:00
<>
2024-05-08 01:09:01 -07:00
<div
2024-10-23 11:55:38 +01:00
ref={terminalContainerRef}
2024-05-08 01:09:01 -07:00
style={{ display: visible ? "block" : "none" }}
className="w-full h-full text-left"
>
2024-05-06 23:34:45 -07:00
{term === null ? (
<div className="flex items-center text-muted-foreground p-2">
<Loader2 className="animate-spin mr-2 h-4 w-4" />
<span>Connecting to terminal...</span>
</div>
) : null}
</div>
</>
)
2024-04-28 20:06:47 -04:00
}
2024-10-23 11:55:38 +01:00
const lightTheme = {
foreground: "#2e3436",
background: "#ffffff",
black: "#2e3436",
brightBlack: "#555753",
red: "#cc0000",
brightRed: "#ef2929",
green: "#4e9a06",
brightGreen: "#8ae234",
yellow: "#c4a000",
brightYellow: "#fce94f",
blue: "#3465a4",
brightBlue: "#729fcf",
magenta: "#75507b",
brightMagenta: "#ad7fa8",
cyan: "#06989a",
brightCyan: "#34e2e2",
white: "#d3d7cf",
brightWhite: "#eeeeec",
cursor: "#2e3436",
cursorAccent: "#ffffff",
selectionBackground: "#3465a4",
selectionForeground: "#ffffff",
selectionInactiveBackground: "#264973",
}
// Dark Theme
const darkTheme = {
foreground: "#f8f8f2",
background: "#0a0a0a",
black: "#21222c",
brightBlack: "#6272a4",
red: "#ff5555",
brightRed: "#ff6e6e",
green: "#50fa7b",
brightGreen: "#69ff94",
yellow: "#f1fa8c",
brightYellow: "#ffffa5",
blue: "#bd93f9",
brightBlue: "#d6acff",
magenta: "#ff79c6",
brightMagenta: "#ff92df",
cyan: "#8be9fd",
brightCyan: "#a4ffff",
white: "#f8f8f2",
brightWhite: "#ffffff",
cursor: "#f8f8f2",
cursorAccent: "#0a0a0a",
selectionBackground: "#264973",
selectionForeground: "#ffffff",
selectionInactiveBackground: "#1a3151",
}