From 8a1a7919278afef18ad69ad7c07a679ed9c5c05f Mon Sep 17 00:00:00 2001 From: "Moe." Date: Sat, 4 Jul 2026 23:47:53 +0800 Subject: [PATCH] feat(connection): add connection attempt cancellation --- .../connection/ConnectionDialog.vue | 6 +- .../src/components/sidebar/TreeItem.vue | 37 ++- apps/desktop/src/i18n/locales/en.ts | 2 + apps/desktop/src/i18n/locales/es.ts | 2 + apps/desktop/src/i18n/locales/it.ts | 2 + apps/desktop/src/i18n/locales/ja.ts | 2 + apps/desktop/src/i18n/locales/pt-BR.ts | 2 + apps/desktop/src/i18n/locales/zh-CN.ts | 2 + apps/desktop/src/i18n/locales/zh-TW.ts | 2 + apps/desktop/src/lib/backend/http.ts | 8 +- apps/desktop/src/lib/backend/tauri.ts | 8 +- .../__tests__/connectionStore.timeout.spec.ts | 215 +++++++++++++++++- apps/desktop/src/stores/connectionStore.ts | 211 ++++++++++++++++- crates/dbx-core/src/connection.rs | 93 +++++++- crates/dbx-web/src/routes/connection.rs | 79 ++++++- src-tauri/src/commands/connection.rs | 61 ++++- 16 files changed, 681 insertions(+), 51 deletions(-) diff --git a/apps/desktop/src/components/connection/ConnectionDialog.vue b/apps/desktop/src/components/connection/ConnectionDialog.vue index 1c5fa66781..fa51fcb870 100644 --- a/apps/desktop/src/components/connection/ConnectionDialog.vue +++ b/apps/desktop/src/components/connection/ConnectionDialog.vue @@ -16,7 +16,7 @@ import { Switch } from "@/components/ui/switch"; import type { ConnectionConfig, DatabaseType, HttpTunnelConfig, JdbcDriverInfo, JdbcMavenBundleInfo, ProxyTunnelConfig, SshTunnelConfig, TransportLayerConfig } from "@/types/database"; import type { MqAdminConfig, MqAuth, MqSystemKind } from "@/types/mq"; import type { NacosAdminConfig, NacosAuthConfig } from "@/types/nacos"; -import { useConnectionStore } from "@/stores/connectionStore"; +import { CONNECTION_ATTEMPT_CANCELLED_MESSAGE, useConnectionStore } from "@/stores/connectionStore"; import { REDIS_SCAN_PAGE_SIZE_DEFAULT, REDIS_SCAN_PAGE_SIZE_MIN, REDIS_SCAN_PAGE_SIZE_MAX, REDIS_SCAN_PAGE_SIZE_OPTIONS } from "@/lib/redis/redisKeyPattern"; import { useSettingsStore } from "@/stores/settingsStore"; import { useToast } from "@/composables/useToast"; @@ -2991,8 +2991,10 @@ async function save() { emit("connectSucceeded", config.name); }) .catch((e: any) => { + const message = String(e?.message || e); + if (message.includes(CONNECTION_ATTEMPT_CANCELLED_MESSAGE)) return; if (config.one_time) void store.removeConnection(config.id); - emit("connectFailed", mongodbAuthFailureHint(String(e?.message || e))); + emit("connectFailed", mongodbAuthFailureHint(message)); }); return; } diff --git a/apps/desktop/src/components/sidebar/TreeItem.vue b/apps/desktop/src/components/sidebar/TreeItem.vue index 0a3c41d718..f71e0e7dc0 100644 --- a/apps/desktop/src/components/sidebar/TreeItem.vue +++ b/apps/desktop/src/components/sidebar/TreeItem.vue @@ -58,9 +58,10 @@ import { Info, Archive, Square, + X, } from "@lucide/vue"; import CustomContextMenu from "@/components/ui/CustomContextMenu.vue"; -import { useConnectionStore } from "@/stores/connectionStore"; +import { CONNECTION_ATTEMPT_CANCELLED_MESSAGE, useConnectionStore } from "@/stores/connectionStore"; import { useQueryStore } from "@/stores/queryStore"; import { useSettingsStore } from "@/stores/settingsStore"; import { useSavedSqlStore } from "@/stores/savedSqlStore"; @@ -512,8 +513,7 @@ function isTooltipDisabled(): boolean { async function toggle() { const node = props.node; if (node.isLoading) { - if (node.type !== "connection") return; - node.isLoading = false; + return; } emit("search-toggle", node); const wasExpanded = !!node.isExpanded; @@ -651,6 +651,7 @@ async function toggle() { } catch (e: any) { if (!wasExpanded) node.isExpanded = false; const errMsg = e?.message || String(e); + if (errMsg.includes(CONNECTION_ATTEMPT_CANCELLED_MESSAGE)) return; toast(t("connection.connectFailed", { message: translateBackendError(t, errMsg) }), 5000); if (errMsg.includes("driver is not installed") || errMsg.includes("is not installed")) { window.dispatchEvent(new Event("dbx-open-driver-store")); @@ -3469,6 +3470,16 @@ async function disconnectConnection() { } } +async function cancelConnectionAttempt() { + if (!props.node.connectionId) return; + try { + const cancelled = await connectionStore.cancelConnecting(props.node.connectionId); + if (cancelled) toast(t("connection.connectCancelled"), 2000); + } catch (e: any) { + toast(t("connection.saveFailed", { message: e?.message || String(e) }), 5000); + } +} + async function closeDatabaseConnection() { const node = props.node; if (node.type !== "database" || !node.connectionId || node.database == null) return; @@ -3640,6 +3651,7 @@ const tableComment = computed(() => ); const paddingLeft = computed(() => treeItemPaddingLeft(props.depth)); const isConnected = computed(() => props.node.type === "connection" && !!props.node.connectionId && connectionStore.connectedIds.has(props.node.connectionId)); +const isConnecting = computed(() => props.node.type === "connection" && !!props.node.connectionId && connectionStore.connectingIds.has(props.node.connectionId)); const isConnectionReadonly = computed(() => props.node.type === "connection" && !!props.node.connectionId && (connectionStore.getConfig(props.node.connectionId)?.read_only ?? false)); const isOpenedDatabase = computed(() => isSidebarDatabaseOpened(props.node, connectionStore.isTreeNodeChildrenLoaded)); const showsDatabaseOpenIndicator = computed(() => props.node.type === "database" && (isOpenedDatabase.value || (!!props.node.connectionId && props.node.database != null && queryStore.isDatabaseOpen(props.node.connectionId, props.node.database)))); @@ -4095,7 +4107,9 @@ function treeItemMenuItems(): ContextMenuItem[] { // 2. Connection if (node.type === "connection") { - if (!isConnected.value) { + if (isConnecting.value) { + items.push({ label: t("connection.cancelConnecting"), action: cancelConnectionAttempt, icon: X }); + } else if (!isConnected.value) { items.push({ label: t("contextMenu.openConnection"), action: toggle, icon: Plug }); } else { items.push({ label: t("contextMenu.closeConnection"), action: disconnectConnection, icon: Unplug }); @@ -4712,11 +4726,22 @@ function treeItemMenuItems(): ContextMenuItem[] { {{ t("connection.readOnlyBadge") }}