Update script-terminal-modal.tsx

This commit is contained in:
MacRimi
2025-12-10 18:36:31 +01:00
parent c62dd2014e
commit d3beb72652

View File

@@ -8,7 +8,6 @@ import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label" import { Label } from "@/components/ui/label"
import { Loader2, Activity, GripHorizontal } from "lucide-react" import { Loader2, Activity, GripHorizontal } from "lucide-react"
import { API_PORT } from "../lib/api-config" import { API_PORT } from "../lib/api-config"
import { fetchApi } from "@/lib/api-config"
import { useIsMobile } from "@/hooks/use-mobile" import { useIsMobile } from "@/hooks/use-mobile"
interface WebInteraction { interface WebInteraction {
@@ -58,17 +57,38 @@ export function ScriptTerminalModal({
const [modalHeight, setModalHeight] = useState(600) const [modalHeight, setModalHeight] = useState(600)
const [isResizing, setIsResizing] = useState(false) const [isResizing, setIsResizing] = useState(false)
const resizeHandlersRef = useRef<{ const resizeHandlersRef = useRef<{
handleMove: ((e: MouseEvent | TouchEvent) => void) | null handleMouseMove: ((e: MouseEvent) => void) | null
handleEnd: (() => void) | null handleMouseUp: (() => void) | null
}>({ handleMove: null, handleEnd: null }) handleTouchMove: ((e: TouchEvent) => void) | null
handleTouchEnd: (() => void) | null
}>({
handleMouseMove: null,
handleMouseUp: null,
handleTouchMove: null,
handleTouchEnd: null,
})
const terminalContainerRef = useCallback( const terminalContainerRef = useRef<HTMLDivElement>(null)
(node: HTMLDivElement | null) => {
if (!node || !isOpen || termRef.current) { const sendKey = useCallback((key: string) => {
return if (!termRef.current) return
const keyMap: Record<string, string> = {
escape: "\x1b",
tab: "\t",
up: "\x1b[A",
down: "\x1b[B",
left: "\x1b[D",
right: "\x1b[C",
enter: "\r",
ctrlc: "\x03",
} }
console.log("[v0] Terminal container mounted, initializing...") const sequence = keyMap[key]
if (sequence && wsRef.current?.readyState === WebSocket.OPEN) {
wsRef.current.send(JSON.stringify({ type: "input", data: sequence }))
}
}, [])
const initializeTerminal = async () => { const initializeTerminal = async () => {
console.log("[v0] Loading xterm modules...") console.log("[v0] Loading xterm modules...")
@@ -119,7 +139,9 @@ export function ScriptTerminalModal({
const fitAddon = new FitAddonClass() const fitAddon = new FitAddonClass()
term.loadAddon(fitAddon) term.loadAddon(fitAddon)
console.log("[v0] Opening terminal in container...") console.log("[v0] Opening terminal in container...")
term.open(node) if (terminalContainerRef.current) {
term.open(terminalContainerRef.current)
}
termRef.current = term termRef.current = term
fitAddonRef.current = fitAddon fitAddonRef.current = fitAddon
@@ -262,13 +284,10 @@ export function ScriptTerminalModal({
}, 100) }, 100)
}) })
resizeObserver.observe(node) if (terminalContainerRef.current) {
resizeObserver.observe(terminalContainerRef.current)
}
} }
initializeTerminal()
},
[isOpen, scriptPath, params],
)
useEffect(() => { useEffect(() => {
const savedHeight = localStorage.getItem("scriptModalHeight") const savedHeight = localStorage.getItem("scriptModalHeight")
@@ -276,7 +295,9 @@ export function ScriptTerminalModal({
setModalHeight(Number.parseInt(savedHeight, 10)) setModalHeight(Number.parseInt(savedHeight, 10))
} }
if (!isOpen) { if (isOpen) {
initializeTerminal()
} else {
if (checkConnectionInterval.current) { if (checkConnectionInterval.current) {
clearInterval(checkConnectionInterval.current) clearInterval(checkConnectionInterval.current)
} }
@@ -291,15 +312,24 @@ export function ScriptTerminalModal({
termRef.current.dispose() termRef.current.dispose()
termRef.current = null termRef.current = null
} }
if (resizeHandlersRef.current.handleMove) { if (resizeHandlersRef.current.handleMouseMove) {
document.removeEventListener("mousemove", resizeHandlersRef.current.handleMove as any) document.removeEventListener("mousemove", resizeHandlersRef.current.handleMouseMove)
document.removeEventListener("touchmove", resizeHandlersRef.current.handleMove as any)
} }
if (resizeHandlersRef.current.handleEnd) { if (resizeHandlersRef.current.handleMouseUp) {
document.removeEventListener("mouseup", resizeHandlersRef.current.handleEnd) document.removeEventListener("mouseup", resizeHandlersRef.current.handleMouseUp)
document.removeEventListener("touchend", resizeHandlersRef.current.handleEnd) }
if (resizeHandlersRef.current.handleTouchMove) {
document.removeEventListener("touchmove", resizeHandlersRef.current.handleTouchMove)
}
if (resizeHandlersRef.current.handleTouchEnd) {
document.removeEventListener("touchend", resizeHandlersRef.current.handleTouchEnd)
}
resizeHandlersRef.current = {
handleMouseMove: null,
handleMouseUp: null,
handleTouchMove: null,
handleTouchEnd: null,
} }
resizeHandlersRef.current = { handleMove: null, handleEnd: null }
sessionIdRef.current = Math.random().toString(36).substring(2, 8) sessionIdRef.current = Math.random().toString(36).substring(2, 8)
setIsComplete(false) setIsComplete(false)
@@ -316,10 +346,18 @@ export function ScriptTerminalModal({
return `ws://localhost:${API_PORT}/ws/script/${sid}` return `ws://localhost:${API_PORT}/ws/script/${sid}`
} }
const { hostname, protocol } = window.location const { protocol, hostname, port } = window.location
const isStandardPort = port === "" || port === "80" || port === "443"
const wsProtocol = protocol === "https:" ? "wss:" : "ws:" const wsProtocol = protocol === "https:" ? "wss:" : "ws:"
if (isStandardPort) {
// When using standard port (proxy scenario), don't add port number
return `${wsProtocol}//${hostname}/ws/script/${sid}`
} else {
// Development or custom port, use API_PORT
return `${wsProtocol}//${hostname}:${API_PORT}/ws/script/${sid}` return `${wsProtocol}//${hostname}:${API_PORT}/ws/script/${sid}`
} }
}
const handleInteractionResponse = (value: string) => { const handleInteractionResponse = (value: string) => {
if (!wsRef.current || !currentInteraction) { if (!wsRef.current || !currentInteraction) {
@@ -419,15 +457,25 @@ export function ScriptTerminalModal({
} }
} }
document.removeEventListener("mousemove", handleMove as any) document.removeEventListener("mousemove", handleMove)
document.removeEventListener("touchmove", handleMove as any) document.removeEventListener("touchmove", handleMove)
document.removeEventListener("mouseup", handleEnd) document.removeEventListener("mouseup", handleEnd)
document.removeEventListener("touchend", handleEnd) document.removeEventListener("touchend", handleEnd)
resizeHandlersRef.current = { handleMove: null, handleEnd: null } resizeHandlersRef.current = {
handleMouseMove: null,
handleMouseUp: null,
handleTouchMove: null,
handleTouchEnd: null,
}
} }
resizeHandlersRef.current = { handleMove, handleEnd } resizeHandlersRef.current = {
handleMouseMove: handleMove as any,
handleMouseUp: handleEnd,
handleTouchMove: handleMove as any,
handleTouchEnd: handleEnd,
}
document.addEventListener("mousemove", handleMove as any) document.addEventListener("mousemove", handleMove as any)
document.addEventListener("touchmove", handleMove as any, { passive: false }) document.addEventListener("touchmove", handleMove as any, { passive: false })
@@ -466,6 +514,77 @@ export function ScriptTerminalModal({
)} )}
</div> </div>
{isMobile && (
<div className="border-t bg-zinc-900/50 px-1 py-2 overflow-x-auto">
<div className="flex gap-1.5 justify-center min-w-max">
<Button
onClick={() => sendKey("escape")}
variant="outline"
size="sm"
className="px-2.5 py-2 text-xs h-9 bg-zinc-800 hover:bg-zinc-700 border-zinc-700"
>
ESC
</Button>
<Button
onClick={() => sendKey("tab")}
variant="outline"
size="sm"
className="px-2.5 py-2 text-xs h-9 bg-zinc-800 hover:bg-zinc-700 border-zinc-700"
>
TAB
</Button>
<Button
onClick={() => sendKey("up")}
variant="outline"
size="sm"
className="px-3 py-2 text-base h-9 bg-zinc-800 hover:bg-zinc-700 border-zinc-700"
>
</Button>
<Button
onClick={() => sendKey("down")}
variant="outline"
size="sm"
className="px-3 py-2 text-base h-9 bg-zinc-800 hover:bg-zinc-700 border-zinc-700"
>
</Button>
<Button
onClick={() => sendKey("left")}
variant="outline"
size="sm"
className="px-3 py-2 text-base h-9 bg-zinc-800 hover:bg-zinc-700 border-zinc-700"
>
</Button>
<Button
onClick={() => sendKey("right")}
variant="outline"
size="sm"
className="px-3 py-2 text-base h-9 bg-zinc-800 hover:bg-zinc-700 border-zinc-700"
>
</Button>
<Button
onClick={() => sendKey("enter")}
variant="outline"
size="sm"
className="px-3 py-2 text-base h-9 bg-zinc-800 hover:bg-zinc-700 border-zinc-700"
>
</Button>
<Button
onClick={() => sendKey("ctrlc")}
variant="outline"
size="sm"
className="px-2 py-2 text-xs h-9 bg-zinc-800 hover:bg-zinc-700 border-zinc-700"
>
CTRL+C
</Button>
</div>
</div>
)}
{!isMobile && ( {!isMobile && (
<div <div
className={`h-2 cursor-ns-resize flex items-center justify-center transition-all duration-150 ${ className={`h-2 cursor-ns-resize flex items-center justify-center transition-all duration-150 ${