Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): stabilize voice input focus restore and agent workspace test (#45169)

yyhbe31456aec42
diff --git a/web/app/components/base/chat/chat/chat-input-area/index.tsx b/web/app/components/base/chat/chat/chat-input-area/index.tsx
index f0a66ba6fd4..ed8b42d3fa0 100644
--- a/web/app/components/base/chat/chat/chat-input-area/index.tsx
+++ b/web/app/components/base/chat/chat/chat-input-area/index.tsx
@@ -8,7 +8,7 @@ import { cn } from '@langgenius/dify-ui/cn'
import { Infotip, InfotipContent, InfotipTrigger } from '@langgenius/dify-ui/infotip'
import { noop } from 'es-toolkit/function'
import { decode } from 'html-entities'
-import { useCallback, useEffect, useId, useRef, useState } from 'react'
+import { useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'
import Textarea from 'react-textarea-autosize'
import FeatureBar from '@/app/components/base/features/new-feature-panel/feature-bar'
@@ -123,7 +123,6 @@ const ChatInputArea = ({
}, [])
useEffect(() => clearCompositionEndTimer, [clearCompositionEndTimer])
const queryRef = useRef('')
- const voiceInputRef = useRef<HTMLDivElement>(null)
const voiceInputReturnFocusRef = useRef<HTMLElement | null>(null)
const voiceInputCompletedRef = useRef(false)
const handleQueryChange = useCallback(
@@ -246,50 +245,35 @@ const ChatInputArea = ({
toast.error(t(($) => $['api.actionFailed'], { ns: 'common' }))
}, [t])
const handleHideVoiceInput = useCallback(() => {
- const textareaElement = textareaRef.current
- const documentElement = textareaElement?.ownerDocument
- const activeElement = documentElement?.activeElement
- const shouldRestoreFocus =
- !!documentElement &&
- (activeElement === documentElement.body ||
- !!(activeElement && voiceInputRef.current?.contains(activeElement)))
+ setShowVoiceInput(false)
+ }, [])
+ useLayoutEffect(() => {
const returnFocusElement = voiceInputReturnFocusRef.current
+ if (showVoiceInput || !returnFocusElement) return
+
const voiceInputCompleted = voiceInputCompletedRef.current
voiceInputReturnFocusRef.current = null
voiceInputCompletedRef.current = false
- setShowVoiceInput(false)
- if (voiceInputCompleted || !shouldRestoreFocus || !documentElement) return
+ const textareaElement = textareaRef.current
+ if (!textareaElement) return
+ const activeElement = textareaElement.ownerDocument.activeElement
+ if (activeElement !== textareaElement.ownerDocument.body && activeElement !== textareaElement)
+ return
- queueMicrotask(() => {
- const currentActiveElement = documentElement.activeElement
- if (currentActiveElement !== documentElement.body && currentActiveElement !== activeElement)
- return
+ if (voiceInputCompleted) {
+ textareaElement.focus({ preventScroll: true })
+ const caretPosition = textareaElement.value.length
+ textareaElement.setSelectionRange(caretPosition, caretPosition)
+ return
+ }
+ if (activeElement === textareaElement) return
- const elementToFocus = returnFocusElement?.isConnected ? returnFocusElement : textareaElement
- elementToFocus?.focus({ preventScroll: true })
- })
- }, [textareaRef])
+ const elementToFocus = returnFocusElement.isConnected ? returnFocusElement : textareaElement
+ elementToFocus.focus({ preventScroll: true })
+ }, [showVoiceInput, textareaRef])
const handleVoiceInputConverted = (text: string) => {
voiceInputCompletedRef.current = true
handleQueryChange(text)
- queueMicrotask(() => {
- const textareaElement = textareaRef.current
- if (!textareaElement) return
-
- const activeElement = textareaElement.ownerDocument.activeElement
- if (
- activeElement &&
- activeElement !== textareaElement.ownerDocument.body &&
- activeElement !== textareaElement &&
- !voiceInputRef.current?.contains(activeElement)
- ) {
- return
- }
-
- textareaElement.focus({ preventScroll: true })
- const caretPosition = textareaElement.value.length
- textareaElement.setSelectionRange(caretPosition, caretPosition)
- })
}
const operation = (
<Operation
@@ -377,7 +361,6 @@ const ChatInputArea = ({
{showVoiceInput && speechToTextTarget && (
<div className="px-2.25">
<VoiceInput
- ref={voiceInputRef}
target={speechToTextTarget}
onCancel={handleHideVoiceInput}
onBeforeTranscribe={onBeforeSpeechToText}
diff --git a/web/app/components/workflow/nodes/agent-v2/components/__tests__/agent-orchestrate-panel-content.spec.tsx b/web/app/components/workflow/nodes/agent-v2/components/__tests__/agent-orchestrate-panel-content.spec.tsx
index 7ede4756062..32b22a4d601 100644
--- a/web/app/components/workflow/nodes/agent-v2/components/__tests__/agent-orchestrate-panel-content.spec.tsx
+++ b/web/app/components/workflow/nodes/agent-v2/components/__tests__/agent-orchestrate-panel-content.spec.tsx
@@ -726,7 +726,11 @@ describe('Inline Agent configure workspace', () => {
debug_conversation_message_count: 0,
}),
})
- await screen.findByRole('textbox', { name: `${prefix}.build.inputPlaceholder` })
+ await screen.findByRole(
+ 'textbox',
+ { name: `${prefix}.build.inputPlaceholder` },
+ { timeout: 5000 },
+ )
expect(restartButton()).toBeDisabled()
})