Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(editor): add connection detail hover preview to query toolbar

hanxuanyu72142da3c4cb
diff --git a/apps/desktop/src/components/connection/ConnectionDetailContent.vue b/apps/desktop/src/components/connection/ConnectionDetailContent.vue
new file mode 100644
index 0000000000..def3004dc3
--- /dev/null
+++ b/apps/desktop/src/components/connection/ConnectionDetailContent.vue
@@ -0,0 +1,32 @@
+<script setup lang="ts">
+import type { ConnectionDetailRow } from "@/lib/connection/connectionDetailRows";
+
+defineProps<{ rows: ConnectionDetailRow[] }>();
+</script>
+
+<template>
+ <div class="w-max min-w-40 max-w-[min(28rem,calc(100vw-24px))] rounded-md border border-border bg-popover p-2 text-popover-foreground shadow-lg">
+ <div class="space-y-1">
+ <div v-for="row in rows" :key="row.label" class="grid grid-cols-[max-content_minmax(0,1fr)] gap-2 text-xs leading-5">
+ <span class="shrink-0 text-muted-foreground">{{ row.label }}</span>
+ <template v-if="row.values">
+ <div class="flex flex-col gap-0.5 font-mono text-foreground/90">
+ <span v-for="(value, index) in row.values" :key="index" class="break-all">{{ value }}</span>
+ </div>
+ </template>
+ <button
+ v-else-if="row.action"
+ type="button"
+ class="w-fit rounded bg-primary/10 px-1 font-mono text-primary underline-offset-2 hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
+ :aria-label="row.actionLabel"
+ :title="row.actionLabel"
+ @click.stop="row.action()"
+ >
+ {{ row.value }}
+ </button>
+ <span v-else-if="row.multiline" class="max-h-20 overflow-hidden whitespace-pre-wrap break-words text-foreground/90">{{ row.value }}</span>
+ <span v-else class="truncate font-mono text-foreground/90" :title="row.value">{{ row.value }}</span>
+ </div>
+ </div>
+ </div>
+</template>
diff --git a/apps/desktop/src/components/connection/ConnectionTreeSelect.vue b/apps/desktop/src/components/connection/ConnectionTreeSelect.vue
index 5e62b140db..245873c092 100644
--- a/apps/desktop/src/components/connection/ConnectionTreeSelect.vue
+++ b/apps/desktop/src/components/connection/ConnectionTreeSelect.vue
@@ -22,9 +22,11 @@ const props = withDefaults(
triggerClass?: HTMLAttributes["class"];
triggerIconClass?: HTMLAttributes["class"];
listClass?: HTMLAttributes["class"];
+ hideTitle?: boolean;
}>(),
{
disabled: false,
+ hideTitle: false,
triggerIconClass: "size-4 text-muted-foreground",
},
);
@@ -154,7 +156,7 @@ defineExpose({
<template>
<Popover v-model:open="open">
<PopoverTrigger as-child>
- <Button type="button" variant="ghost" :disabled="disabled" :title="selectedLabel" :class="cn('h-6 w-auto max-w-56 min-w-0 justify-between gap-1 border-0 bg-transparent px-1 text-xs font-normal shadow-none hover:bg-muted/50 focus-visible:ring-0', triggerClass)">
+ <Button type="button" variant="ghost" :disabled="disabled" :title="hideTitle ? undefined : selectedLabel" :class="cn('h-6 w-auto max-w-56 min-w-0 justify-between gap-1 border-0 bg-transparent px-1 text-xs font-normal shadow-none hover:bg-muted/50 focus-visible:ring-0', triggerClass)">
<slot name="trigger-label" :value="modelValue" :label="selectedLabel">
<span class="truncate">{{ selectedLabel }}</span>
</slot>
diff --git a/apps/desktop/src/components/layout/EditorToolbar.vue b/apps/desktop/src/components/layout/EditorToolbar.vue
index fd090141bc..006fc47af4 100644
--- a/apps/desktop/src/components/layout/EditorToolbar.vue
+++ b/apps/desktop/src/components/layout/EditorToolbar.vue
@@ -38,9 +38,11 @@ import { Button } from "@/components/ui/button";
import { SearchableSelect } from "@/components/ui/searchable-select";
import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
import { Tooltip, TooltipTrigger, TooltipContent } from "@/components/ui/tooltip";
+import LightTooltip from "@/components/ui/LightTooltip.vue";
import TruncatedTextTooltip from "@/components/ui/TruncatedTextTooltip.vue";
import DatabaseIcon from "@/components/icons/DatabaseIcon.vue";
import ConnectionTreeSelect from "@/components/connection/ConnectionTreeSelect.vue";
+import ConnectionDetailContent from "@/components/connection/ConnectionDetailContent.vue";
import ProductionContextBadge from "@/components/common/ProductionContextBadge.vue";
import { useConnectionStore } from "@/stores/connectionStore";
import { useQueryStore } from "@/stores/queryStore";
@@ -61,6 +63,8 @@ import { looksLikeDmlStatement } from "@/lib/sql/dmlChangePreview";
import { canFormatSqlForDatabaseType } from "@/lib/sql/sqlFormatter";
import type { QueryTab, ConnectionConfig } from "@/types/database";
import { isObjectDependencySection } from "@/lib/database/objectDependencyDetails";
+import { connectionDetailRows } from "@/lib/connection/connectionDetailRows";
+import { findConnectionGroupPath } from "@/lib/sidebar/sidebarLayout";
const props = defineProps<{
activeTab: QueryTab;
@@ -253,6 +257,12 @@ const activeProductionContext = computed(() => productionContextForDatabase(prop
const showConnectionProductionBadge = computed(() => activeProductionContext.value.reason === "connection");
const showDatabaseProductionBadge = computed(() => activeProductionContext.value.reason === "database");
const activeConnectionValue = computed(() => props.activeConnection?.id || "");
+const activeConnectionDetailRows = computed(() => (props.activeConnection ? connectionDetailRows(props.activeConnection, t) : []));
+const activeConnectionGroupPath = computed(() => {
+ const connection = props.activeConnection;
+ if (!connection) return "";
+ return findConnectionGroupPath(connectionStore.sidebarLayout, connection.id)?.join(" / ") || "";
+});
const activeSchemaValue = computed(() => props.activeTab.schema || "");
// Salesforce identity badge. A Salesforce tab talks to one live org and every grid
// save is a real REST write, so the signed-in user stays visible next to the
@@ -991,28 +1001,33 @@ defineExpose({
<div class="flex shrink-0 items-center gap-2">
<div class="flex shrink-0 items-center gap-1" :class="{ 'connection-required-prompt': connectionRequiredVisible }">
<span v-if="activeConnection?.color" class="h-4 w-1 rounded-full shrink-0" :style="{ backgroundColor: activeConnection.color }" />
- <ConnectionTreeSelect
- ref="connectionPickerRef"
- :model-value="activeConnectionValue"
- :connections="connectionStore.connections"
- :layout="connectionStore.sidebarLayout"
- :placeholder="t('editor.selectConnection')"
- :search-placeholder="t('editor.searchConnection')"
- :empty-text="t('grid.noSearchResults')"
- trigger-class="font-medium text-foreground"
- trigger-icon-class="h-3 w-3"
- list-class="w-96 max-w-[calc(100vw-2rem)]"
- @update:model-value="(connectionId) => emit('changeConnection', connectionId)"
- >
- <template #trigger-label="{ label }">
- <div v-if="activeConnection" class="flex min-w-0 items-center gap-1.5">
- <DatabaseIcon :db-type="connectionIconType(activeConnection)" class="h-3.5 w-3.5 shrink-0" />
- <span class="truncate">{{ label }}</span>
- <ProductionContextBadge v-if="showConnectionProductionBadge" compact />
- </div>
- <span v-else class="truncate text-muted-foreground">{{ t("editor.selectConnection") }}</span>
- </template>
- </ConnectionTreeSelect>
+ <LightTooltip text="" :disabled="!activeConnectionDetailRows.length" side="bottom" :side-offset="6" :delay="250" :close-delay="80" surface="popover">
+ <ConnectionTreeSelect
+ ref="connectionPickerRef"
+ :model-value="activeConnectionValue"
+ :connections="connectionStore.connections"
+ :layout="connectionStore.sidebarLayout"
+ :placeholder="t('editor.selectConnection')"
+ :search-placeholder="t('editor.searchConnection')"
+ :empty-text="t('grid.noSearchResults')"
+ :hide-title="activeConnectionDetailRows.length > 0"
+ trigger-class="font-medium text-foreground"
+ trigger-icon-class="h-3 w-3"
+ list-class="w-96 max-w-[calc(100vw-2rem)]"
+ @update:model-value="(connectionId) => emit('changeConnection', connectionId)"
+ >
+ <template #trigger-label="{ label }">
+ <div v-if="activeConnection" class="flex min-w-0 items-center gap-1.5">
+ <DatabaseIcon :db-type="connectionIconType(activeConnection)" class="h-3.5 w-3.5 shrink-0" />
+ <span v-if="activeConnectionGroupPath" class="shrink-0 text-muted-foreground">{{ activeConnectionGroupPath }} /</span>
+ <span class="truncate">{{ label }}</span>
+ <ProductionContextBadge v-if="showConnectionProductionBadge" compact />
+ </div>
+ <span v-else class="truncate text-muted-foreground">{{ t("editor.selectConnection") }}</span>
+ </template>
+ </ConnectionTreeSelect>
+ <template #content><ConnectionDetailContent :rows="activeConnectionDetailRows" /></template>
+ </LightTooltip>
</div>
<div v-if="showCatalogSelector" class="flex shrink-0 items-center gap-1">
<SearchableSelect
diff --git a/apps/desktop/src/components/sidebar/TreeItem.vue b/apps/desktop/src/components/sidebar/TreeItem.vue
index 3e099972aa..7543220ba9 100644
--- a/apps/desktop/src/components/sidebar/TreeItem.vue
+++ b/apps/desktop/src/components/sidebar/TreeItem.vue
@@ -49,14 +49,14 @@ import { useToast } from "@/composables/useToast";
import DatabaseIcon from "@/components/icons/DatabaseIcon.vue";
import PluginIcon from "@/components/plugins/PluginIcon.vue";
import ConnectionErrorIndicator from "@/components/connection/ConnectionErrorIndicator.vue";
+import ConnectionDetailContent from "@/components/connection/ConnectionDetailContent.vue";
import ReadOnlySessionControl from "@/components/connection/ReadOnlySessionControl.vue";
import ProductionContextBadge from "@/components/common/ProductionContextBadge.vue";
import { Badge } from "@/components/ui/badge";
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
import LightTooltip from "@/components/ui/LightTooltip.vue";
-import { encodeOceanBaseUrlUsername } from "@/lib/connection/connectionUrlBuilder";
-import type { ColumnInfo, ConnectionConfig, CustomTypeTreeMemberMeta, DatabaseType, TreeNode, TriggerInfo } from "@/types/database";
+import type { ColumnInfo, CustomTypeTreeMemberMeta, DatabaseType, TreeNode, TriggerInfo } from "@/types/database";
import { alignedCommentLeadingWidth, canTreeNodePin, canTreeNodeShowExpander, isSidebarCommentSupportedType, sidebarTreeNodeComment, trailingCommentAvailableWidth, trailingCommentGapPx, treeItemPaddingLeft, treeLabelWidthClass, usesFullWidthTreeLabel } from "@/lib/sidebar/sidebarTreeItemLayout";
import {
clearActiveTableReferencePayload,
@@ -72,16 +72,13 @@ import {
import { AI_ASSISTANT_TABLE_DROP_ROOT_SELECTOR } from "@/lib/ai/aiTableReferenceDrop";
import { beginTableReferenceDragFeedback, isOverSqlEditorTarget, type TableReferenceDragFeedback } from "@/lib/editor/tableReferenceDragFeedback";
import { formatSidebarObjectStorage } from "@/lib/sidebar/sidebarDatabaseStorage";
-import { effectiveRedisDatabaseIndex } from "@/lib/redis/redisDatabaseIndex";
import { dataTabOpenModeFromTreeClick } from "@/lib/sidebar/dataTabOpenPolicy";
import { effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect";
import { isTableVGroupGroupableRowType, selectedTableVGroupMoveTargets, tableVGroupIdFromNodeId } from "@/lib/table/tableVGroup";
import { findTreeNodeById } from "@/lib/sql/newQueryContext";
import { resolveTableVGroupDropTarget, setTableVGroupDropTargetNodeId, tableVGroupDropTargetNodeId } from "@/lib/sidebar/sidebarTableVGroupDrag";
-import { connectionDisplayUrlScheme } from "@/lib/connection/connectionPresentation";
-import { redactConnectionStringSecrets } from "@/lib/connection/connectionStringRedaction";
import { isExecuteSqlShortcut, isFocusSearchShortcut } from "@/lib/editor/keyboardShortcuts";
-import { encodeSpannerResourcePath } from "@/lib/connection/spannerResourcePath";
+import { connectionDetailRows, type ConnectionDetailRow } from "@/lib/connection/connectionDetailRows";
import { hexToRgba } from "@/lib/common/color";
import { sidebarDisplayTableName } from "@/lib/sidebar/sidebarTableNameDisplay";
import { shouldMeasureSidebarLabelOverflow } from "@/lib/sidebar/sidebarLabelTooltip";
@@ -504,16 +501,6 @@ function hasActiveObjectNameFilter(node: TreeNode): boolean {
return !!filter && (filter.includePatterns.length > 0 || filter.excludePatterns.length > 0);
}
-type DetailTooltipRow = {
- label: string;
- value: string;
- multiline?: boolean;
- /** When set, renders each value on its own line (e.g. one host per line) */
- values?: string[];
- action?: () => void;
- actionLabel?: string;
-};
-
function cleanTooltipValue(value: string | number | null | undefined): string {
return String(value ?? "").trim();
}
@@ -534,63 +521,13 @@ function formatXuguStorageDetailValue(key: string, value: string): string {
return value;
}
-function isLocalFileConnection(config: Pick<ConnectionConfig, "db_type" | "port">): boolean {
- return config.db_type === "sqlite" || config.db_type === "duckdb" || config.db_type === "access" || (config.db_type === "h2" && config.port === 0);
-}
-
-function hostForDisplay(host: string): string {
- if (!host.includes(":") || host.startsWith("[") || host.includes("://") || host.includes(",")) return host;
- return `[${host}]`;
-}
-
-// A Redis database is a numeric index; dirty stored values (e.g. redis-cli flags
-// pasted into the field) resolve to the index the backend actually connects with.
-function tooltipDatabaseValue(config: ConnectionConfig): string {
- const database = cleanTooltipValue(config.database);
- return config.db_type === "redis" && database ? effectiveRedisDatabaseIndex(database) : database;
-}
-
-function connectionTooltipUrl(config: ConnectionConfig): string {
- const explicit = cleanTooltipValue(config.connection_string);
- if (explicit) return redactConnectionStringSecrets(explicit);
-
- const host = cleanTooltipValue(config.host);
- if (!host) return "";
- if (host.includes("://")) return redactConnectionStringSecrets(host);
-
- if (isLocalFileConnection(config)) {
- if (config.db_type === "access") return `jdbc:ucanaccess://${host}`;
- return `${config.db_type}://${host}`;
- }
-
- const scheme = connectionDisplayUrlScheme(config);
- const port = Number(config.port) > 0 ? `:${config.port}` : "";
- const user = cleanTooltipValue(config.username);
- const encodedUser = config.db_type === "mysql" && config.driver_profile?.trim().toLowerCase() === "oceanbase" ? encodeOceanBaseUrlUsername(user) : encodeURIComponent(user).replace(/%40/g, "@");
- const userInfo = user ? `${encodedUser}@` : "";
- const database = tooltipDatabaseValue(config);
- const encodedDatabase = config.db_type === "spanner" ? encodeSpannerResourcePath(database) : encodeURIComponent(database);
- const path = database ? `/${encodedDatabase}` : "";
- const params = cleanTooltipValue(config.url_params);
- const query = params ? (params.startsWith("?") ? params : `?${params}`) : "";
- return redactConnectionStringSecrets(`${scheme}://${userInfo}${hostForDisplay(host)}${port}${path}${query}`);
-}
-
const detailTooltip = computed(() => {
const node = activeNode.value;
if (node.type === "connection" && node.connectionId) {
const config = connectionStore.getConfig(node.connectionId);
if (!config) return null;
- const hostLabel = isLocalFileConnection(config) ? t("connection.filePath") : t("connection.host");
- const hostValue = cleanTooltipValue(config.host);
- const hostValues = hostValue.includes(",")
- ? hostValue
- .split(",")
- .map((h) => h.trim())
- .filter(Boolean)
- : [];
const visibleFilterSummary = connectionCanConfigureSidebarVisibleDatabases(config.db_type) || config.db_type === "nacos" ? connectionStore.getSidebarVisibleFilterSummary(node.connectionId) : null;
- const visibleFilterRow: DetailTooltipRow | null =
+ const visibleFilterRow: ConnectionDetailRow | null =
visibleFilterSummary?.selected != null && visibleFilterSummary.total != null
? {
label: t(visibleFilterSummary.mode === "namespace" ? "nacos.nacosVisibleNamespacesDetailLabel" : visibleFilterSummary.mode === "schema" ? "visibleSchemas.detailLabel" : "visibleDatabases.detailLabel"),
@@ -599,25 +536,14 @@ const detailTooltip = computed(() => {
actionLabel: t(visibleFilterSummary.mode === "namespace" ? "nacos.nacosVisibleNamespacesDetailActionLabel" : visibleFilterSummary.mode === "schema" ? "visibleSchemas.detailActionLabel" : "visibleDatabases.detailActionLabel", { connection: config.name }),
}
: null;
- const rows: DetailTooltipRow[] = [
- { label: t("connection.name"), value: cleanTooltipValue(config.name) },
- { label: "URL", value: connectionTooltipUrl(config), multiline: true },
- ...(hostValues.length > 0 ? [{ label: hostLabel, value: hostValues[0], values: hostValues } as DetailTooltipRow] : [{ label: hostLabel, value: hostValue, multiline: isLocalFileConnection(config) } as DetailTooltipRow]),
- { label: "Port", value: Number(config.port) > 0 ? String(config.port) : "" },
- { label: t("connection.database"), value: tooltipDatabaseValue(config) },
- { label: t("connection.user"), value: cleanTooltipValue(config.username) },
- { label: t("connection.type"), value: config.driver_label || config.driver_profile || config.db_type },
- { label: t("connection.databaseInfo.productVersion"), value: cleanTooltipValue(config.database_info?.productVersion) },
- ...(visibleFilterRow ? [visibleFilterRow] : []),
- { label: t("connection.note"), value: cleanTooltipValue(config.note), multiline: true },
- ].filter((row) => row.value);
+ const rows = connectionDetailRows(config, t, visibleFilterRow ?? undefined);
return { rows };
}
if (node.type === "trigger" && node.meta && node.connectionId && effectiveDatabaseTypeForConnection(connectionStore.getConfig(node.connectionId)) === "xugu") {
const trigger = node.meta as TriggerInfo;
const hasXuguDetails = trigger.level != null || trigger.condition != null || trigger.language != null || trigger.enabled != null || trigger.valid != null || trigger.created_at != null || trigger.comment != null;
if (!hasXuguDetails) return null;
- const rows: DetailTooltipRow[] = [
+ const rows: ConnectionDetailRow[] = [
{ label: t("objects.name"), value: visibleLabel(node) },
{ label: t("objects.triggerTiming"), value: cleanTooltipValue(trigger.timing) },
{ label: t("objects.triggerEvent"), value: cleanTooltipValue(trigger.event) },
@@ -632,7 +558,7 @@ const detailTooltip = computed(() => {
return rows.length ? { rows } : null;
}
if (node.type === "tablespace" && node.xuguTablespace && node.connectionId && effectiveDatabaseTypeForConnection(connectionStore.getConfig(node.connectionId)) === "xugu") {
- const rows: DetailTooltipRow[] = xuguTablespaceDetailRows(node.xuguTablespace).map((row) => ({
+ const rows: ConnectionDetailRow[] = xuguTablespaceDetailRows(node.xuguTablespace).map((row) => ({
label: t(`tree.xuguStorage.${row.key}`),
value: formatXuguStorageDetailValue(row.key, row.value),
multiline: row.multiline,
@@ -640,7 +566,7 @@ const detailTooltip = computed(() => {
return rows.length ? { rows } : null;
}
if (node.type === "datafile" && node.xuguDatafile && node.connectionId && effectiveDatabaseTypeForConnection(connectionStore.getConfig(node.connectionId)) === "xugu") {
- const rows: DetailTooltipRow[] = xuguDatafileDetailRows(node.xuguDatafile).map((row) => ({
+ const rows: ConnectionDetailRow[] = xuguDatafileDetailRows(node.xuguDatafile).map((row) => ({
label: t(`tree.xuguStorage.${row.key}`),
value: formatXuguStorageDetailValue(row.key, row.value),
multiline: row.multiline,
@@ -660,7 +586,7 @@ const detailTooltip = computed(() => {
const column = node.type === "column" ? (node.meta as ColumnInfo | undefined) : undefined;
const comment = column && "comment" in column ? column.comment : node.comment;
if ((!comment && !column) || !isSidebarCommentSupportedType(node.type)) return null;
- const rows: DetailTooltipRow[] = [
+ const rows: ConnectionDetailRow[] = [
{ label: t("connection.name"), value: visibleLabel(node) },
...(column ? [{ label: t("structureEditor.nullable"), value: t(column.is_nullable ? "structureEditor.nullable" : "structureEditor.notNull") }] : []),
{ label: t("structureEditor.comment"), value: cleanTooltipValue(comment), multiline: true },
@@ -1893,32 +1819,7 @@ function onKeydown(event: KeyboardEvent) {
</button>
</div>
<template v-if="detailTooltip" #content>
- <div class="w-max min-w-40 max-w-[min(28rem,calc(100vw-24px))] rounded-md border border-border bg-popover p-2 text-popover-foreground shadow-lg">
- <div class="space-y-1">
- <div v-for="row in detailTooltip.rows" :key="row.label" class="grid grid-cols-[max-content_minmax(0,1fr)] gap-2 text-xs leading-5">
- <span class="text-muted-foreground shrink-0">{{ row.label }}</span>
- <template v-if="row.values">
- <div class="flex flex-col gap-0.5 font-mono text-foreground/90">
- <span v-for="(v, vi) in row.values" :key="vi" class="break-all">{{ v }}</span>
- </div>
- </template>
- <button
- v-else-if="row.action"
- type="button"
- class="w-fit rounded bg-primary/10 px-1 font-mono text-primary underline-offset-2 hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
- :aria-label="row.actionLabel"
- :title="row.actionLabel"
- @click.stop="row.action()"
- >
- {{ row.value }}
- </button>
- <span v-else-if="row.multiline" class="max-h-20 overflow-hidden whitespace-pre-wrap break-words text-foreground/90">
- {{ row.value }}
- </span>
- <span v-else class="truncate font-mono text-foreground/90" :title="row.value">{{ row.value }}</span>
- </div>
- </div>
- </div>
+ <ConnectionDetailContent :rows="detailTooltip.rows" />
</template>
</LightTooltip>
</div>
diff --git a/apps/desktop/src/lib/connection/connectionDetailRows.ts b/apps/desktop/src/lib/connection/connectionDetailRows.ts
new file mode 100644
index 0000000000..c12c31beff
--- /dev/null
+++ b/apps/desktop/src/lib/connection/connectionDetailRows.ts
@@ -0,0 +1,85 @@
+import type { ConnectionConfig } from "@/types/database";
+import { connectionDisplayUrlScheme } from "@/lib/connection/connectionPresentation";
+import { redactConnectionStringSecrets } from "@/lib/connection/connectionStringRedaction";
+import { encodeOceanBaseUrlUsername } from "@/lib/connection/connectionUrlBuilder";
+import { encodeSpannerResourcePath } from "@/lib/connection/spannerResourcePath";
+import { effectiveRedisDatabaseIndex } from "@/lib/redis/redisDatabaseIndex";
+
+export type ConnectionDetailRow = {
+ label: string;
+ value: string;
+ multiline?: boolean;
+ values?: string[];
+ action?: () => void;
+ actionLabel?: string;
+};
+
+export type ConnectionDetailVisibleFilter = Pick<ConnectionDetailRow, "label" | "value" | "action" | "actionLabel">;
+
+function clean(value: string | number | null | undefined): string {
+ return String(value ?? "").trim();
+}
+
+export function isLocalFileConnection(config: Pick<ConnectionConfig, "db_type" | "port">): boolean {
+ return config.db_type === "sqlite" || config.db_type === "duckdb" || config.db_type === "access" || (config.db_type === "h2" && config.port === 0);
+}
+
+function hostForDisplay(host: string): string {
+ if (!host.includes(":") || host.startsWith("[") || host.includes("://") || host.includes(",")) return host;
+ return `[${host}]`;
+}
+
+function databaseValue(config: ConnectionConfig): string {
+ const database = clean(config.database);
+ return config.db_type === "redis" && database ? effectiveRedisDatabaseIndex(database) : database;
+}
+
+export function connectionDetailUrl(config: ConnectionConfig): string {
+ const explicit = clean(config.connection_string);
+ if (explicit) return redactConnectionStringSecrets(explicit);
+
+ const host = clean(config.host);
+ if (!host) return "";
+ if (host.includes("://")) return redactConnectionStringSecrets(host);
+
+ if (isLocalFileConnection(config)) {
+ if (config.db_type === "access") return `jdbc:ucanaccess://${host}`;
+ return `${config.db_type}://${host}`;
+ }
+
+ const scheme = connectionDisplayUrlScheme(config);
+ const port = Number(config.port) > 0 ? `:${config.port}` : "";
+ const user = clean(config.username);
+ const encodedUser = config.db_type === "mysql" && config.driver_profile?.trim().toLowerCase() === "oceanbase" ? encodeOceanBaseUrlUsername(user) : encodeURIComponent(user).replace(/%40/g, "@");
+ const userInfo = user ? `${encodedUser}@` : "";
+ const database = databaseValue(config);
+ const encodedDatabase = config.db_type === "spanner" ? encodeSpannerResourcePath(database) : encodeURIComponent(database);
+ const path = database ? `/${encodedDatabase}` : "";
+ const params = clean(config.url_params);
+ const query = params ? (params.startsWith("?") ? params : `?${params}`) : "";
+ return redactConnectionStringSecrets(`${scheme}://${userInfo}${hostForDisplay(host)}${port}${path}${query}`);
+}
+
+export function connectionDetailRows(config: ConnectionConfig, translate: (key: string) => string, visibleFilter?: ConnectionDetailVisibleFilter): ConnectionDetailRow[] {
+ const hostLabel = isLocalFileConnection(config) ? translate("connection.filePath") : translate("connection.host");
+ const hostValue = clean(config.host);
+ const hostValues = hostValue.includes(",")
+ ? hostValue
+ .split(",")
+ .map((host) => host.trim())
+ .filter(Boolean)
+ : [];
+
+ return [
+ { label: translate("connection.name"), value: clean(config.name) },
+ { label: "URL", value: connectionDetailUrl(config), multiline: true },
+ ...(hostValues.length > 0 ? [{ label: hostLabel, value: hostValues[0]!, values: hostValues }] : [{ label: hostLabel, value: hostValue, multiline: isLocalFileConnection(config) }]),
+ { label: "Port", value: Number(config.port) > 0 ? String(config.port) : "" },
+ { label: translate("connection.database"), value: databaseValue(config) },
+ { label: translate("connection.user"), value: clean(config.username) },
+ { label: translate("connection.type"), value: config.driver_label || config.driver_profile || config.db_type },
+ { label: translate("connection.databaseInfo.productVersion"), value: clean(config.database_info?.productVersion) },
+ ...(visibleFilter ? [visibleFilter] : []),
+ { label: translate("connection.note"), value: clean(config.note), multiline: true },
+ ].filter((row) => row.value);
+}