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

262 lines
9.1 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 22:40:24 +01:00
import { useState, 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 22:40:24 +01:00
import { CheckCircle2, XCircle, GripHorizontal } from "lucide-react"
import { TerminalPanel } from "./terminal-panel"
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 [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 18:36:34 +01:00
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 22:40:24 +01:00
const initMessage = {
script_path: scriptPath,
params: {
EXECUTION_MODE: "web",
...params,
},
2025-12-01 01:15:19 +01:00
}
2025-12-06 11:25:27 +01:00
const handleInteractionResponse = (value: string) => {
2025-12-06 19:03:19 +01:00
if (value === "cancel" || value === "") {
setCurrentInteraction(null)
setInteractionInput("")
2025-12-06 22:40:24 +01:00
onClose()
2025-12-06 19:03:19 +01:00
return
}
2025-12-06 22:40:24 +01:00
// TerminalPanel manejará el envío de la respuesta
2025-12-06 11:25:27 +01:00
setCurrentInteraction(null)
setInteractionInput("")
2025-12-06 18:36:34 +01:00
}
2025-12-06 22:20:34 +01:00
const handleResizeStart = (e: React.MouseEvent | React.TouchEvent) => {
setIsResizing(true)
startYRef.current = "clientY" in e ? e.clientY : e.touches[0].clientY
startHeightRef.current = modalHeight
document.addEventListener("mousemove", handleResize as any)
document.addEventListener("touchmove", handleResize as any)
document.addEventListener("mouseup", handleResizeEnd)
document.addEventListener("touchend", handleResizeEnd)
}
const handleResize = (e: MouseEvent | TouchEvent) => {
if (!isResizing) return
const currentY = e instanceof MouseEvent ? e.clientY : e.touches[0].clientY
const deltaY = currentY - startYRef.current
const newHeight = startHeightRef.current + (deltaY / window.innerHeight) * 100
setModalHeight(Math.max(50, Math.min(95, newHeight)))
}
const handleResizeEnd = () => {
setIsResizing(false)
document.removeEventListener("mousemove", handleResize as any)
document.removeEventListener("touchmove", handleResize as any)
document.removeEventListener("mouseup", handleResizeEnd)
document.removeEventListener("touchend", handleResizeEnd)
}
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">
2025-12-06 20:46:13 +01:00
{isComplete &&
(exitCode === 0 ? (
2025-12-06 18:36:34 +01:00
<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" />
2025-12-06 20:46:13 +01:00
))}
2025-12-06 18:36:34 +01:00
<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-06 22:40:24 +01:00
<div className="flex-1 overflow-hidden">
<TerminalPanel
isScriptModal={true}
initMessage={initMessage}
onWebInteraction={(interaction) => {
setCurrentInteraction({
type: interaction.type as any,
id: interaction.id,
title: interaction.title || "",
message: interaction.message || "",
options: interaction.options,
default: interaction.default,
})
}}
onClose={onClose}
/>
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
2025-12-06 19:03:19 +01:00
className={`h-2 cursor-ns-resize flex items-center justify-center transition-colors ${
isResizing ? "bg-blue-500" : "bg-zinc-800 hover:bg-blue-500/50"
}`}
2025-12-06 18:36:34 +01:00
onMouseDown={handleResizeStart}
onTouchStart={handleResizeStart}
>
2025-12-06 19:03:19 +01:00
<GripHorizontal className={`h-4 w-4 ${isResizing ? "text-white" : "text-zinc-500"}`} />
2025-12-06 18:36:34 +01:00
</div>
)}
2025-12-01 01:04:31 +01:00
</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 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-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 19:03:19 +01:00
<Button
onClick={() => handleInteractionResponse("cancel")}
variant="outline"
className="flex-1 hover:bg-red-600 hover:text-white hover:border-red-600"
>
Cancel
</Button>
2025-12-06 11:25:27 +01:00
</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"
>
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
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
2025-12-06 19:03:19 +01:00
onClick={() => handleInteractionResponse("cancel")}
2025-12-06 18:36:34 +01:00
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 19:03:19 +01:00
<div className="flex gap-2">
<Button
onClick={() => handleInteractionResponse("ok")}
className="flex-1 bg-blue-600 hover:bg-blue-700"
>
OK
</Button>
<Button
onClick={() => handleInteractionResponse("cancel")}
variant="outline"
className="flex-1 hover:bg-red-600 hover:text-white hover:border-red-600"
>
Cancel
</Button>
</div>
2025-12-06 11:25:27 +01:00
)}
</div>
</DialogContent>
</Dialog>
)}
2025-12-01 01:04:31 +01:00
</>
)
}