Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useRef, useState } from 'react'
import { useTranslation } from 'react-i18next'

import { presentDialog } from '@/componentsV2/base/Dialog'
Expand Down Expand Up @@ -28,25 +28,40 @@ export function useTextInput(options: UseTextInputOptions = {}): UseTextInputRet
const [text, setTextInternal] = useState('')
const threshold = options.threshold ?? LONG_TEXT_THRESHOLD

// Track processing state to prevent duplicate file creation
const processingTextRef = useRef<string | null>(null)

const setText = async (newText: string) => {
// Prevent duplicate processing of the same text
if (processingTextRef.current === newText) {
return
}

// Check if text exceeds threshold
if (isLongText(newText, threshold)) {
const result = await processInputText(newText, {
threshold,
onConvertToFile: options.onFileCreated
})
processingTextRef.current = newText
try {
const result = await processInputText(newText, {
threshold,
onConvertToFile: options.onFileCreated
})

if (result.success) {
setTextInternal(result.data?.processedText ?? '')
if (result.data?.convertedToFile) {
presentDialog('info', {
title: t('inputs.longTextConverted.title'),
content: t('inputs.longTextConverted.message', { length: newText.length })
})
if (result.success) {
setTextInternal(result.data?.processedText ?? '')
if (result.data?.convertedToFile) {
presentDialog('info', {
title: t('inputs.longTextConverted.title'),
content: t('inputs.longTextConverted.message', { length: newText.length })
})
}
} else {
// Fallback on error - keep the text
setTextInternal(newText)
}
} else {
// Fallback on error - keep the text
setTextInternal(newText)
} finally {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可按下面方式修复,避免旧请求把新请求的 guard 清掉:

} finally {
  if (processingTextRef.current === newText) {
    processingTextRef.current = null
  }
}

这样在 AB 并发时,只有仍持有当前 guard 的那次调用才会清空,setText(B) 不会在 B 处理中被误放行。

if (processingTextRef.current === newText) {
processingTextRef.current = null
}
}
} else {
setTextInternal(newText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const FileItem: FC<FileItemProps> = ({ file, onRemove, size, disabledContextMenu
logger.error('Handle Preview Error', error)
})
}

return (
<BaseItem
file={file}
Expand Down