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

338 lines
11 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 11:19:26 +01:00
import { useState, useEffect, useRef } from "react"
2025-12-01 01:22:04 +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-06 18:36:34 +01:00
import { CheckCircle2, XCircle, Loader2, Activity, GripHorizontal } from "lucide-react"
2025-12-06 13:54:37 +01:00
import { TerminalPanel } from "./terminal-panel"
2025-12-01 01:40:04 +01:00
import { API_PORT } from "@/lib/api-config"
2025-12-06 18:36:34 +01:00
import { useIsMobile } from "@/hooks/use-mobile"
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
scriptName: string
params?: Record<string, string>
title: string
description: string
}
export function ScriptTerminalModal({
open,
onClose,
scriptPath,
scriptName,
params = {},
title,
description,
}: ScriptTerminalModalProps) {
const [sessionId] = useState(() => Math.random().toString(36).substring(7))
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)
const [interactionInput, setInteractionInput] = useState("")
2025-12-06 13:54:37 +01:00
const wsRef = useRef<WebSocket | null>(null)
2025-12-06 18:36:34 +01:00
const [isConnected, setIsConnected] = useState(false)
const checkConnectionInterval = useRef<NodeJS.Timeout | null>(null)
const isMobile = useIsMobile()
const [modalHeight, setModalHeight] = useState(80)
const [isResizing, setIsResizing] = useState(false)
const startYRef = useRef(0)
const startHeightRef = useRef(80)
2025-12-06 13:28:56 +01:00
2025-12-06 11:54:36 +01:00
useEffect(() => {
if (open) {
2025-12-06 13:38:19 +01:00
setIsComplete(false)
setExitCode(null)
2025-12-06 12:46:41 +01:00
setInteractionInput("")
2025-12-06 13:38:19 +01:00
setCurrentInteraction(null)
2025-12-06 18:36:34 +01:00
setIsConnected(false)
checkConnectionInterval.current = setInterval(() => {
if (wsRef.current) {
setIsConnected(wsRef.current.readyState === WebSocket.OPEN)
}
}, 500)
}
return () => {
if (checkConnectionInterval.current) {
clearInterval(checkConnectionInterval.current)
}
2025-12-06 11:54:36 +01:00
}
2025-12-06 13:54:37 +01:00
}, [open])
2025-12-06 11:54:36 +01:00
2025-12-06 18:36:34 +01:00
const handleResizeStart = (e: React.MouseEvent | React.TouchEvent) => {
if (isMobile) return
setIsResizing(true)
startYRef.current = "touches" in e ? e.touches[0].clientY : e.clientY
startHeightRef.current = modalHeight
e.preventDefault()
}
useEffect(() => {
if (!isResizing) return
const handleResizeMove = (e: MouseEvent | TouchEvent) => {
const currentY = "touches" in e ? e.touches[0].clientY : e.clientY
const deltaY = startYRef.current - currentY
const viewportHeight = window.innerHeight
const deltaVh = (deltaY / viewportHeight) * 100
const newHeight = Math.min(Math.max(startHeightRef.current + deltaVh, 50), 95)
setModalHeight(newHeight)
}
const handleResizeEnd = () => {
setIsResizing(false)
}
document.addEventListener("mousemove", handleResizeMove)
document.addEventListener("mouseup", handleResizeEnd)
document.addEventListener("touchmove", handleResizeMove)
document.addEventListener("touchend", handleResizeEnd)
return () => {
document.removeEventListener("mousemove", handleResizeMove)
document.removeEventListener("mouseup", handleResizeEnd)
document.removeEventListener("touchmove", handleResizeMove)
document.removeEventListener("touchend", handleResizeEnd)
}
}, [isResizing])
2025-12-01 01:40:04 +01:00
const getScriptWebSocketUrl = (): string => {
2025-12-01 01:15:19 +01:00
if (typeof window === "undefined") {
2025-12-01 01:40:04 +01:00
return `ws://localhost:${API_PORT}/ws/script/${sessionId}`
2025-12-01 01:15:19 +01:00
}
2025-12-01 01:40:04 +01:00
const { hostname, protocol } = window.location
const wsProtocol = protocol === "https:" ? "wss:" : "ws:"
return `${wsProtocol}//${hostname}:${API_PORT}/ws/script/${sessionId}`
2025-12-01 01:15:19 +01:00
}
2025-12-06 11:54:36 +01:00
const wsUrl = getScriptWebSocketUrl()
2025-12-06 12:46:41 +01:00
2025-12-06 13:54:37 +01:00
const handleWebSocketCreated = (ws: WebSocket) => {
wsRef.current = ws
2025-12-06 18:36:34 +01:00
setIsConnected(ws.readyState === WebSocket.OPEN)
2025-12-06 13:54:37 +01:00
}
2025-12-06 12:46:41 +01:00
const handleWebInteraction = (interaction: WebInteraction) => {
setCurrentInteraction(interaction)
}
2025-12-06 11:54:36 +01:00
2025-12-06 11:25:27 +01:00
const handleInteractionResponse = (value: string) => {
2025-12-06 13:54:37 +01:00
if (!wsRef.current || !currentInteraction) {
2025-12-06 13:28:56 +01:00
return
}
2025-12-06 11:25:27 +01:00
const response = JSON.stringify({
type: "interaction_response",
id: currentInteraction.id,
value: value,
})
2025-12-06 13:54:37 +01:00
if (wsRef.current.readyState === WebSocket.OPEN) {
wsRef.current.send(response)
}
2025-12-06 12:46:41 +01:00
2025-12-06 11:25:27 +01:00
setCurrentInteraction(null)
setInteractionInput("")
}
2025-12-06 18:36:34 +01:00
const handleCloseModal = () => {
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
wsRef.current.close()
}
if (checkConnectionInterval.current) {
clearInterval(checkConnectionInterval.current)
}
onClose()
}
2025-12-01 01:04:31 +01:00
return (
<>
2025-12-06 18:36:34 +01:00
<Dialog open={open}>
<DialogContent
className="max-w-4xl p-0 flex flex-col"
style={{ height: isMobile ? "80vh" : `${modalHeight}vh` }}
onInteractOutside={(e) => e.preventDefault()}
onEscapeKeyDown={(e) => e.preventDefault()}
>
2025-12-01 01:22:04 +01:00
<DialogTitle className="sr-only">{title}</DialogTitle>
2025-12-06 18:36:34 +01:00
<div className="flex items-center gap-2 p-4 border-b">
{isComplete ? (
exitCode === 0 ? (
<CheckCircle2 className="h-5 w-5 text-green-500" />
2025-12-01 01:15:19 +01:00
) : (
2025-12-06 18:36:34 +01:00
<XCircle className="h-5 w-5 text-red-500" />
)
) : (
<Loader2 className="h-5 w-5 animate-spin" />
)}
<div>
<h2 className="text-lg font-semibold">{title}</h2>
{description && <p className="text-sm text-muted-foreground">{description}</p>}
2025-12-01 01:15:19 +01:00
</div>
2025-12-01 01:04:31 +01:00
</div>
2025-12-01 01:40:04 +01:00
<div className="flex-1 overflow-hidden">
2025-12-06 11:30:49 +01:00
<TerminalPanel
2025-12-06 11:54:36 +01:00
websocketUrl={wsUrl}
2025-12-06 11:30:49 +01:00
initMessage={{
script_path: scriptPath,
params: params,
}}
2025-12-06 12:46:41 +01:00
onWebInteraction={handleWebInteraction}
2025-12-06 13:54:37 +01:00
onWebSocketCreated={handleWebSocketCreated}
2025-12-06 18:36:34 +01:00
isScriptModal={true}
2025-12-06 11:30:49 +01:00
/>
2025-12-01 01:15:19 +01:00
</div>
2025-12-01 01:04:31 +01:00
2025-12-06 18:36:34 +01:00
{!isMobile && (
<div
className="h-2 bg-border hover:bg-primary/20 cursor-ns-resize flex items-center justify-center transition-colors"
onMouseDown={handleResizeStart}
onTouchStart={handleResizeStart}
>
<GripHorizontal className="h-4 w-4 text-muted-foreground" />
</div>
)}
2025-12-01 01:15:19 +01:00
<div className="flex items-center justify-between p-4 border-t">
2025-12-06 18:36:34 +01:00
<div className="flex items-center gap-3">
<Activity className="h-5 w-5 text-blue-500" />
<div
className={`w-2 h-2 rounded-full ${isConnected ? "bg-green-500" : "bg-red-500"}`}
title={isConnected ? "Connected" : "Disconnected"}
></div>
<span className="text-xs text-muted-foreground">{isConnected ? "Online" : "Offline"}</span>
</div>
<Button
onClick={handleCloseModal}
variant="outline"
className="bg-red-600 hover:bg-red-700 border-red-500 text-white"
>
Close
</Button>
2025-12-01 01:04:31 +01:00
</div>
</DialogContent>
</Dialog>
2025-12-06 11:25:27 +01:00
{currentInteraction && (
2025-12-06 13:54:37 +01:00
<Dialog open={true}>
2025-12-06 13:11:04 +01:00
<DialogContent
className="max-w-4xl max-h-[80vh] overflow-y-auto"
2025-12-06 13:54:37 +01:00
onInteractOutside={(e) => e.preventDefault()}
onEscapeKeyDown={(e) => e.preventDefault()}
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-06 12:25:57 +01:00
<p className="whitespace-pre-wrap">{currentInteraction.message}</p>
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")}
className="flex-1 bg-blue-600 hover:bg-blue-700 text-white"
>
2025-12-06 11:25:27 +01:00
Yes
</Button>
2025-12-06 13:00:29 +01:00
<Button
onClick={() => handleInteractionResponse("no")}
variant="outline"
className="flex-1 hover:bg-red-600 hover:text-white hover:border-red-600"
>
2025-12-06 11:25:27 +01:00
No
</Button>
</div>
)}
{currentInteraction.type === "menu" && currentInteraction.options && (
<div className="space-y-2">
{currentInteraction.options.map((option) => (
<Button
key={option.value}
onClick={() => handleInteractionResponse(option.value)}
variant="outline"
2025-12-06 18:36:34 +01:00
className="w-full justify-start hover:bg-blue-600 hover:text-white"
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"
className="w-full hover:bg-red-600 hover:text-white hover:border-red-600"
>
Cancel / Go Back
</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
value={interactionInput}
onChange={(e) => setInteractionInput(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
handleInteractionResponse(interactionInput)
}
}}
placeholder={currentInteraction.default || ""}
/>
2025-12-06 18:36:34 +01:00
<div className="flex gap-2">
<Button
onClick={() => handleInteractionResponse(interactionInput)}
className="flex-1 bg-blue-600 hover:bg-blue-700"
>
Submit
</Button>
<Button
onClick={() => handleInteractionResponse("")}
variant="outline"
className="flex-1 hover:bg-red-600 hover:text-white hover:border-red-600"
>
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 18:36:34 +01:00
<Button
onClick={() => handleInteractionResponse("ok")}
className="w-full bg-blue-600 hover:bg-blue-700"
>
2025-12-06 11:25:27 +01:00
OK
</Button>
)}
</div>
</DialogContent>
</Dialog>
)}
2025-12-01 01:04:31 +01:00
</>
)
}