import React from "react"; import { Button, Dropdown, Select, Tooltip, type MenuProps } from "antd"; import { DownOutlined, EyeInvisibleOutlined, EyeOutlined, FormatPainterOutlined, PlayCircleOutlined, RobotOutlined, SearchOutlined, SaveOutlined, SettingOutlined, StopOutlined, } from "@ant-design/icons"; import { t as defaultTranslate } from '../i18n'; import { useOptionalI18n } from '../i18n/provider'; import type { SavedConnection } from "../types"; import { getShortcutDisplayLabel, type ShortcutPlatform, type ShortcutPlatformBinding, } from "../utils/shortcuts"; import QueryEditorTransactionSettings, { type SqlEditorCommitMode, } from "./QueryEditorTransactionSettings"; type QueryEditorToolbarProps = { isV2Ui: boolean; currentConnectionId: string; currentDb: string; queryCapableConnections: SavedConnection[]; dbList: string[]; maxRows: number; sqlEditorCommitMode: SqlEditorCommitMode; sqlEditorAutoCommitDelayMs: number; pendingTransactionToolbar: React.ReactNode; runQueryShortcutBinding: ShortcutPlatformBinding; saveQueryShortcutBinding: ShortcutPlatformBinding; formatSqlShortcutBinding: ShortcutPlatformBinding; triggerSqlAiCompletionShortcutBinding: ShortcutPlatformBinding; toggleQueryResultsPanelShortcutBinding: ShortcutPlatformBinding; activeShortcutPlatform: ShortcutPlatform; isResultPanelVisible: boolean; loading: boolean; saveMoreMenuItems: MenuProps["items"]; formatSettingsMenu: MenuProps["items"]; onConnectionChange: (connectionId: string) => void; onDatabaseChange: (dbName: string) => void; onMaxRowsChange: (maxRows: number) => void; onCommitModeChange: (mode: SqlEditorCommitMode) => void; onAutoCommitDelayMsChange: (delayMs: number) => void; onCaptureEditorCursorPosition: () => void; onRun: () => void; onCancel: () => void; onQuickSave: () => void; onFindInEditor: () => void; onFormat: () => void; onTriggerSqlAiCompletion: () => void; onToggleResultPanelVisibility: () => void; onAIAction: (action: "generate" | "explain" | "optimize" | "schema") => void; }; const FULL_NAME_TOOLTIP_DELAY_SECONDS = 1; type FullNameSelectOption = { label: string; value: string; title: string; fullName: string; }; const renderFullNameSelectTooltip = (fullName: React.ReactNode) => { const fullNameText = String(fullName ?? ""); return ( {fullNameText} ); }; const QueryEditorToolbar: React.FC = ({ isV2Ui, currentConnectionId, currentDb, queryCapableConnections, dbList, maxRows, sqlEditorCommitMode, sqlEditorAutoCommitDelayMs, pendingTransactionToolbar, runQueryShortcutBinding, saveQueryShortcutBinding, formatSqlShortcutBinding, triggerSqlAiCompletionShortcutBinding, toggleQueryResultsPanelShortcutBinding, activeShortcutPlatform, isResultPanelVisible, loading, saveMoreMenuItems, formatSettingsMenu, onConnectionChange, onDatabaseChange, onMaxRowsChange, onCommitModeChange, onAutoCommitDelayMsChange, onCaptureEditorCursorPosition, onRun, onCancel, onQuickSave, onFindInEditor, onFormat, onTriggerSqlAiCompletion, onToggleResultPanelVisibility, onAIAction, }) => { const i18n = useOptionalI18n(); const t = i18n?.t ?? defaultTranslate; const baseMoreMenuItems = saveMoreMenuItems ?? []; const connectionSelectOptions: FullNameSelectOption[] = queryCapableConnections.map((connection) => ({ label: connection.name, value: connection.id, title: "", fullName: connection.name, })); const databaseSelectOptions: FullNameSelectOption[] = dbList.map((db) => ({ label: db, value: db, title: "", fullName: db, })); const toggleResultPanelShortcutLabel = toggleQueryResultsPanelShortcutBinding.enabled && toggleQueryResultsPanelShortcutBinding.combo ? getShortcutDisplayLabel( toggleQueryResultsPanelShortcutBinding.combo, activeShortcutPlatform, ) : ""; const toggleResultPanelTitle = toggleQueryResultsPanelShortcutBinding.enabled && toggleQueryResultsPanelShortcutBinding.combo ? t( isResultPanelVisible ? "query_editor.action.hide_results_panel_with_shortcut" : "query_editor.action.show_results_panel_with_shortcut", { shortcut: toggleResultPanelShortcutLabel }, ) : isResultPanelVisible ? t("query_editor.action.hide_results_panel") : t("query_editor.action.show_results_panel"); const formatSqlTitle = formatSqlShortcutBinding.enabled && formatSqlShortcutBinding.combo ? t("query_editor.action.format_sql_with_shortcut", { shortcut: getShortcutDisplayLabel( formatSqlShortcutBinding.combo, activeShortcutPlatform, ), }) : t("query_editor.action.format_sql"); const findInEditorShortcutCombo = activeShortcutPlatform === "mac" ? "Meta+F" : "Ctrl+F"; const findInEditorTitle = t( "query_editor.action.find_in_editor_with_shortcut", { shortcut: getShortcutDisplayLabel( findInEditorShortcutCombo, activeShortcutPlatform, ), }, ); const triggerSqlAiCompletionLabel = triggerSqlAiCompletionShortcutBinding.enabled && triggerSqlAiCompletionShortcutBinding.combo ? `${t("app.shortcuts.action.triggerSqlAiCompletion.label")} ยท ${getShortcutDisplayLabel( triggerSqlAiCompletionShortcutBinding.combo, activeShortcutPlatform, )}` : t("app.shortcuts.action.triggerSqlAiCompletion.label"); const aiMenuItems: MenuProps["items"] = [ { key: "ai-inline-completion", label: triggerSqlAiCompletionLabel, icon: , onClick: onTriggerSqlAiCompletion, }, { type: "divider" as const }, { key: "ai-generate", label: t("query_editor.action.ai_text_to_sql_menu"), icon: , onClick: () => onAIAction("generate"), }, { key: "ai-explain", label: t("query_editor.action.ai_explain_sql_menu"), icon: , onClick: () => onAIAction("explain"), }, { key: "ai-optimize", label: t("query_editor.action.ai_optimize_sql_menu"), icon: , onClick: () => onAIAction("optimize"), }, { type: "divider" as const }, { key: "ai-schema", label: t("query_editor.action.ai_schema_analysis"), icon: , onClick: () => onAIAction("schema"), }, ]; const moreMenuItems: MenuProps["items"] = isV2Ui ? [ ...baseMoreMenuItems, ...(baseMoreMenuItems.length > 0 ? [{ type: "divider" as const }] : []), { key: "toggle-result-panel", label: toggleResultPanelTitle, icon: isResultPanelVisible ? ( ) : ( ), onClick: onToggleResultPanelVisibility, }, ] : baseMoreMenuItems; const selects = (
renderFullNameSelectTooltip(option.data.fullName)} labelRender={(option) => renderFullNameSelectTooltip(option.label ?? option.value)} showSearch />