From e446dffeeade0e67d0854540b9b059294b306bb4 Mon Sep 17 00:00:00 2001 From: Joshen Lim Date: Mon, 18 Nov 2024 16:23:45 +0800 Subject: [PATCH] Add explain code action to SQL editor --- .../interfaces/SQLEditor/MonacoEditor.tsx | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx b/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx index 7be8fb03bc3..b0571a93f66 100644 --- a/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx +++ b/apps/studio/components/interfaces/SQLEditor/MonacoEditor.tsx @@ -13,6 +13,8 @@ import { cn } from 'ui' import { untitledSnippetTitle } from './SQLEditor.constants' import type { IStandaloneCodeEditor } from './SQLEditor.types' import { createSqlSnippetSkeletonV2 } from './SQLEditor.utils' +import { useIsAssistantV2Enabled } from '../App/FeaturePreview/FeaturePreviewContext' +import { useAppStateSnapshot } from 'state/app-state' export type MonacoEditorProps = { id: string @@ -39,6 +41,9 @@ const MonacoEditor = ({ const project = useSelectedProject() const snapV2 = useSqlEditorV2StateSnapshot() + const isAssistantV2Enabled = useIsAssistantV2Enabled() + const { setAiAssistantPanel } = useAppStateSnapshot() + const [intellisenseEnabled] = useLocalStorageQuery( LOCAL_STORAGE_KEYS.SQL_EDITOR_INTELLISENSE, true @@ -69,6 +74,25 @@ const MonacoEditor = ({ }, }) + if (isAssistantV2Enabled) { + editor.addAction({ + id: 'explain-code', + label: 'Explain Code', + contextMenuGroupId: 'operation', + contextMenuOrder: 1, + run: () => { + const selectedValue = (editorRef?.current as any) + .getModel() + .getValueInRange((editorRef?.current as any)?.getSelection()) + setAiAssistantPanel({ + open: true, + sqlSnippets: [selectedValue], + initialInput: 'Can you explain this section to me in more detail?', + }) + }, + }) + } + editor.onDidChangeCursorSelection(({ selection }) => { const noSelection = selection.startLineNumber === selection.endLineNumber &&