From 5df6681ade2b0eb18bf6153b2d6ff497ec416981 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Mon, 17 Apr 2023 21:44:05 -0500 Subject: [PATCH] fix script field editor --- .../field_editor/form_fields/script_field.tsx | 7 +++++-- .../components/preview/field_preview_context.tsx | 15 +++++++++------ .../components/preview/preview_controller.ts | 2 ++ .../public/components/preview/types.ts | 5 +++++ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/plugins/data_view_field_editor/public/components/field_editor/form_fields/script_field.tsx b/src/plugins/data_view_field_editor/public/components/field_editor/form_fields/script_field.tsx index 6a53ae14186de..fddf14c864743 100644 --- a/src/plugins/data_view_field_editor/public/components/field_editor/form_fields/script_field.tsx +++ b/src/plugins/data_view_field_editor/public/components/field_editor/form_fields/script_field.tsx @@ -59,6 +59,9 @@ const currentDocumentSelector = (state: PreviewState) => state.documents[state.c const currentDocumentIsLoadingSelector = (state: PreviewState) => state.isLoadingDocuments; const ScriptFieldComponent = ({ existingConcreteFields, links, placeholder }: Props) => { + const { + validation: { setScriptEditorValidation }, + } = useFieldPreviewContext(); const monacoEditor = useRef(null); const editorValidationSubscription = useRef(); const fieldCurrentValue = useRef(''); @@ -143,7 +146,7 @@ const ScriptFieldComponent = ({ existingConcreteFields, links, placeholder }: Pr editorValidationSubscription.current = PainlessLang.validation$().subscribe( ({ isValid, isValidating, errors }) => { - controller.setScriptEditorValidation({ + setScriptEditorValidation({ isValid, isValidating, message: errors[0]?.message ?? null, @@ -151,7 +154,7 @@ const ScriptFieldComponent = ({ existingConcreteFields, links, placeholder }: Pr } ); }, - [controller] + [setScriptEditorValidation] ); const updateMonacoMarkers = useCallback((markers: monaco.editor.IMarkerData[]) => { diff --git a/src/plugins/data_view_field_editor/public/components/preview/field_preview_context.tsx b/src/plugins/data_view_field_editor/public/components/preview/field_preview_context.tsx index 3addd448f1e7e..f554025ce9f4b 100644 --- a/src/plugins/data_view_field_editor/public/components/preview/field_preview_context.tsx +++ b/src/plugins/data_view_field_editor/public/components/preview/field_preview_context.tsx @@ -75,8 +75,6 @@ const documentsSelector = (state: PreviewState) => { }; }; -const scriptEditorValidationSelector = (state: PreviewState) => state.scriptEditorValidation; - export const FieldPreviewProvider: FunctionComponent<{ controller: PreviewController }> = ({ controller, children, @@ -121,6 +119,12 @@ export const FieldPreviewProvider: FunctionComponent<{ controller: PreviewContro /** The parameters required for the Painless _execute API */ const [params, setParams] = useState(defaultParams); + const [scriptEditorValidation, setScriptEditorValidation] = useState<{ + isValidating: boolean; + isValid: boolean; + message: string | null; + }>({ isValidating: false, isValid: true, message: null }); + /** Flag to show/hide the preview panel */ const [isPanelVisible, setIsPanelVisible] = useState(true); /** Flag to indicate if we are loading document from cluster */ @@ -133,10 +137,6 @@ export const FieldPreviewProvider: FunctionComponent<{ controller: PreviewContro const { currentDocument, currentDocIndex, currentDocId, totalDocs, currentIdx } = useStateSelector(controller.state$, documentsSelector); - const scriptEditorValidation = useStateSelector( - controller.state$, - scriptEditorValidationSelector - ); let isPreviewAvailable = true; @@ -513,6 +513,9 @@ export const FieldPreviewProvider: FunctionComponent<{ controller: PreviewContro isVisible: isPanelVisible, setIsVisible: setIsPanelVisible, }, + validation: { + setScriptEditorValidation, + }, reset, }), [ diff --git a/src/plugins/data_view_field_editor/public/components/preview/preview_controller.ts b/src/plugins/data_view_field_editor/public/components/preview/preview_controller.ts index 80b11e74597aa..b572827eac06d 100644 --- a/src/plugins/data_view_field_editor/public/components/preview/preview_controller.ts +++ b/src/plugins/data_view_field_editor/public/components/preview/preview_controller.ts @@ -95,9 +95,11 @@ export class PreviewController { } }; + /* disabled while investigating issues with painless script editor setScriptEditorValidation = (scriptEditorValidation: PreviewState['scriptEditorValidation']) => { this.updateState({ scriptEditorValidation }); }; + */ setCustomId = (customId?: string) => { this.updateState({ customId }); diff --git a/src/plugins/data_view_field_editor/public/components/preview/types.ts b/src/plugins/data_view_field_editor/public/components/preview/types.ts index 377aed627ba54..347e0a709cf28 100644 --- a/src/plugins/data_view_field_editor/public/components/preview/types.ts +++ b/src/plugins/data_view_field_editor/public/components/preview/types.ts @@ -133,6 +133,11 @@ export interface Context { isLastDoc: boolean; }; reset: () => void; + validation: { + setScriptEditorValidation: React.Dispatch< + React.SetStateAction<{ isValid: boolean; isValidating: boolean; message: string | null }> + >; + }; } export type PainlessExecuteContext =