From aadb278e5fa2e0f5903f8df395c3ed79e5dd72fd Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 5 Oct 2025 00:45:07 -0600 Subject: [PATCH] refactor: simplify WebSocket connection logic in DockerTerminal component - Removed redundant checks for containerId before establishing WebSocket connection. - Streamlined the connection setup and added the AttachAddon directly after the terminal is opened. - Updated UI text to clarify the connection method. --- .../docker/terminal/docker-terminal.tsx | 47 ++++++------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/apps/dokploy/components/dashboard/docker/terminal/docker-terminal.tsx b/apps/dokploy/components/dashboard/docker/terminal/docker-terminal.tsx index d150774e7..842509bc0 100644 --- a/apps/dokploy/components/dashboard/docker/terminal/docker-terminal.tsx +++ b/apps/dokploy/components/dashboard/docker/terminal/docker-terminal.tsx @@ -4,7 +4,6 @@ import { FitAddon } from "xterm-addon-fit"; import "@xterm/xterm/css/xterm.css"; import { AttachAddon } from "@xterm/addon-attach"; import { useTheme } from "next-themes"; -import { Label } from "@/components/ui/label"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; interface Props { @@ -38,48 +37,30 @@ export const DockerTerminal: React.FC = ({ }, }); const addonFit = new FitAddon(); + const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; + + const wsUrl = `${protocol}//${window.location.host}/docker-container-terminal?containerId=${containerId}&activeWay=${activeWay}${serverId ? `&serverId=${serverId}` : ""}`; + + const ws = new WebSocket(wsUrl); + + const addonAttach = new AttachAddon(ws); // @ts-ignore term.open(termRef.current); // @ts-ignore term.loadAddon(addonFit); + term.loadAddon(addonAttach); addonFit.fit(); - - // only connect if containerId is provided - if ( - containerId && - containerId !== "" && - containerId !== "select-a-container" - ) { - const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; - - const wsUrl = `${protocol}//${window.location.host}/docker-container-terminal?containerId=${containerId}&activeWay=${activeWay}${serverId ? `&serverId=${serverId}` : ""}`; - - const ws = new WebSocket(wsUrl); - - const addonAttach = new AttachAddon(ws); - - term.loadAddon(addonAttach); - - ws.onopen = () => { - setIsConnected(true); - }; - - ws.onclose = () => { - setIsConnected(false); - }; - - return () => { - ws.readyState === WebSocket.OPEN && ws.close(); - }; - } + return () => { + ws.readyState === WebSocket.OPEN && ws.close(); + }; }, [containerId, activeWay, id]); return (
- + + Select way to connect to {containerId} + Bash