From 8fe376848f1c6c5494583c87f01dde6d1ca1b635 Mon Sep 17 00:00:00 2001 From: GuanMu Date: Wed, 11 Mar 2026 11:09:29 +0800 Subject: [PATCH] fix: PromptEditor leaves a pending blur timer that triggers a Vitest unhandled error (#33253) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- .../plugins/on-blur-or-focus-block.tsx | 22 ++++++++++++++----- web/eslint-suppressions.json | 5 ----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/web/app/components/base/prompt-editor/plugins/on-blur-or-focus-block.tsx b/web/app/components/base/prompt-editor/plugins/on-blur-or-focus-block.tsx index 84eacb01ed..80c3ddba21 100644 --- a/web/app/components/base/prompt-editor/plugins/on-blur-or-focus-block.tsx +++ b/web/app/components/base/prompt-editor/plugins/on-blur-or-focus-block.tsx @@ -20,17 +20,21 @@ const OnBlurBlock: FC = ({ }) => { const [editor] = useLexicalComposerContext() - const ref = useRef(null) + const ref = useRef | null>(null) useEffect(() => { - return mergeRegister( + const clearHideMenuTimeout = () => { + if (ref.current) { + clearTimeout(ref.current) + ref.current = null + } + } + + const unregister = mergeRegister( editor.registerCommand( CLEAR_HIDE_MENU_TIMEOUT, () => { - if (ref.current) { - clearTimeout(ref.current) - ref.current = null - } + clearHideMenuTimeout() return true }, COMMAND_PRIORITY_EDITOR, @@ -41,6 +45,7 @@ const OnBlurBlock: FC = ({ // Check if the clicked target element is var-search-input const target = event?.relatedTarget as HTMLElement if (!target?.classList?.contains('var-search-input')) { + clearHideMenuTimeout() ref.current = setTimeout(() => { editor.dispatchCommand(KEY_ESCAPE_COMMAND, new KeyboardEvent('keydown', { key: 'Escape' })) }, 200) @@ -61,6 +66,11 @@ const OnBlurBlock: FC = ({ COMMAND_PRIORITY_EDITOR, ), ) + + return () => { + clearHideMenuTimeout() + unregister() + } }, [editor, onBlur, onFocus]) return null diff --git a/web/eslint-suppressions.json b/web/eslint-suppressions.json index 1c9e9d43f6..b7f18c3d3c 100644 --- a/web/eslint-suppressions.json +++ b/web/eslint-suppressions.json @@ -3624,11 +3624,6 @@ "count": 2 } }, - "app/components/base/prompt-editor/plugins/on-blur-or-focus-block.tsx": { - "ts/no-explicit-any": { - "count": 1 - } - }, "app/components/base/prompt-editor/plugins/query-block/index.tsx": { "react-refresh/only-export-components": { "count": 2