diff --git a/src/editor/components/CodeEditor/CodeEditor.tsx b/src/editor/components/CodeEditor/CodeEditor.tsx index 2f5def0fb..4d6d6a926 100644 --- a/src/editor/components/CodeEditor/CodeEditor.tsx +++ b/src/editor/components/CodeEditor/CodeEditor.tsx @@ -17,6 +17,8 @@ import './CodeEditor.scss'; const b = block('code-editor'); +const ON_CHANGE_DEBOUNCE_TIMEOUT = 300; + interface CodeEditorProps { code: string; fullscreenModeOn: boolean; @@ -26,57 +28,60 @@ interface CodeEditorProps { message?: CodeEditorMessageProps; } -export const CodeEditor = ({ - onChange, - validator, - fullscreenModeOn, - onFullscreenModeOnUpdate, - code, -}: CodeEditorProps) => { - const [message, setMessage] = useState(() => validator(code)); - const {theme = Theme.Light} = useContext(EditorContext); +export const CodeEditor = React.memo( + ({onChange, validator, fullscreenModeOn, onFullscreenModeOnUpdate, code}: CodeEditorProps) => { + const [message, setMessage] = useState(() => validator(code)); + const {theme = Theme.Light} = useContext(EditorContext); - // eslint-disable-next-line react-hooks/exhaustive-deps - const onChangeWithValidation = useCallback( - debounce((newCode: string) => { - const validationResult = validator(newCode); + // eslint-disable-next-line react-hooks/exhaustive-deps + const onChangeWithValidation = useCallback( + debounce((newCode: string) => { + const validationResult = validator(newCode); - setMessage(validationResult); - onChange(parseCode(newCode)); - }, 200), - [onChange, validator], - ); + setMessage(validationResult); + onChange(parseCode(newCode)); + }, ON_CHANGE_DEBOUNCE_TIMEOUT), + [onChange, validator], + ); - return ( -
-
- +
+
+ - -
-
- +
+
+ {message && ( +
+
+ {message.text} +
+
+ )} +
-
- {message && ( -
-
{message.text}
-
- )} -
- - ); -}; + ); + }, +); + +CodeEditor.displayName = 'CodeEditor';