Skip to content

Commit

Permalink
[Console Monaco Migration] Add autocomplete polling to editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaStoeva committed Apr 11, 2024
1 parent 357b6f8 commit d23a7cb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { CodeEditor } from '@kbn/code-editor';
import { css } from '@emotion/react';
import { CONSOLE_LANG_ID, CONSOLE_THEME_ID } from '@kbn/monaco';
import { useSetInitialValue } from './use_set_initial_value';
import { useSetupAutocompletePolling } from './use_setup_autocomplete_polling';
import { useServicesContext, useEditorReadContext } from '../../../contexts';

export interface EditorProps {
Expand All @@ -21,6 +22,8 @@ export const MonacoEditor = ({ initialTextValue }: EditorProps) => {
const {
services: {
notifications: { toasts },
settings: settingsService,
autocompleteInfo,
},
} = useServicesContext();
const { settings } = useEditorReadContext();
Expand All @@ -33,6 +36,8 @@ export const MonacoEditor = ({ initialTextValue }: EditorProps) => {
setInitialValue({ initialTextValue, setValue, toasts });
}, [initialTextValue, setInitialValue, toasts]);

useSetupAutocompletePolling({ autocompleteInfo, settingsService });

return (
<div
css={css`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { useEffect } from 'react';
import { AutocompleteInfo, Settings } from '../../../../services';

interface SetInitialValueParams {
/** The Console autocomplete service. */
autocompleteInfo: AutocompleteInfo;
/** The Console settings service. */
settingsService: Settings;
}

/**
* Hook that sets up the autocomplete polling for Console editor.
*
* @param params The {@link SetInitialValueParams} to use.
*/
export const useSetupAutocompletePolling = (params: SetInitialValueParams) => {
const { autocompleteInfo, settingsService } = params;

useEffect(() => {
autocompleteInfo.retrieve(settingsService, settingsService.getAutocomplete());

return () => {
autocompleteInfo.clearSubscriptions();
};
}, [autocompleteInfo, settingsService]);
};

0 comments on commit d23a7cb

Please sign in to comment.