diff --git a/apps/studio/components/ui/AIEditor/index.tsx b/apps/studio/components/ui/AIEditor/index.tsx index 1fa7d3bb507..ce8c6865a71 100644 --- a/apps/studio/components/ui/AIEditor/index.tsx +++ b/apps/studio/components/ui/AIEditor/index.tsx @@ -1,4 +1,4 @@ -import Editor, { Monaco, OnMount } from '@monaco-editor/react' +import { Monaco, OnMount } from '@monaco-editor/react' import { AnimatePresence, motion } from 'framer-motion' import type { editor as monacoEditor } from 'monaco-editor' import { useCallback, useEffect, useRef, useState } from 'react' @@ -6,18 +6,20 @@ import { toast } from 'sonner' import { KeyboardShortcut } from 'ui' import { useSetCommandMenuOpen } from 'ui-patterns' +import { CodeEditor, type ValidLanguages } from '../CodeEditor/CodeEditor' import { DiffEditor } from '../DiffEditor' import ResizableAIWidget from './ResizableAIWidget' import { getEditorSelectionParts } from './utils' import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider' import { constructHeaders } from '@/data/fetchers' +import { useLatest } from '@/hooks/misc/useLatest' import { SHORTCUT_IDS } from '@/state/shortcuts/registry' import { useIsShortcutEnabled } from '@/state/shortcuts/useIsShortcutEnabled' import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state' interface AIEditorProps { id?: string - language?: string + language: ValidLanguages value?: string defaultValue?: string aiEndpoint?: string @@ -71,14 +73,9 @@ export const AIEditor = ({ const isCommandMenuHotkeyEnabled = useIsShortcutEnabled(SHORTCUT_IDS.COMMAND_MENU_OPEN) const setCommandMenuOpen = useSetCommandMenuOpen() - const executeQueryRef = useRef(executeQuery) - executeQueryRef.current = executeQuery - - const commandMenuHotkeyEnabledRef = useRef(isCommandMenuHotkeyEnabled) - commandMenuHotkeyEnabledRef.current = isCommandMenuHotkeyEnabled - - const setCommandMenuOpenRef = useRef(setCommandMenuOpen) - setCommandMenuOpenRef.current = setCommandMenuOpen + const executeQueryRef = useLatest(executeQuery) + const commandMenuHotkeyEnabledRef = useLatest(isCommandMenuHotkeyEnabled) + const setCommandMenuOpenRef = useLatest(setCommandMenuOpen) const [currentValue, setCurrentValue] = useState(value || defaultValue) const [isDiffMode, setIsDiffMode] = useState(false) @@ -237,17 +234,6 @@ export const AIEditor = ({ }) } - if (!!executeQueryRef.current) { - editor.addAction({ - id: 'run-query', - label: 'Run Query', - keybindings: [monaco.KeyMod.CtrlCmd + monaco.KeyCode.Enter], - contextMenuGroupId: 'operation', - contextMenuOrder: 0, - run: () => executeQueryRef.current?.(), - }) - } - refreshCloseAction() // Add AI Assistant toggle keybinding (Cmd+I) @@ -281,11 +267,6 @@ export const AIEditor = ({ setCommandMenuOpenRef.current(true) } }) - - if (autoFocus) { - if (editor.getValue().length === 1) editor.setPosition({ lineNumber: 1, column: 2 }) - editor.focus() - } } const handlePrompt = async ( @@ -325,19 +306,6 @@ export const AIEditor = ({ } } - const defaultOptions: monacoEditor.IStandaloneEditorConstructionOptions = { - tabSize: 2, - fontSize: 13, - readOnly, - minimap: { enabled: false }, - wordWrap: 'on', - lineNumbers: 'on', - folding: false, - padding: { top: 4 }, - lineNumbersMinChars: 3, - ...options, - } - useEffect(() => { setCurrentValue(value || defaultValue) }, [value, defaultValue]) @@ -414,19 +382,20 @@ export const AIEditor = ({ ) : (