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

247 lines
8.4 KiB
TypeScript
Raw Normal View History

2025-12-01 01:04:31 +01:00
"use client"
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-01 01:15:19 +01:00
import { X, CheckCircle2, XCircle, Loader2 } from "lucide-react"
2025-12-06 13:48:46 +01:00
import { TerminalPanel, type TerminalPanelHandle } from "./terminal-panel"
2025-12-01 01:40:04 +01:00
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
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:48:46 +01:00
const terminalRef = useRef<TerminalPanelHandle>(null)
2025-12-06 11:19:26 +01:00
2025-12-06 13:28:56 +01:00
useEffect(() => {
console.log("[v0] currentInteraction changed:", currentInteraction)
if (currentInteraction) {
console.log("[v0] Interaction opened, type:", currentInteraction.type, "id:", currentInteraction.id)
} else {
console.log("[v0] Interaction closed/cleared")
console.trace("[v0] Stack trace for currentInteraction = null")
}
}, [currentInteraction])
2025-12-06 11:54:36 +01:00
useEffect(() => {
if (open) {
console.log("[v0] ScriptTerminalModal opened with:", {
scriptPath,
scriptName,
params,
sessionId,
})
2025-12-06 13:38:19 +01:00
// Reset state only when modal opens
setIsComplete(false)
setExitCode(null)
2025-12-06 12:46:41 +01:00
setInteractionInput("")
2025-12-06 13:38:19 +01:00
// Don't clear currentInteraction here - it causes issues
} else {
// Clear interaction when modal closes
setCurrentInteraction(null)
2025-12-06 11:54:36 +01:00
}
2025-12-06 13:38:19 +01:00
}, [open]) // Only depend on 'open' to avoid unnecessary re-runs
2025-12-06 11:54:36 +01:00
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
const handleWebInteraction = (interaction: WebInteraction) => {
2025-12-06 13:28:56 +01:00
console.log("[v0] handleWebInteraction called with:", interaction)
2025-12-06 12:46:41 +01:00
setCurrentInteraction(interaction)
2025-12-06 13:38:19 +01:00
console.log("[v0] currentInteraction set to:", interaction.type, interaction.id)
2025-12-06 12:46:41 +01:00
}
2025-12-06 11:54:36 +01:00
2025-12-06 11:25:27 +01:00
const handleInteractionResponse = (value: string) => {
2025-12-06 13:28:56 +01:00
console.log("[v0] handleInteractionResponse called with value:", value)
if (!terminalRef.current || !currentInteraction) {
console.log("[v0] Cannot send response - no terminal ref or interaction")
return
}
2025-12-06 11:25:27 +01:00
const response = JSON.stringify({
type: "interaction_response",
id: currentInteraction.id,
value: value,
})
console.log("[v0] Sending interaction response:", response)
2025-12-06 12:46:41 +01:00
2025-12-06 13:48:46 +01:00
terminalRef.current.sendMessage(response)
console.log("[v0] Response sent successfully")
2025-12-06 12:46:41 +01:00
2025-12-06 13:28:56 +01:00
console.log("[v0] Clearing currentInteraction after response")
2025-12-06 11:25:27 +01:00
setCurrentInteraction(null)
setInteractionInput("")
}
2025-12-01 01:04:31 +01:00
return (
<>
<Dialog open={open} onOpenChange={onClose}>
2025-12-01 01:15:19 +01:00
<DialogContent className="max-w-4xl h-[80vh] p-0 flex flex-col">
2025-12-01 01:22:04 +01:00
<DialogTitle className="sr-only">{title}</DialogTitle>
2025-12-01 01:15:19 +01:00
{/* Header */}
<div className="flex items-center justify-between p-4 border-b">
<div className="flex items-center gap-2">
{isComplete ? (
exitCode === 0 ? (
<CheckCircle2 className="h-5 w-5 text-green-500" />
) : (
<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>}
</div>
</div>
<Button variant="ghost" size="icon" onClick={onClose}>
<X className="h-4 w-4" />
</Button>
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
ref={terminalRef}
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 11:30:49 +01:00
/>
2025-12-01 01:15:19 +01:00
</div>
2025-12-01 01:04:31 +01:00
2025-12-01 01:15:19 +01:00
{/* Footer */}
<div className="flex items-center justify-between p-4 border-t">
2025-12-01 01:40:04 +01:00
<div className="text-sm text-muted-foreground">Session ID: {sessionId}</div>
2025-12-01 01:15:19 +01:00
{isComplete && <Button onClick={onClose}>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:11:04 +01:00
<Dialog open={true} modal={true}>
<DialogContent
className="max-w-4xl max-h-[80vh] overflow-y-auto"
onInteractOutside={(e) => {
2025-12-06 13:28:56 +01:00
console.log("[v0] onInteractOutside triggered - preventing close")
2025-12-06 13:11:04 +01:00
e.preventDefault()
}}
onEscapeKeyDown={(e) => {
2025-12-06 13:28:56 +01:00
console.log("[v0] onEscapeKeyDown triggered - preventing close")
2025-12-06 13:11:04 +01:00
e.preventDefault()
}}
>
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"
className="w-full justify-start"
>
{option.label}
</Button>
))}
</div>
)}
2025-12-06 12:25:57 +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 || ""}
/>
<Button onClick={() => handleInteractionResponse(interactionInput)} className="w-full">
Submit
</Button>
</div>
))}
2025-12-06 11:25:27 +01:00
{currentInteraction.type === "msgbox" && (
<Button onClick={() => handleInteractionResponse("ok")} className="w-full">
OK
</Button>
)}
</div>
</DialogContent>
</Dialog>
)}
2025-12-01 01:04:31 +01:00
</>
)
}