mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2025-12-14 16:16:21 +00:00
Update script-terminal-modal.tsx
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"use client"
|
||||
|
||||
import { useState } from "react"
|
||||
import { useState, useEffect, useRef } from "react"
|
||||
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import { X, CheckCircle2, XCircle, Loader2 } from "lucide-react"
|
||||
@@ -17,14 +17,6 @@ interface ScriptTerminalModalProps {
|
||||
description: string
|
||||
}
|
||||
|
||||
interface WebInteraction {
|
||||
type: "yesno" | "menu" | "msgbox" | "input"
|
||||
id: string
|
||||
title: string
|
||||
message: string
|
||||
options?: Array<{ label: string; value: string }>
|
||||
}
|
||||
|
||||
export function ScriptTerminalModal({
|
||||
open,
|
||||
onClose,
|
||||
@@ -37,6 +29,98 @@ export function ScriptTerminalModal({
|
||||
const [sessionId] = useState(() => Math.random().toString(36).substring(7))
|
||||
const [isComplete, setIsComplete] = useState(false)
|
||||
const [exitCode, setExitCode] = useState<number | null>(null)
|
||||
const wsRef = useRef<WebSocket | null>(null)
|
||||
const terminalRef = useRef<any>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
|
||||
const originalWebSocket = window.WebSocket
|
||||
let interceptedWs: WebSocket | null = null
|
||||
|
||||
window.WebSocket = ((url: string | URL, protocols?: string | string[]) => {
|
||||
const ws = new originalWebSocket(url, protocols)
|
||||
|
||||
// Solo interceptar nuestro WebSocket específico
|
||||
if (url.toString().includes(`/ws/script/${sessionId}`)) {
|
||||
interceptedWs = ws
|
||||
wsRef.current = ws
|
||||
|
||||
// Cuando se abre la conexión, enviar los parámetros del script
|
||||
ws.addEventListener("open", () => {
|
||||
const initMessage = JSON.stringify({
|
||||
script_path: scriptPath,
|
||||
params: params,
|
||||
})
|
||||
console.log("[v0] Sending script init message:", initMessage)
|
||||
ws.send(initMessage)
|
||||
})
|
||||
|
||||
// Interceptar mensajes entrantes para filtrar y detectar eventos especiales
|
||||
const originalOnMessage = Object.getOwnPropertyDescriptor(WebSocket.prototype, "onmessage")
|
||||
|
||||
Object.defineProperty(ws, "onmessage", {
|
||||
set(handler) {
|
||||
const wrappedHandler = (event: MessageEvent) => {
|
||||
const data = event.data
|
||||
|
||||
if (typeof data === "string" && data.includes("Connected to ProxMenux terminal")) {
|
||||
console.log("[v0] Filtered welcome message")
|
||||
return // No pasar este mensaje a la terminal
|
||||
}
|
||||
|
||||
// Detectar mensajes JSON especiales
|
||||
try {
|
||||
const parsed = JSON.parse(data)
|
||||
|
||||
// Detectar finalización del script
|
||||
if (parsed.type === "exit") {
|
||||
console.log("[v0] Script completed with exit code:", parsed.code)
|
||||
setIsComplete(true)
|
||||
setExitCode(parsed.code || 0)
|
||||
}
|
||||
|
||||
// Detectar errores
|
||||
if (parsed.type === "error") {
|
||||
console.error("[v0] Script error:", parsed.message)
|
||||
}
|
||||
} catch (e) {
|
||||
// No es JSON, es output normal de terminal
|
||||
}
|
||||
|
||||
// Pasar el mensaje al handler original
|
||||
if (handler) {
|
||||
handler.call(ws, event)
|
||||
}
|
||||
}
|
||||
|
||||
// Almacenar el handler wrapped
|
||||
Object.defineProperty(this, "_wrappedOnMessage", {
|
||||
value: wrappedHandler,
|
||||
writable: true,
|
||||
})
|
||||
|
||||
if (originalOnMessage?.set) {
|
||||
originalOnMessage.set.call(this, wrappedHandler)
|
||||
}
|
||||
},
|
||||
get() {
|
||||
return this._wrappedOnMessage
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return ws
|
||||
}) as any
|
||||
|
||||
// Cleanup: restaurar WebSocket original
|
||||
return () => {
|
||||
window.WebSocket = originalWebSocket
|
||||
if (interceptedWs) {
|
||||
wsRef.current = null
|
||||
}
|
||||
}
|
||||
}, [open, sessionId, scriptPath, params])
|
||||
|
||||
const getScriptWebSocketUrl = (): string => {
|
||||
if (typeof window === "undefined") {
|
||||
@@ -79,7 +163,7 @@ export function ScriptTerminalModal({
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-hidden">
|
||||
<TerminalPanel websocketUrl={getScriptWebSocketUrl()} />
|
||||
<TerminalPanel ref={terminalRef} websocketUrl={getScriptWebSocketUrl()} />
|
||||
</div>
|
||||
|
||||
{/* Footer */}
|
||||
|
||||
Reference in New Issue
Block a user