From d476b25515fbfbcc45c922e37735ae850f4dfd6b Mon Sep 17 00:00:00 2001 From: Kent Wang Date: Mon, 30 Jun 2025 10:01:18 +0800 Subject: [PATCH] Add a ref to ensure invalid ref returns the latest value (#8816) --- .../ui/components/editors/environment-editor.tsx | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/insomnia/src/ui/components/editors/environment-editor.tsx b/packages/insomnia/src/ui/components/editors/environment-editor.tsx index 68654bda2c..6aa0512ade 100644 --- a/packages/insomnia/src/ui/components/editors/environment-editor.tsx +++ b/packages/insomnia/src/ui/components/editors/environment-editor.tsx @@ -24,6 +24,7 @@ export interface EnvironmentEditorHandle { export const EnvironmentEditor = forwardRef( ({ environmentInfo, onBlur, onChange }, ref) => { const editorRef = useRef(null); + const editorErrorRef = useRef(''); const [error, setError] = useState(''); const getValue = useCallback(() => { // @ts-expect-error -- current can be null @@ -41,12 +42,17 @@ export const EnvironmentEditor = forwardRef( useImperativeHandle( ref, () => ({ - isValid: () => !error, + isValid: () => !editorErrorRef.current, getValue, }), - [error, getValue], + [getValue], ); + const updateEditorError = (message: string) => { + editorErrorRef.current = message; + setError(message); + }; + const defaultValue = orderedJSON.stringify( environmentInfo.object, environmentInfo.propertyOrder || null, @@ -60,7 +66,7 @@ export const EnvironmentEditor = forwardRef( autoPrettify enableNunjucks onChange={() => { - setError(''); + updateEditorError(''); try { const value = getValue(); // Check for invalid key names @@ -68,13 +74,13 @@ export const EnvironmentEditor = forwardRef( // Check root and nested properties const err = checkNestedKeys(value.object); if (err) { - setError(err); + updateEditorError(err); } else { onChange?.(value); } } } catch (err) { - setError(err.message); + updateEditorError(err.message); } }} defaultValue={defaultValue}