/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { forwardRef, useState, useEffect, useImperativeHandle, useRef } from "react"; import { EuiFormRow } from "@elastic/eui"; import type { MonacoDiffEditorProps } from "react-monaco-editor"; import { IJSONEditorRef } from "../JSONEditor"; import CustomFormRow from "../CustomFormRow"; export interface JSONDiffEditorProps extends Partial { value: string; onChange?: (value: JSONDiffEditorProps["value"]) => void; "data-test-subj"?: string; disabled?: boolean; } const JSONDiffEditor = forwardRef(({ value, onChange, ...others }: JSONDiffEditorProps, ref: React.Ref) => { const [confirmModalVisible, setConfirmModalVisible] = useState(false); const [editorValue, setEditorValue] = useState(value); const valueRef = useRef(editorValue); valueRef.current = editorValue; useEffect(() => { setEditorValue(value); }, [value]); useImperativeHandle(ref, () => ({ validate: () => new Promise((resolve, reject) => { try { JSON.parse(valueRef.current || "{}"); resolve(""); } catch (e) { setConfirmModalVisible(true); reject("Format validate error"); } }), getValue: () => valueRef.current, setValue: (val: string) => setEditorValue(val), })); return (