Update terminal-panel.tsx

This commit is contained in:
MacRimi
2025-11-21 19:15:35 +01:00
parent cff2c12d70
commit e26956dbe8

View File

@@ -2,6 +2,7 @@
import type React from "react" import type React from "react"
import { useEffect, useRef } from "react" import { useEffect, useRef } from "react"
import { API_PORT } from "@/lib/api-config"
let Terminal: any let Terminal: any
let FitAddon: any let FitAddon: any
@@ -16,7 +17,27 @@ type TerminalPanelProps = {
websocketUrl?: string // Custom WebSocket URL if needed websocketUrl?: string // Custom WebSocket URL if needed
} }
export const TerminalPanel: React.FC<TerminalPanelProps> = ({ websocketUrl = "ws://localhost:8008/ws/terminal" }) => { function getWebSocketUrl(): string {
if (typeof window === "undefined") {
return "ws://localhost:8008/ws/terminal"
}
const { protocol, hostname, port } = window.location
const isStandardPort = port === "" || port === "80" || port === "443"
// Use wss:// for https, ws:// for http
const wsProtocol = protocol === "https:" ? "wss:" : "ws:"
if (isStandardPort) {
// Behind proxy - use current host
return `${wsProtocol}//${hostname}/ws/terminal`
} else {
// Direct access - use API port
return `${wsProtocol}//${hostname}:${API_PORT}/ws/terminal`
}
}
export const TerminalPanel: React.FC<TerminalPanelProps> = ({ websocketUrl }) => {
const containerRef = useRef<HTMLDivElement | null>(null) const containerRef = useRef<HTMLDivElement | null>(null)
const termRef = useRef<any>(null) const termRef = useRef<any>(null)
const fitAddonRef = useRef<any>(null) const fitAddonRef = useRef<any>(null)
@@ -28,6 +49,8 @@ export const TerminalPanel: React.FC<TerminalPanelProps> = ({ websocketUrl = "ws
useEffect(() => { useEffect(() => {
if (!containerRef.current || !Terminal || !FitAddon) return if (!containerRef.current || !Terminal || !FitAddon) return
console.log("[v0] TerminalPanel: Initializing terminal")
const term = new Terminal({ const term = new Terminal({
fontFamily: "monospace", fontFamily: "monospace",
fontSize: 13, fontSize: 13,
@@ -45,33 +68,34 @@ export const TerminalPanel: React.FC<TerminalPanelProps> = ({ websocketUrl = "ws
termRef.current = term termRef.current = term
fitAddonRef.current = fitAddon fitAddonRef.current = fitAddon
// WebSocket connection to backend (Flask) const wsUrl = websocketUrl || getWebSocketUrl()
const ws = new WebSocket(websocketUrl) console.log("[v0] TerminalPanel: Connecting to WebSocket:", wsUrl)
const ws = new WebSocket(wsUrl)
wsRef.current = ws wsRef.current = ws
ws.onopen = () => { ws.onopen = () => {
console.log("[v0] TerminalPanel: WebSocket connected")
term.writeln("\x1b[32mConnected to ProxMenux terminal.\x1b[0m") term.writeln("\x1b[32mConnected to ProxMenux terminal.\x1b[0m")
// Optional: notify backend to start shell
// ws.send(JSON.stringify({ type: 'start', shell: 'bash' }));
} }
ws.onmessage = (event) => { ws.onmessage = (event) => {
// Backend sends plain text (bash output)
term.write(event.data) term.write(event.data)
} }
ws.onerror = () => { ws.onerror = (error) => {
console.error("[v0] TerminalPanel: WebSocket error:", error)
term.writeln("\r\n\x1b[31m[ERROR] WebSocket connection error\x1b[0m") term.writeln("\r\n\x1b[31m[ERROR] WebSocket connection error\x1b[0m")
} }
ws.onclose = () => { ws.onclose = () => {
console.log("[v0] TerminalPanel: WebSocket closed")
term.writeln("\r\n\x1b[33m[INFO] Connection closed\x1b[0m") term.writeln("\r\n\x1b[33m[INFO] Connection closed\x1b[0m")
} }
// Send user input to backend // Send user input to backend
term.onData((data) => { term.onData((data) => {
if (ws.readyState === WebSocket.OPEN) { if (ws.readyState === WebSocket.OPEN) {
// Send raw data or JSON depending on backend implementation
ws.send(data) ws.send(data)
} }
}) })
@@ -87,13 +111,13 @@ export const TerminalPanel: React.FC<TerminalPanelProps> = ({ websocketUrl = "ws
window.addEventListener("resize", handleResize) window.addEventListener("resize", handleResize)
return () => { return () => {
console.log("[v0] TerminalPanel: Cleaning up")
window.removeEventListener("resize", handleResize) window.removeEventListener("resize", handleResize)
ws.close() ws.close()
term.dispose() term.dispose()
} }
}, [websocketUrl]) }, [websocketUrl])
// --- Helpers for special key sequences ---
const sendSequence = (seq: string) => { const sendSequence = (seq: string) => {
const term = termRef.current const term = termRef.current
const ws = wsRef.current const ws = wsRef.current
@@ -132,7 +156,6 @@ export const TerminalPanel: React.FC<TerminalPanelProps> = ({ websocketUrl = "ws
} }
} }
// --- Touch gestures for arrow keys ---
const handleTouchStart = (e: React.TouchEvent<HTMLDivElement>) => { const handleTouchStart = (e: React.TouchEvent<HTMLDivElement>) => {
const touch = e.touches[0] const touch = e.touches[0]
touchStartRef.current = { touchStartRef.current = {