1
+ import React from 'react' ;
2
+
1
3
import NiceModal from '@ebay/nice-modal-react' ;
2
- import { useMonacoGhost } from '@ydb-platform/monaco-ghost' ;
4
+ import { createMonacoGhostInstance } from '@ydb-platform/monaco-ghost' ;
3
5
import throttle from 'lodash/throttle' ;
4
6
import type Monaco from 'monaco-editor' ;
5
7
@@ -11,7 +13,7 @@ import {
11
13
selectUserInput ,
12
14
} from '../../../../store/reducers/query/query' ;
13
15
import type { QueryAction } from '../../../../types/store/query' ;
14
- import { LAST_USED_QUERY_ACTION_KEY } from '../../../../utils/constants' ;
16
+ import { ENABLE_CODE_ASSISTANT , LAST_USED_QUERY_ACTION_KEY } from '../../../../utils/constants' ;
15
17
import {
16
18
useEventHandler ,
17
19
useSetting ,
@@ -23,9 +25,8 @@ import {useUpdateErrorsHighlighting} from '../../../../utils/monaco/highlightErr
23
25
import { QUERY_ACTIONS } from '../../../../utils/query' ;
24
26
import { SAVE_QUERY_DIALOG } from '../SaveQuery/SaveQuery' ;
25
27
import i18n from '../i18n' ;
26
- import { useSavedQueries } from '../utils/useSavedQueries' ;
27
28
28
- import { useCodeAssist , useEditorOptions } from './helpers' ;
29
+ import { useCodeAssistHelpers , useEditorOptions } from './helpers' ;
29
30
import { getKeyBindings } from './keybindings' ;
30
31
31
32
const CONTEXT_MENU_GROUP_ID = 'navigation' ;
@@ -45,8 +46,11 @@ export function YqlEditor({
45
46
} : YqlEditorProps ) {
46
47
const input = useTypedSelector ( selectUserInput ) ;
47
48
const dispatch = useTypedDispatch ( ) ;
49
+ const [ monacoGhostInstance , setMonacoGhostInstance ] =
50
+ React . useState < ReturnType < typeof createMonacoGhostInstance > > ( ) ;
48
51
const historyQueries = useTypedSelector ( selectQueriesHistory ) ;
49
- const savedQueries = useSavedQueries ( ) ;
52
+ const [ isCodeAssistEnabled ] = useSetting ( ENABLE_CODE_ASSISTANT ) ;
53
+
50
54
const editorOptions = useEditorOptions ( ) ;
51
55
const updateErrorsHighlighting = useUpdateErrorsHighlighting ( ) ;
52
56
@@ -71,20 +75,18 @@ export function YqlEditor({
71
75
window . ydbEditor = undefined ;
72
76
} ;
73
77
74
- const codeAssist = useCodeAssist ( ) ;
75
- const { registerMonacoGhost} = useMonacoGhost ( {
76
- api : {
77
- getCodeAssistSuggestions : codeAssist . getCodeAssistSuggestions ,
78
- } ,
79
- eventHandlers : {
80
- onCompletionAccept : codeAssist . onCompletionAccept ,
81
- onCompletionDecline : codeAssist . onCompletionDecline ,
82
- onCompletionIgnore : codeAssist . onCompletionIgnore ,
83
- } ,
84
- config : {
85
- language : YQL_LANGUAGE_ID ,
86
- } ,
87
- } ) ;
78
+ const { monacoGhostConfig, prepareUserQueriesCache} = useCodeAssistHelpers ( ) ;
79
+
80
+ React . useEffect ( ( ) => {
81
+ if ( monacoGhostInstance && isCodeAssistEnabled ) {
82
+ monacoGhostInstance . register ( monacoGhostConfig ) ;
83
+ prepareUserQueriesCache ( ) ;
84
+ }
85
+
86
+ return ( ) => {
87
+ monacoGhostInstance ?. unregister ( ) ;
88
+ } ;
89
+ } , [ isCodeAssistEnabled , monacoGhostConfig , monacoGhostInstance , prepareUserQueriesCache ] ) ;
88
90
89
91
const editorDidMount = ( editor : Monaco . editor . IStandaloneCodeEditor , monaco : typeof Monaco ) => {
90
92
window . ydbEditor = editor ;
@@ -100,18 +102,9 @@ export function YqlEditor({
100
102
} ) ;
101
103
102
104
if ( window . api . codeAssist ) {
103
- registerMonacoGhost ( editor ) ;
104
- codeAssist . prepareUserQueriesCache ( [
105
- ...historyQueries . map ( ( query , index ) => ( {
106
- name : `query${ index } .yql` ,
107
- text : query . queryText ,
108
- } ) ) ,
109
- ...savedQueries . map ( ( query ) => ( {
110
- name : query . name ,
111
- text : query . body ,
112
- } ) ) ,
113
- ] ) ;
105
+ setMonacoGhostInstance ( createMonacoGhostInstance ( editor ) ) ;
114
106
}
107
+
115
108
initResizeHandler ( editor ) ;
116
109
initUserPrompt ( editor , getLastQueryText ) ;
117
110
editor . focus ( ) ;
0 commit comments