Files
ProxMenux/AppImage/components/script-terminal-modal.tsx

795 lines
26 KiB
TypeScript
Raw Normal View History

2025-12-01 01:04:31 +01:00
"use client"
2025-12-06 18:36:34 +01:00
import type React from "react"
2025-12-06 23:06:18 +01:00
import { useState, useEffect, useRef, useCallback } from "react"
2025-12-10 17:08:41 +01:00
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog"
2025-12-01 01:04:31 +01:00
import { Button } from "@/components/ui/button"
2025-12-06 11:25:27 +01:00
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
2025-12-10 23:48:43 +01:00
import {
Loader2,
Activity,
ArrowUp,
ArrowDown,
ArrowLeft,
ArrowRight,
CornerDownLeft,
GripHorizontal,
} from "lucide-react"
2025-12-10 21:47:52 +01:00
import "xterm/css/xterm.css"
import { API_PORT } from "@/lib/api-config"
2025-12-01 01:04:31 +01:00
2025-12-06 11:25:27 +01:00
interface WebInteraction {
2025-12-06 12:25:57 +01:00
type: "yesno" | "menu" | "msgbox" | "input" | "inputbox"
2025-12-06 11:25:27 +01:00
id: string
title: string
message: string
options?: Array<{ label: string; value: string }>
2025-12-06 12:25:57 +01:00
default?: string
2025-12-06 11:25:27 +01:00
}
2025-12-01 01:04:31 +01:00
interface ScriptTerminalModalProps {
open: boolean
onClose: () => void
scriptPath: string
2025-12-10 20:17:13 +01:00
scriptName: string
2025-12-10 21:47:52 +01:00
scriptDescription?: string
2025-12-10 20:17:13 +01:00
title: string
description: string
2025-12-01 01:04:31 +01:00
}
2025-12-10 17:08:41 +01:00
export function ScriptTerminalModal({
open: isOpen,
2025-12-01 01:04:31 +01:00
onClose,
scriptPath,
2025-12-10 20:17:13 +01:00
scriptName,
2025-12-10 21:47:52 +01:00
scriptDescription,
2025-12-10 20:17:13 +01:00
title,
description,
2025-12-01 01:04:31 +01:00
}: ScriptTerminalModalProps) {
2025-12-10 17:08:41 +01:00
const termRef = useRef<any>(null)
2025-12-06 23:06:18 +01:00
const wsRef = useRef<WebSocket | null>(null)
2025-12-10 17:08:41 +01:00
const fitAddonRef = useRef<any>(null)
2025-12-06 23:06:18 +01:00
const sessionIdRef = useRef<string>(Math.random().toString(36).substring(2, 8))
2025-12-10 21:47:52 +01:00
const [connectionStatus, setConnectionStatus] = useState<"connecting" | "online" | "offline">("connecting")
2025-12-10 17:08:41 +01:00
const [isComplete, setIsComplete] = useState(false)
const [exitCode, setExitCode] = useState<number | null>(null)
2025-12-06 11:25:27 +01:00
const [currentInteraction, setCurrentInteraction] = useState<WebInteraction | null>(null)
2025-12-10 17:08:41 +01:00
const [interactionInput, setInteractionInput] = useState("")
const checkConnectionInterval = useRef<NodeJS.Timeout | null>(null)
2025-12-10 18:54:35 +01:00
const [isMobile, setIsMobile] = useState(false)
const [isTablet, setIsTablet] = useState(false)
2025-12-10 17:08:41 +01:00
2025-12-06 23:06:18 +01:00
const [isWaitingNextInteraction, setIsWaitingNextInteraction] = useState(false)
2025-12-10 17:08:41 +01:00
const waitingTimeoutRef = useRef<NodeJS.Timeout | null>(null)
2025-12-06 23:06:18 +01:00
2025-12-10 23:43:05 +01:00
const [modalHeight, setModalHeight] = useState(600)
2025-12-06 18:36:34 +01:00
const [isResizing, setIsResizing] = useState(false)
2025-12-11 17:30:12 +01:00
const resizeBarRef = useRef<HTMLDivElement>(null)
2025-12-11 18:27:11 +01:00
const modalHeightRef = useRef(600) // Ref para mantener el valor actualizado
2025-12-10 18:36:31 +01:00
2025-12-11 18:19:38 +01:00
// Debug visual para tablets
const [debugInfo, setDebugInfo] = useState<string[]>([])
2025-12-10 18:36:31 +01:00
const terminalContainerRef = useRef<HTMLDivElement>(null)
const sendKey = useCallback((key: string) => {
if (!termRef.current) return
const keyMap: Record<string, string> = {
escape: "\x1b",
tab: "\t",
up: "\x1b[A",
down: "\x1b[B",
left: "\x1b[D",
right: "\x1b[C",
enter: "\r",
ctrlc: "\x03",
}
const sequence = keyMap[key]
if (sequence && wsRef.current?.readyState === WebSocket.OPEN) {
wsRef.current.send(JSON.stringify({ type: "input", data: sequence }))
}
}, [])
const initializeTerminal = async () => {
const [TerminalClass, FitAddonClass] = await Promise.all([
import("xterm").then((mod) => mod.Terminal),
import("xterm-addon-fit").then((mod) => mod.FitAddon),
import("xterm/css/xterm.css"),
])
const fontSize = window.innerWidth < 768 ? 12 : 16
const term = new TerminalClass({
rendererType: "dom",
fontFamily: '"Courier", "Courier New", "Liberation Mono", "DejaVu Sans Mono", monospace',
fontSize: fontSize,
lineHeight: 1,
cursorBlink: true,
scrollback: 2000,
disableStdin: false,
customGlyphs: true,
fontWeight: "500",
fontWeightBold: "700",
theme: {
background: "#000000",
foreground: "#ffffff",
cursor: "#ffffff",
cursorAccent: "#000000",
black: "#2e3436",
red: "#cc0000",
green: "#4e9a06",
yellow: "#c4a000",
blue: "#3465a4",
magenta: "#75507b",
cyan: "#06989a",
white: "#d3d7cf",
brightBlack: "#555753",
brightRed: "#ef2929",
brightGreen: "#8ae234",
brightYellow: "#fce94f",
brightBlue: "#729fcf",
brightMagenta: "#ad7fa8",
brightCyan: "#34e2e2",
brightWhite: "#eeeeec",
},
})
const fitAddon = new FitAddonClass()
term.loadAddon(fitAddon)
if (terminalContainerRef.current) {
term.open(terminalContainerRef.current)
}
termRef.current = term
fitAddonRef.current = fitAddon
setTimeout(() => {
2025-12-10 21:57:16 +01:00
if (fitAddonRef.current && termRef.current) {
fitAddonRef.current.fit()
2025-12-06 23:06:18 +01:00
}
2025-12-10 21:57:16 +01:00
}, 100)
2025-12-06 23:06:18 +01:00
2025-12-10 18:36:31 +01:00
const wsUrl = getScriptWebSocketUrl(sessionIdRef.current)
const ws = new WebSocket(wsUrl)
wsRef.current = ws
ws.onopen = () => {
2025-12-10 21:47:52 +01:00
setConnectionStatus("online")
2025-12-10 18:36:31 +01:00
const initMessage = {
script_path: scriptPath,
params: {
EXECUTION_MODE: "web",
},
}
2025-12-06 23:06:18 +01:00
2025-12-10 18:36:31 +01:00
ws.send(JSON.stringify(initMessage))
2025-12-06 23:06:18 +01:00
2025-12-10 18:36:31 +01:00
setTimeout(() => {
2025-12-10 21:57:16 +01:00
if (fitAddonRef.current && termRef.current && ws.readyState === WebSocket.OPEN) {
const cols = termRef.current.cols
const rows = termRef.current.rows
2025-12-10 18:36:31 +01:00
ws.send(
JSON.stringify({
type: "resize",
cols: cols,
rows: rows,
}),
)
2025-12-06 23:06:18 +01:00
}
2025-12-10 18:36:31 +01:00
}, 100)
}
2025-12-06 23:06:18 +01:00
2025-12-10 18:36:31 +01:00
ws.onmessage = (event) => {
try {
const msg = JSON.parse(event.data)
2025-12-06 23:06:18 +01:00
2025-12-10 18:36:31 +01:00
if (msg.type === "web_interaction" && msg.interaction) {
2025-12-06 23:06:18 +01:00
setIsWaitingNextInteraction(false)
2025-12-10 17:08:41 +01:00
if (waitingTimeoutRef.current) {
clearTimeout(waitingTimeoutRef.current)
2025-12-06 23:06:18 +01:00
}
2025-12-10 18:36:31 +01:00
setCurrentInteraction({
type: msg.interaction.type,
id: msg.interaction.id,
title: msg.interaction.title || "",
message: msg.interaction.message || "",
options: msg.interaction.options,
default: msg.interaction.default,
})
return
2025-12-06 23:06:18 +01:00
}
2025-12-10 18:36:31 +01:00
if (msg.type === "error") {
term.writeln(`\x1b[31m${msg.message}\x1b[0m`)
return
2025-12-06 23:06:18 +01:00
}
2025-12-10 18:36:31 +01:00
} catch {
// Not JSON, es output normal de terminal
}
2025-12-06 23:06:18 +01:00
2025-12-10 18:36:31 +01:00
term.write(event.data)
2025-12-06 23:06:18 +01:00
2025-12-10 18:36:31 +01:00
setIsWaitingNextInteraction(false)
if (waitingTimeoutRef.current) {
clearTimeout(waitingTimeoutRef.current)
}
}
2025-12-06 23:06:18 +01:00
2025-12-10 18:36:31 +01:00
ws.onerror = (error) => {
2025-12-10 21:47:52 +01:00
setConnectionStatus("offline")
2025-12-10 18:36:31 +01:00
term.writeln("\x1b[31mWebSocket error occurred\x1b[0m")
}
2025-12-06 23:06:18 +01:00
2025-12-10 18:36:31 +01:00
ws.onclose = (event) => {
2025-12-10 21:47:52 +01:00
setConnectionStatus("offline")
2025-12-10 18:36:31 +01:00
term.writeln("\x1b[33mConnection closed\x1b[0m")
if (!isComplete) {
setIsComplete(true)
setExitCode(event.code === 1000 ? 0 : 1)
2025-12-06 23:06:18 +01:00
}
2025-12-10 18:36:31 +01:00
}
2025-12-06 23:06:18 +01:00
2025-12-10 18:36:31 +01:00
term.onData((data) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(data)
}
})
checkConnectionInterval.current = setInterval(() => {
2025-12-10 21:57:16 +01:00
if (wsRef.current) {
2025-12-10 21:47:52 +01:00
setConnectionStatus(
2025-12-10 21:57:16 +01:00
wsRef.current.readyState === WebSocket.OPEN
2025-12-10 21:47:52 +01:00
? "online"
2025-12-10 21:57:16 +01:00
: wsRef.current.readyState === WebSocket.CONNECTING
2025-12-10 21:47:52 +01:00
? "connecting"
: "offline",
)
2025-12-10 18:36:31 +01:00
}
}, 500)
let resizeTimeout: NodeJS.Timeout | null = null
const resizeObserver = new ResizeObserver(() => {
if (resizeTimeout) clearTimeout(resizeTimeout)
resizeTimeout = setTimeout(() => {
2025-12-10 21:57:16 +01:00
if (fitAddonRef.current && termRef.current && wsRef.current?.readyState === WebSocket.OPEN) {
fitAddonRef.current.fit()
wsRef.current.send(
JSON.stringify({
type: "resize",
cols: termRef.current.cols,
rows: termRef.current.rows,
}),
)
2025-12-10 18:36:31 +01:00
}
}, 100)
})
if (terminalContainerRef.current) {
resizeObserver.observe(terminalContainerRef.current)
}
}
2025-12-06 23:06:18 +01:00
useEffect(() => {
2025-12-10 17:08:41 +01:00
const savedHeight = localStorage.getItem("scriptModalHeight")
if (savedHeight) {
2025-12-11 18:27:11 +01:00
const height = Number.parseInt(savedHeight, 10)
setModalHeight(height)
modalHeightRef.current = height // Sincronizar el ref
2025-12-10 17:08:41 +01:00
}
2025-12-10 18:36:31 +01:00
if (isOpen) {
initializeTerminal()
} else {
2025-12-10 17:08:41 +01:00
if (checkConnectionInterval.current) {
clearInterval(checkConnectionInterval.current)
}
if (waitingTimeoutRef.current) {
clearTimeout(waitingTimeoutRef.current)
}
2025-12-06 23:06:18 +01:00
if (wsRef.current) {
wsRef.current.close()
wsRef.current = null
}
if (termRef.current) {
termRef.current.dispose()
termRef.current = null
}
2025-12-06 23:25:35 +01:00
2025-12-10 17:08:41 +01:00
sessionIdRef.current = Math.random().toString(36).substring(2, 8)
2025-12-06 23:06:18 +01:00
setIsComplete(false)
setExitCode(null)
2025-12-10 17:08:41 +01:00
setInteractionInput("")
2025-12-06 23:06:18 +01:00
setCurrentInteraction(null)
setIsWaitingNextInteraction(false)
2025-12-10 21:47:52 +01:00
setConnectionStatus("connecting")
2025-12-06 23:06:18 +01:00
}
2025-12-10 17:08:41 +01:00
}, [isOpen])
2025-12-06 23:06:18 +01:00
2025-12-10 18:54:35 +01:00
useEffect(() => {
const updateDeviceType = () => {
const width = window.innerWidth
const isTouchDevice = "ontouchstart" in window || navigator.maxTouchPoints > 0
const isTabletSize = width >= 768 && width <= 1366
setIsMobile(width < 768)
setIsTablet(isTouchDevice && isTabletSize)
}
updateDeviceType()
const handleResize = () => updateDeviceType()
window.addEventListener("resize", handleResize)
return () => {
window.removeEventListener("resize", handleResize)
}
}, [])
2025-12-06 23:06:18 +01:00
const getScriptWebSocketUrl = (sid: string): string => {
if (typeof window === "undefined") {
return `ws://localhost:${API_PORT}/ws/script/${sid}`
}
2025-12-10 18:36:31 +01:00
const { protocol, hostname, port } = window.location
const isStandardPort = port === "" || port === "80" || port === "443"
2025-12-06 23:06:18 +01:00
const wsProtocol = protocol === "https:" ? "wss:" : "ws:"
2025-12-10 18:36:31 +01:00
if (isStandardPort) {
return `${wsProtocol}//${hostname}/ws/script/${sid}`
} else {
return `${wsProtocol}//${hostname}:${API_PORT}/ws/script/${sid}`
}
2025-12-01 01:15:19 +01:00
}
2025-12-06 11:25:27 +01:00
const handleInteractionResponse = (value: string) => {
2025-12-06 23:06:18 +01:00
if (!wsRef.current || !currentInteraction) {
return
}
2025-12-06 19:03:19 +01:00
if (value === "cancel" || value === "") {
setCurrentInteraction(null)
2025-12-10 17:08:41 +01:00
setInteractionInput("")
2025-12-06 23:06:18 +01:00
handleCloseModal()
2025-12-06 19:03:19 +01:00
return
}
2025-12-06 23:06:18 +01:00
const response = JSON.stringify({
type: "interaction_response",
id: currentInteraction.id,
value: value,
})
if (wsRef.current.readyState === WebSocket.OPEN) {
wsRef.current.send(response)
}
2025-12-06 11:25:27 +01:00
setCurrentInteraction(null)
2025-12-10 17:08:41 +01:00
setInteractionInput("")
2025-12-06 23:06:18 +01:00
2025-12-10 17:08:41 +01:00
waitingTimeoutRef.current = setTimeout(() => {
setIsWaitingNextInteraction(true)
}, 50)
2025-12-06 23:06:18 +01:00
}
const handleCloseModal = () => {
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
wsRef.current.close()
}
2025-12-10 17:08:41 +01:00
if (checkConnectionInterval.current) {
clearInterval(checkConnectionInterval.current)
}
2025-12-06 23:06:18 +01:00
if (termRef.current) {
termRef.current.dispose()
}
onClose()
2025-12-06 18:36:34 +01:00
}
2025-12-11 17:44:23 +01:00
const handleResizeStart = (e: React.MouseEvent | React.TouchEvent) => {
2025-12-06 23:25:35 +01:00
e.preventDefault()
e.stopPropagation()
2025-12-11 18:19:38 +01:00
const debugMsg = `[${new Date().toLocaleTimeString()}] Resize start - Type: ${e.type}, isMobile: ${isMobile}, isTablet: ${isTablet}`
console.log(debugMsg)
setDebugInfo(prev => [...prev.slice(-4), debugMsg])
2025-12-11 17:30:12 +01:00
setIsResizing(true)
2025-12-11 17:44:23 +01:00
// Detectar si es touch o mouse
const clientY = "touches" in e ? e.touches[0].clientY : e.clientY
const startY = clientY
2025-12-06 23:25:35 +01:00
const startHeight = modalHeight
2025-12-06 22:20:34 +01:00
2025-12-11 18:19:38 +01:00
const debugMsg2 = `Start Y: ${startY}, Start height: ${startHeight}`
console.log(debugMsg2)
setDebugInfo(prev => [...prev.slice(-4), debugMsg2])
let moveCount = 0
2025-12-11 17:44:23 +01:00
const handleMove = (moveEvent: MouseEvent | TouchEvent) => {
2025-12-11 18:19:38 +01:00
moveEvent.preventDefault()
moveEvent.stopPropagation()
2025-12-11 17:44:23 +01:00
const currentY = "touches" in moveEvent ? moveEvent.touches[0].clientY : moveEvent.clientY
2025-12-06 23:25:35 +01:00
const deltaY = currentY - startY
2025-12-10 23:43:05 +01:00
const newHeight = Math.max(300, Math.min(window.innerHeight - 100, startHeight + deltaY))
2025-12-06 23:25:35 +01:00
2025-12-11 18:19:38 +01:00
moveCount++
if (moveCount % 5 === 0) {
console.log(`Move #${moveCount} - currentY: ${currentY}, deltaY: ${deltaY}, newHeight: ${newHeight}`)
}
2025-12-11 18:27:11 +01:00
// Actualizar tanto el state como el ref
modalHeightRef.current = newHeight
2025-12-06 23:25:35 +01:00
setModalHeight(newHeight)
if (fitAddonRef.current && termRef.current && wsRef.current?.readyState === WebSocket.OPEN) {
2025-12-10 21:57:16 +01:00
setTimeout(() => {
2025-12-10 22:10:41 +01:00
if (fitAddonRef.current && termRef.current) {
fitAddonRef.current.fit()
wsRef.current?.send(
JSON.stringify({
type: "resize",
cols: termRef.current.cols,
rows: termRef.current.rows,
}),
)
}
2025-12-10 21:57:16 +01:00
}, 10)
2025-12-06 23:25:35 +01:00
}
}
const handleEnd = () => {
2025-12-11 18:27:11 +01:00
// Usar el ref que tiene el valor actualizado
const finalHeight = modalHeightRef.current
const debugMsg3 = `Resize end - Final height: ${finalHeight}, Total moves: ${moveCount}`
2025-12-11 18:19:38 +01:00
console.log(debugMsg3)
setDebugInfo(prev => [...prev.slice(-4), debugMsg3])
2025-12-11 17:30:12 +01:00
setIsResizing(false)
2025-12-11 17:44:23 +01:00
2025-12-11 18:19:38 +01:00
document.removeEventListener("mousemove", handleMove as any, false)
document.removeEventListener("mouseup", handleEnd, false)
document.removeEventListener("touchmove", handleMove as any, false)
document.removeEventListener("touchend", handleEnd, false)
document.removeEventListener("touchcancel", handleEnd, false)
2025-12-06 23:25:35 +01:00
2025-12-11 18:27:11 +01:00
// Guardar usando el valor del ref
localStorage.setItem("scriptModalHeight", finalHeight.toString())
2025-12-10 18:36:31 +01:00
}
2025-12-06 22:20:34 +01:00
2025-12-11 18:19:38 +01:00
document.addEventListener("mousemove", handleMove as any, false)
document.addEventListener("mouseup", handleEnd, false)
document.addEventListener("touchmove", handleMove as any, { passive: false, capture: false })
document.addEventListener("touchend", handleEnd, false)
document.addEventListener("touchcancel", handleEnd, false)
2025-12-06 22:20:34 +01:00
}
2025-12-10 22:30:11 +01:00
const sendCommand = (command: string) => {
if (wsRef.current?.readyState === WebSocket.OPEN) {
wsRef.current.send(JSON.stringify({ type: "input", data: command }))
}
}
2025-12-01 01:04:31 +01:00
return (
<>
2025-12-10 17:08:41 +01:00
<Dialog open={isOpen} onOpenChange={onClose}>
2025-12-06 18:36:34 +01:00
<DialogContent
2025-12-10 17:08:41 +01:00
className="max-w-7xl p-0 flex flex-col gap-0 overflow-hidden"
2025-12-10 23:34:30 +01:00
style={{
2025-12-10 23:43:05 +01:00
height: isMobile || isTablet ? "80vh" : `${modalHeight}px`,
2025-12-10 23:34:30 +01:00
maxHeight: "none",
}}
2025-12-06 18:36:34 +01:00
onInteractOutside={(e) => e.preventDefault()}
onEscapeKeyDown={(e) => e.preventDefault()}
2025-12-10 22:48:07 +01:00
hideClose
2025-12-06 18:36:34 +01:00
>
2025-12-10 20:17:13 +01:00
<DialogTitle className="sr-only">{title}</DialogTitle>
2025-12-10 17:08:41 +01:00
<div className="flex items-center gap-2 p-4 border-b">
<div>
2025-12-10 20:17:13 +01:00
<h2 className="text-lg font-semibold">{title}</h2>
{description && <p className="text-sm text-muted-foreground">{description}</p>}
2025-12-10 17:08:41 +01:00
</div>
</div>
2025-12-01 01:04:31 +01:00
2025-12-11 18:19:38 +01:00
{/* Debug panel - solo visible en tablets */}
{isTablet && debugInfo.length > 0 && (
<div className="bg-yellow-900/50 border-b border-yellow-700 p-2 text-xs font-mono text-yellow-200 max-h-20 overflow-y-auto">
{debugInfo.map((info, i) => (
<div key={i}>{info}</div>
))}
</div>
)}
2025-12-06 23:06:18 +01:00
<div className="overflow-hidden relative flex-1">
<div ref={terminalContainerRef} className="w-full h-full" />
{isWaitingNextInteraction && !currentInteraction && (
<div className="absolute inset-0 flex items-center justify-center bg-black/50 backdrop-blur-sm">
<div className="flex flex-col items-center gap-3">
<Loader2 className="h-8 w-8 animate-spin text-blue-500" />
<p className="text-sm text-muted-foreground">Processing...</p>
</div>
</div>
)}
2025-12-01 01:15:19 +01:00
</div>
2025-12-01 01:04:31 +01:00
2025-12-10 23:48:43 +01:00
{/* Resize bar - visible en tablet y escritorio */}
{!isMobile && (
<div
2025-12-11 17:30:12 +01:00
ref={resizeBarRef}
2025-12-11 17:44:23 +01:00
onMouseDown={handleResizeStart}
onTouchStart={handleResizeStart}
className={`h-2 w-full cursor-row-resize transition-colors flex items-center justify-center group relative ${
isResizing ? "bg-blue-500" : "bg-zinc-800 hover:bg-blue-600"
2025-12-11 17:30:12 +01:00
}`}
2025-12-11 17:44:23 +01:00
style={{ touchAction: "none" }}
2025-12-10 23:48:43 +01:00
>
2025-12-11 17:44:23 +01:00
<GripHorizontal className={`h-4 w-4 transition-colors pointer-events-none ${
isResizing ? "text-white" : "text-zinc-600 group-hover:text-white"
}`} />
2025-12-10 23:48:43 +01:00
</div>
)}
{/* Mobile/Tablet button toolbar */}
2025-12-10 23:34:30 +01:00
{(isMobile || isTablet) && (
2025-12-10 22:48:07 +01:00
<div className="flex items-center justify-center gap-1.5 px-1 py-2 border-t bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
2025-12-10 18:54:35 +01:00
<Button
2025-12-10 22:30:11 +01:00
onPointerDown={(e) => {
e.preventDefault()
e.stopPropagation()
sendCommand("\x1b")
}}
2025-12-10 18:54:35 +01:00
variant="outline"
size="sm"
2025-12-11 17:44:23 +01:00
className="h-8 px-2 text-xs bg-zinc-800 hover:bg-zinc-700 border-zinc-700 text-white min-w-[50px]"
2025-12-10 18:54:35 +01:00
>
ESC
</Button>
<Button
2025-12-10 22:30:11 +01:00
onPointerDown={(e) => {
e.preventDefault()
e.stopPropagation()
sendCommand("\t")
}}
2025-12-10 18:54:35 +01:00
variant="outline"
size="sm"
2025-12-11 17:44:23 +01:00
className="h-8 px-2 text-xs bg-zinc-800 hover:bg-zinc-700 border-zinc-700 text-white min-w-[50px]"
2025-12-10 18:54:35 +01:00
>
TAB
</Button>
<Button
2025-12-10 22:30:11 +01:00
onPointerDown={(e) => {
e.preventDefault()
e.stopPropagation()
sendCommand("\x1b[A")
}}
2025-12-10 18:54:35 +01:00
variant="outline"
size="sm"
2025-12-10 22:30:11 +01:00
className="h-8 px-2.5 text-xs bg-zinc-800 hover:bg-zinc-700 border-zinc-700 text-white"
2025-12-10 18:54:35 +01:00
>
2025-12-10 22:30:11 +01:00
<ArrowUp className="h-4 w-4" />
2025-12-10 18:54:35 +01:00
</Button>
<Button
2025-12-10 22:30:11 +01:00
onPointerDown={(e) => {
e.preventDefault()
e.stopPropagation()
sendCommand("\x1b[B")
}}
2025-12-10 18:54:35 +01:00
variant="outline"
size="sm"
2025-12-10 22:30:11 +01:00
className="h-8 px-2.5 text-xs bg-zinc-800 hover:bg-zinc-700 border-zinc-700 text-white"
2025-12-10 18:54:35 +01:00
>
2025-12-10 22:30:11 +01:00
<ArrowDown className="h-4 w-4" />
2025-12-10 18:54:35 +01:00
</Button>
<Button
2025-12-10 22:30:11 +01:00
onPointerDown={(e) => {
e.preventDefault()
e.stopPropagation()
sendCommand("\x1b[D")
}}
2025-12-10 18:54:35 +01:00
variant="outline"
size="sm"
2025-12-10 22:30:11 +01:00
className="h-8 px-2.5 text-xs bg-zinc-800 hover:bg-zinc-700 border-zinc-700 text-white"
2025-12-10 18:54:35 +01:00
>
2025-12-10 22:30:11 +01:00
<ArrowLeft className="h-4 w-4" />
2025-12-10 18:54:35 +01:00
</Button>
<Button
2025-12-10 22:30:11 +01:00
onPointerDown={(e) => {
e.preventDefault()
e.stopPropagation()
sendCommand("\x1b[C")
}}
2025-12-10 18:54:35 +01:00
variant="outline"
size="sm"
2025-12-10 22:30:11 +01:00
className="h-8 px-2.5 text-xs bg-zinc-800 hover:bg-zinc-700 border-zinc-700 text-white"
2025-12-10 18:54:35 +01:00
>
2025-12-10 22:30:11 +01:00
<ArrowRight className="h-4 w-4" />
2025-12-10 18:54:35 +01:00
</Button>
<Button
2025-12-10 22:30:11 +01:00
onPointerDown={(e) => {
e.preventDefault()
e.stopPropagation()
sendCommand("\r")
}}
2025-12-10 18:54:35 +01:00
variant="outline"
size="sm"
2025-12-10 22:30:11 +01:00
className="h-8 px-2.5 text-xs bg-zinc-800 hover:bg-zinc-700 border-zinc-700 text-white"
2025-12-10 18:54:35 +01:00
>
2025-12-10 22:30:11 +01:00
<CornerDownLeft className="h-4 w-4" />
2025-12-10 18:54:35 +01:00
</Button>
<Button
2025-12-10 22:30:11 +01:00
onPointerDown={(e) => {
e.preventDefault()
e.stopPropagation()
sendCommand("\x03")
}}
2025-12-10 18:54:35 +01:00
variant="outline"
size="sm"
2025-12-11 17:44:23 +01:00
className="h-8 px-2 text-xs bg-zinc-800 hover:bg-zinc-700 border-zinc-700 text-white min-w-[65px]"
2025-12-10 18:54:35 +01:00
>
CTRL+C
</Button>
2025-12-10 18:36:31 +01:00
</div>
)}
2025-12-10 23:48:43 +01:00
{/* Footer with connection status and close button */}
<div className="flex items-center justify-between px-4 py-3 border-t bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
2025-12-06 23:06:18 +01:00
<div className="flex items-center gap-3">
2025-12-10 17:08:41 +01:00
<Activity className="h-5 w-5 text-blue-500" />
<div
2025-12-10 21:47:52 +01:00
className={`w-2 h-2 rounded-full ${
connectionStatus === "online"
? "bg-green-500"
: connectionStatus === "connecting"
? "bg-blue-500"
: "bg-red-500"
}`}
title={
connectionStatus === "online"
? "Connected"
: connectionStatus === "connecting"
? "Connecting"
: "Disconnected"
}
2025-12-10 17:08:41 +01:00
></div>
2025-12-10 21:47:52 +01:00
<span className="text-xs text-muted-foreground">
{connectionStatus === "online"
? "Online"
: connectionStatus === "connecting"
? "Connecting..."
: "Offline"}
</span>
2025-12-06 23:06:18 +01:00
</div>
<Button
onClick={handleCloseModal}
variant="outline"
className="bg-red-600 hover:bg-red-700 border-red-500 text-white"
>
Close
</Button>
</div>
2025-12-01 01:04:31 +01:00
</DialogContent>
</Dialog>
2025-12-06 11:25:27 +01:00
2025-12-10 17:08:41 +01:00
{currentInteraction && (
2025-12-06 13:54:37 +01:00
<Dialog open={true}>
2025-12-06 13:11:04 +01:00
<DialogContent
2025-12-06 23:25:35 +01:00
className="max-w-4xl max-h-[80vh] overflow-y-auto animate-in fade-in-0 zoom-in-95 duration-100"
2025-12-06 13:54:37 +01:00
onInteractOutside={(e) => e.preventDefault()}
onEscapeKeyDown={(e) => e.preventDefault()}
2025-12-06 19:03:19 +01:00
hideClose
2025-12-06 13:11:04 +01:00
>
2025-12-06 11:25:27 +01:00
<DialogTitle>{currentInteraction.title}</DialogTitle>
<div className="space-y-4">
2025-12-10 17:35:43 +01:00
<p
className="whitespace-pre-wrap"
dangerouslySetInnerHTML={{
__html: currentInteraction.message.replace(/\\n/g, "<br/>").replace(/\n/g, "<br/>"),
}}
/>
2025-12-06 11:25:27 +01:00
{currentInteraction.type === "yesno" && (
<div className="flex gap-2">
2025-12-06 13:00:29 +01:00
<Button
onClick={() => handleInteractionResponse("yes")}
2025-12-06 23:25:35 +01:00
className="flex-1 bg-blue-600 hover:bg-blue-700 text-white transition-all duration-150"
2025-12-06 13:00:29 +01:00
>
2025-12-06 11:25:27 +01:00
Yes
</Button>
2025-12-06 19:03:19 +01:00
<Button
onClick={() => handleInteractionResponse("cancel")}
variant="outline"
2025-12-06 23:25:35 +01:00
className="flex-1 hover:bg-red-600 hover:text-white hover:border-red-600 transition-all duration-150"
2025-12-06 19:03:19 +01:00
>
Cancel
</Button>
2025-12-06 11:25:27 +01:00
</div>
)}
{currentInteraction.type === "menu" && currentInteraction.options && (
<div className="space-y-2">
2025-12-06 23:25:35 +01:00
{currentInteraction.options.map((option, index) => (
2025-12-06 11:25:27 +01:00
<Button
key={option.value}
onClick={() => handleInteractionResponse(option.value)}
variant="outline"
2025-12-06 23:25:35 +01:00
className="w-full justify-start hover:bg-blue-600 hover:text-white transition-all duration-100 animate-in fade-in-0 slide-in-from-left-2"
style={{ animationDelay: `${index * 30}ms` }}
2025-12-06 11:25:27 +01:00
>
{option.label}
</Button>
))}
2025-12-06 18:36:34 +01:00
<Button
onClick={() => handleInteractionResponse("cancel")}
variant="outline"
2025-12-06 23:25:35 +01:00
className="w-full hover:bg-red-600 hover:text-white hover:border-red-600 transition-all duration-150"
2025-12-06 18:36:34 +01:00
>
2025-12-06 19:03:19 +01:00
Cancel
2025-12-06 18:36:34 +01:00
</Button>
2025-12-06 11:25:27 +01:00
</div>
)}
2025-12-06 13:54:37 +01:00
{(currentInteraction.type === "input" || currentInteraction.type === "inputbox") && (
<div className="space-y-2">
<Label>Your input:</Label>
<Input
2025-12-10 17:08:41 +01:00
value={interactionInput}
onChange={(e) => setInteractionInput(e.target.value)}
2025-12-06 13:54:37 +01:00
onKeyDown={(e) => {
if (e.key === "Enter") {
2025-12-10 17:08:41 +01:00
handleInteractionResponse(interactionInput)
2025-12-06 13:54:37 +01:00
}
}}
placeholder={currentInteraction.default || ""}
2025-12-06 23:25:35 +01:00
className="transition-all duration-150"
2025-12-06 13:54:37 +01:00
/>
2025-12-10 17:08:41 +01:00
<div className="flex gap-2">
<Button
onClick={() => handleInteractionResponse(interactionInput)}
className="flex-1 bg-blue-600 hover:bg-blue-700 transition-all duration-150"
>
Submit
</Button>
<Button
onClick={() => handleInteractionResponse("cancel")}
variant="outline"
className="flex-1 hover:bg-red-600 hover:text-white hover:border-red-600 transition-all duration-150"
>
Cancel
</Button>
</div>
2025-12-06 13:54:37 +01:00
</div>
)}
2025-12-06 11:25:27 +01:00
{currentInteraction.type === "msgbox" && (
2025-12-06 19:03:19 +01:00
<div className="flex gap-2">
<Button
onClick={() => handleInteractionResponse("ok")}
2025-12-06 23:25:35 +01:00
className="flex-1 bg-blue-600 hover:bg-blue-700 transition-all duration-150"
2025-12-06 19:03:19 +01:00
>
OK
</Button>
<Button
onClick={() => handleInteractionResponse("cancel")}
variant="outline"
2025-12-06 23:25:35 +01:00
className="flex-1 hover:bg-red-600 hover:text-white hover:border-red-600 transition-all duration-150"
2025-12-06 19:03:19 +01:00
>
Cancel
</Button>
</div>
2025-12-06 11:25:27 +01:00
)}
</div>
</DialogContent>
</Dialog>
)}
2025-12-01 01:04:31 +01:00
</>
)
2025-12-11 17:30:12 +01:00
}