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。

3.0 KB
Raw Blame
import type { FlatTreeNode } from "@/composables/useFlatTree";
import type { DatabaseType, TreeNode } from "@/types/database";
import { canTreeNodeShowExpander } from "@/lib/sidebar/sidebarTreeItemLayout";
import { customTypeCapabilities, supportsOracleTypeAttributeExpansion } from "@/lib/database/databaseObjectCapabilities";

export type SidebarTreeArrowAction = { kind: "select"; nodeId: string } | { kind: "toggle"; nodeId: string; expanded: boolean } | { kind: "none" };

/**
 * Resolves the standard tree-navigation action (VS Code / file explorer
 * semantics) for an arrow key pressed on the row identified by
 * `currentNodeId`. Callers pass the visible rows they render, excluding
 * non-selectable pseudo rows (e.g. per-database table search controls), so
 * vertical movement naturally skips them. `databaseType` is the connection's
 * dialect of the active row and mirrors the chevron expandability gates.
 */
export function sidebarTreeArrowAction(rows: readonly FlatTreeNode[], currentNodeId: string, key: string, options?: { databaseType?: DatabaseType }): SidebarTreeArrowAction {
  const currentIndex = rows.findIndex((row) => row.id === currentNodeId);
  if (currentIndex < 0) return { kind: "none" };

  const current = rows[currentIndex]!;
  const node = current.node;

  if (key === "ArrowUp") {
    return currentIndex > 0 ? { kind: "select", nodeId: rows[currentIndex - 1]!.id } : { kind: "none" };
  }
  if (key === "ArrowDown") {
    return currentIndex < rows.length - 1 ? { kind: "select", nodeId: rows[currentIndex + 1]!.id } : { kind: "none" };
  }
  if (key === "ArrowRight") {
    const next = rows[currentIndex + 1];
    if (node.isExpanded && next && next.depth === current.depth + 1) return { kind: "select", nodeId: next.id };
    if (!node.isExpanded && isArrowExpandable(node, options?.databaseType)) return { kind: "toggle", nodeId: node.id, expanded: true };
    return { kind: "none" };
  }
  if (key === "ArrowLeft") {
    if (node.isExpanded && node.children?.length) return { kind: "toggle", nodeId: node.id, expanded: false };
    for (let index = currentIndex - 1; index >= 0; index -= 1) {
      if (rows[index]!.depth < current.depth) return { kind: "select", nodeId: rows[index]!.id };
    }
    return { kind: "none" };
  }
  return { kind: "none" };
}

function isArrowExpandable(node: TreeNode, databaseType?: DatabaseType): boolean {
  // Match the chevron gate (TreeItem/SidebarTreeRuntimeHost canExpand):
  // PostgreSQL-family custom types only expand when the catalog marked them
  // with loadable members; member-less kinds (enum/domain/range/base) stay
  // collapsed even when child metadata rows exist.
  if (node.type === "type" && customTypeCapabilities(databaseType).details) return node.hasMembers === true || (node.hasMembers === undefined && supportsOracleTypeAttributeExpansion(databaseType));
  return canTreeNodeShowExpander({
    type: node.type,
    childCount: node.children?.length ?? 0,
    explicitContainer: (node.type === "package" && node.children !== undefined) || node.xuguTypeMembersExpandable === true,
  });
}