1+ import React from 'react' ;
2+
13import NiceModal from '@ebay/nice-modal-react' ;
2- import { useMonacoGhost } from '@ydb-platform/monaco-ghost' ;
4+ import { createMonacoGhostInstance } from '@ydb-platform/monaco-ghost' ;
35import throttle from 'lodash/throttle' ;
46import type Monaco from 'monaco-editor' ;
57
@@ -23,9 +25,8 @@ import {useUpdateErrorsHighlighting} from '../../../../utils/monaco/highlightErr
2325import { QUERY_ACTIONS } from '../../../../utils/query' ;
2426import { SAVE_QUERY_DIALOG } from '../SaveQuery/SaveQuery' ;
2527import i18n from '../i18n' ;
26- import { useSavedQueries } from '../utils/useSavedQueries' ;
2728
28- import { useCodeAssist , useEditorOptions } from './helpers' ;
29+ import { useCodeAssistHelpers , useEditorOptions } from './helpers' ;
2930import { getKeyBindings } from './keybindings' ;
3031
3132const CONTEXT_MENU_GROUP_ID = 'navigation' ;
@@ -44,10 +45,11 @@ export function YqlEditor({
4445 handleGetExplainQueryClick,
4546} : YqlEditorProps ) {
4647 const input = useTypedSelector ( selectUserInput ) ;
47- const isCodeAssistEnabled = useSetting ( ENABLE_CODE_ASSISTANT ) ;
4848 const dispatch = useTypedDispatch ( ) ;
49+ const monacoGhostInstanceRef = React . useRef < ReturnType < typeof createMonacoGhostInstance > > ( ) ;
4950 const historyQueries = useTypedSelector ( selectQueriesHistory ) ;
50- const savedQueries = useSavedQueries ( ) ;
51+ const [ isCodeAssistEnabled ] = useSetting ( ENABLE_CODE_ASSISTANT ) ;
52+
5153 const editorOptions = useEditorOptions ( ) ;
5254 const updateErrorsHighlighting = useUpdateErrorsHighlighting ( ) ;
5355
@@ -72,20 +74,21 @@ export function YqlEditor({
7274 window . ydbEditor = undefined ;
7375 } ;
7476
75- const codeAssist = useCodeAssist ( ) ;
76- const { registerMonacoGhost} = useMonacoGhost ( {
77- api : {
78- getCodeAssistSuggestions : codeAssist . getCodeAssistSuggestions ,
79- } ,
80- eventHandlers : {
81- onCompletionAccept : codeAssist . onCompletionAccept ,
82- onCompletionDecline : codeAssist . onCompletionDecline ,
83- onCompletionIgnore : codeAssist . onCompletionIgnore ,
84- } ,
85- config : {
86- language : YQL_LANGUAGE_ID ,
87- } ,
88- } ) ;
77+ const { monacoGhostConfig, prepareUserQueriesCache} = useCodeAssistHelpers ( ) ;
78+
79+ React . useEffect ( ( ) => {
80+ if ( monacoGhostInstanceRef . current && window . api . codeAssist ) {
81+ if ( isCodeAssistEnabled ) {
82+ monacoGhostInstanceRef . current . register ( monacoGhostConfig ) ;
83+ prepareUserQueriesCache ( ) ;
84+ } else {
85+ monacoGhostInstanceRef . current . unregister ( ) ;
86+ }
87+ }
88+ return ( ) => {
89+ monacoGhostInstanceRef . current ?. unregister ( ) ;
90+ } ;
91+ } , [ isCodeAssistEnabled , monacoGhostConfig , prepareUserQueriesCache ] ) ;
8992
9093 const editorDidMount = ( editor : Monaco . editor . IStandaloneCodeEditor , monaco : typeof Monaco ) => {
9194 window . ydbEditor = editor ;
@@ -100,19 +103,7 @@ export function YqlEditor({
100103 }
101104 } ) ;
102105
103- if ( window . api . codeAssist && isCodeAssistEnabled ) {
104- registerMonacoGhost ( editor ) ;
105- codeAssist . prepareUserQueriesCache ( [
106- ...historyQueries . map ( ( query , index ) => ( {
107- name : `query${ index } .yql` ,
108- text : query . queryText ,
109- } ) ) ,
110- ...savedQueries . map ( ( query ) => ( {
111- name : query . name ,
112- text : query . body ,
113- } ) ) ,
114- ] ) ;
115- }
106+ monacoGhostInstanceRef . current = createMonacoGhostInstance ( editor ) ;
116107 initResizeHandler ( editor ) ;
117108 initUserPrompt ( editor , getLastQueryText ) ;
118109 editor . focus ( ) ;
0 commit comments