"use client" import type React from "react" import { useState, useEffect, useRef } from "react" import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { CheckCircle2, XCircle, Loader2, Activity, GripHorizontal } from "lucide-react" import { TerminalPanel } from "./terminal-panel" import { API_PORT } from "@/lib/api-config" import { useIsMobile } from "@/hooks/use-mobile" interface WebInteraction { type: "yesno" | "menu" | "msgbox" | "input" | "inputbox" id: string title: string message: string options?: Array<{ label: string; value: string }> default?: string } interface ScriptTerminalModalProps { open: boolean onClose: () => void scriptPath: string scriptName: string params?: Record 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(null) const [currentInteraction, setCurrentInteraction] = useState(null) const [interactionInput, setInteractionInput] = useState("") const wsRef = useRef(null) const [isConnected, setIsConnected] = useState(false) const checkConnectionInterval = useRef(null) const isMobile = useIsMobile() const [modalHeight, setModalHeight] = useState(80) const [isResizing, setIsResizing] = useState(false) const startYRef = useRef(0) const startHeightRef = useRef(80) useEffect(() => { if (open) { setIsComplete(false) setExitCode(null) setInteractionInput("") setCurrentInteraction(null) setIsConnected(false) checkConnectionInterval.current = setInterval(() => { if (wsRef.current) { setIsConnected(wsRef.current.readyState === WebSocket.OPEN) } }, 500) } return () => { if (checkConnectionInterval.current) { clearInterval(checkConnectionInterval.current) } } }, [open]) 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]) const getScriptWebSocketUrl = (): string => { if (typeof window === "undefined") { return `ws://localhost:${API_PORT}/ws/script/${sessionId}` } const { hostname, protocol } = window.location const wsProtocol = protocol === "https:" ? "wss:" : "ws:" return `${wsProtocol}//${hostname}:${API_PORT}/ws/script/${sessionId}` } const wsUrl = getScriptWebSocketUrl() const handleWebSocketCreated = (ws: WebSocket) => { wsRef.current = ws setIsConnected(ws.readyState === WebSocket.OPEN) } const handleWebInteraction = (interaction: WebInteraction) => { setCurrentInteraction(interaction) } const handleInteractionResponse = (value: string) => { if (!wsRef.current || !currentInteraction) { return } if (value === "cancel" || value === "") { setCurrentInteraction(null) setInteractionInput("") handleCloseModal() return } const response = JSON.stringify({ type: "interaction_response", id: currentInteraction.id, value: value, }) if (wsRef.current.readyState === WebSocket.OPEN) { wsRef.current.send(response) } setCurrentInteraction(null) setInteractionInput("") } const handleCloseModal = () => { if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) { wsRef.current.close() } if (checkConnectionInterval.current) { clearInterval(checkConnectionInterval.current) } onClose() } return ( <> e.preventDefault()} onEscapeKeyDown={(e) => e.preventDefault()} > {title}
{isComplete ? ( exitCode === 0 ? ( ) : ( ) ) : ( )}

{title}

{description &&

{description}

}
{!isMobile && (
)}
{isConnected ? "Online" : "Offline"}
{currentInteraction && ( e.preventDefault()} onEscapeKeyDown={(e) => e.preventDefault()} hideClose > {currentInteraction.title}

{currentInteraction.message}

{currentInteraction.type === "yesno" && (
)} {currentInteraction.type === "menu" && currentInteraction.options && (
{currentInteraction.options.map((option) => ( ))}
)} {(currentInteraction.type === "input" || currentInteraction.type === "inputbox") && (
setInteractionInput(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") { handleInteractionResponse(interactionInput) } }} placeholder={currentInteraction.default || ""} />
)} {currentInteraction.type === "msgbox" && (
)}
)} ) }