Commit 5d5df55e by weiyudumei

fix: 修复自动提交逻辑被调用两次的问题

- 拆分第一个 useEffect,避免因 attachment 或 textFields 变化而重置状态
- 优化自动提交 useEffect,只在确定执行时才更新 ref
- 添加 setTimeout 清理函数,避免组件卸载后仍执行
- 为 CardWorkflowForm 添加稳定的 key,确保组件实例正确复用
parent 2b1c2a28
......@@ -53,7 +53,11 @@ export const CardWorkflowForm: React.FC<CardWorkflowFormProps> = ({
}
setFormValues(initialValues)
// 重置自动提交标志,当 firstQaFlag 变化时重新评估
}, [textFields, searchParams, answer.firstQaFlag])
// 单独处理 firstQaFlag 的变化,重置自动提交标志
useEffect(() => {
// 重置自动提交标志,当 firstQaFlag 从 true 变为 false/undefined 时重新评估
if (answer.firstQaFlag !== true) {
hasAutoSubmittedRef.current = false
lastFirstQaFlagRef.current = false // 重置记录的值,确保下次变为 true 时能正确触发
......@@ -64,22 +68,21 @@ export const CardWorkflowForm: React.FC<CardWorkflowFormProps> = ({
lastFirstQaFlagRef.current = false // 设置为 false,让自动提交逻辑能正确触发
}
}
}, [attachment, answer.firstQaFlag, searchParams, textFields])
}, [answer.firstQaFlag])
// 如果 firstQaFlag 为 true,自动提交(仅对流式返回的新记录,历史记录不会渲染此组件)
useEffect(() => {
// 如果 firstQaFlag 为 true,尝试自动提交
// 检查 firstQaFlag 是否从 false/undefined 变为 true(首次变为 true)
const isFirstQaFlagChangedToTrue = answer.firstQaFlag === true && lastFirstQaFlagRef.current !== true
// 更新记录的值
lastFirstQaFlagRef.current = answer.firstQaFlag
// 只在 firstQaFlag 首次变为 true 且尚未自动提交时执行
if (isFirstQaFlagChangedToTrue && !hasAutoSubmittedRef.current && !isSubmitting && !isCompleted) {
// 检查是否有字段(textFields 不为空)
if (textFields.length === 0) {
console.log('[CardWorkflowForm] 自动提交:textFields 为空,跳过')
// 更新记录的值
lastFirstQaFlagRef.current = answer.firstQaFlag
return
}
......@@ -102,6 +105,8 @@ export const CardWorkflowForm: React.FC<CardWorkflowFormProps> = ({
if (allFieldsHaveValues) {
// 立即设置 hasAutoSubmittedRef,防止重复提交
hasAutoSubmittedRef.current = true
// 更新记录的值
lastFirstQaFlagRef.current = answer.firstQaFlag
// 构建 paramMap
const paramMap: Record<string, string> = {}
......@@ -118,15 +123,25 @@ export const CardWorkflowForm: React.FC<CardWorkflowFormProps> = ({
console.log('[CardWorkflowForm] 自动提交:准备提交', { paramMap, recordId, workFlowSessionId })
// 延迟一下再提交,确保表单值已经设置完成
setTimeout(() => {
const timeoutId = setTimeout(() => {
console.log('[CardWorkflowForm] 自动提交:执行提交')
onSubmitWorkflowRef.current(paramMap, recordId, workFlowSessionId)
}, 100)
// 清理函数:如果组件卸载或依赖项变化,取消定时器
return () => {
clearTimeout(timeoutId)
}
} else {
console.log('[CardWorkflowForm] 自动提交:字段值不完整,跳过', {
fields: textFields.map(f => ({ key: f.key, value: searchParams.get(f.key) })),
})
// 更新记录的值
lastFirstQaFlagRef.current = answer.firstQaFlag
}
} else {
// 更新记录的值(即使不执行自动提交,也要更新记录)
lastFirstQaFlagRef.current = answer.firstQaFlag
}
// 移除 onSubmitWorkflow 和 textFields 作为依赖项,使用 useRef 保存引用,避免函数引用变化或 textFields 更新导致重复触发
// textFields 更新不应该触发重新提交,因为我们已经检查了 firstQaFlag 的变化
......
......@@ -176,6 +176,7 @@ export const ChatAnswerAttachment: React.FC<ChatAnswerAttachmentProps> = ({
&& attachment.content?.name === 'form_input'
&& attachment.content?.fieldList?.some(field => field.type === 'text') && (
<CardWorkflowForm
key={`workflow-form-${attachment.id || index}-${answer.recordId || 'new'}`}
attachment={attachment}
answer={answer}
onSubmitWorkflow={onWorkflowSubmit || (() => { })}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment