From 7e08c8881eb5904bbfbffb45874dcba3da8b0e4f Mon Sep 17 00:00:00 2001 From: UndefinedPony Date: Sun, 22 Dec 2024 16:37:26 +0100 Subject: [PATCH] refactor: adapt terminal component to pass local server data and add initialized check --- .../settings/web-server/terminal.tsx | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/apps/dokploy/components/dashboard/settings/web-server/terminal.tsx b/apps/dokploy/components/dashboard/settings/web-server/terminal.tsx index d38f5d9e..b6757b9c 100644 --- a/apps/dokploy/components/dashboard/settings/web-server/terminal.tsx +++ b/apps/dokploy/components/dashboard/settings/web-server/terminal.tsx @@ -5,6 +5,7 @@ import { FitAddon } from "xterm-addon-fit"; import "@xterm/xterm/css/xterm.css"; import { AttachAddon } from "@xterm/addon-attach"; import { useTheme } from "next-themes"; +import { getLocalServerData } from "./local-server-config"; interface Props { id: string; @@ -12,9 +13,16 @@ interface Props { } export const Terminal: React.FC = ({ id, serverId }) => { - const termRef = useRef(null); + const termRef = useRef(null); + const initialized = useRef(false); const { resolvedTheme } = useTheme(); useEffect(() => { + if (initialized.current) { + // Required in strict mode to avoid issues due to double wss connection + return; + } + + initialized.current = true; const container = document.getElementById(id); if (container) { container.innerHTML = ""; @@ -33,7 +41,18 @@ export const Terminal: React.FC = ({ id, serverId }) => { const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; - const wsUrl = `${protocol}//${window.location.host}/terminal?serverId=${serverId}`; + const isLocalServer = serverId === "local"; + + const urlParams = new URLSearchParams(); + urlParams.set("serverId", serverId); + + if (isLocalServer) { + const { port, username } = getLocalServerData(); + urlParams.set("port", port.toString()); + urlParams.set("username", username); + } + + const wsUrl = `${protocol}//${window.location.host}/terminal?${urlParams}`; const ws = new WebSocket(wsUrl); const addonAttach = new AttachAddon(ws);