import type { ObjectSourceKind, TreeNodeType } from "@/types/database"; import { matchesShortcut, type ShortcutLikeEvent } from "@/lib/keyboardShortcuts"; export type TreeNodeRowAction = "open-data" | "toggle" | "none"; export type TreeNodeRowDoubleClickAction = | "open-data" | "open-object-browser" | "open-object-browser-and-expand" | "open-source" | "open-saved-sql" | "toggle" | "none"; export type SidebarSelectionCopyAction = "copy-name" | "none"; export type SidebarActivation = "single" | "double"; const dataNodeTypes = new Set(["table", "view"]); const toggleLeafNodeTypes = new Set(["redis-db", "mongo-collection"]); const objectBrowserNodeTypes = new Set(["database", "schema", "object-browser"]); const sourceNodeTypes = new Set(["procedure", "function"]); export function objectSourceKindForTreeNode(type: TreeNodeType): ObjectSourceKind | null { if (type === "view") return "VIEW"; if (type === "procedure") return "PROCEDURE"; if (type === "function") return "FUNCTION"; return null; } export function treeNodeRowAction( type: TreeNodeType, canExpand: boolean, activation: SidebarActivation = "single", ): TreeNodeRowAction { if (activation === "double") return "none"; if (dataNodeTypes.has(type)) return "open-data"; if (toggleLeafNodeTypes.has(type)) return "toggle"; if (canExpand) return "toggle"; return "none"; } export function treeNodeRowDoubleClickAction( type: TreeNodeType, canOpenObjectBrowser: boolean, activation: SidebarActivation = "single", canExpand = false, ): TreeNodeRowDoubleClickAction { if (activation === "double") { if (dataNodeTypes.has(type)) return "open-data"; if (sourceNodeTypes.has(type)) return "open-source"; if (type === "saved-sql-file") return "open-saved-sql"; if (toggleLeafNodeTypes.has(type)) return "toggle"; if (canOpenObjectBrowser && objectBrowserNodeTypes.has(type) && canExpand) return "open-object-browser-and-expand"; if (canOpenObjectBrowser && objectBrowserNodeTypes.has(type)) return "open-object-browser"; if (canExpand) return "toggle"; } if (canOpenObjectBrowser && objectBrowserNodeTypes.has(type)) return "open-object-browser"; return "none"; } export function sidebarSelectionCopyAction(event: ShortcutLikeEvent): SidebarSelectionCopyAction { return matchesShortcut(event, "Mod+C") ? "copy-name" : "none"; }