mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-19 14:41:24 +08:00
fix(translate): auto copy failed (#10745)
* fix(translate): auto copy failed Because translatedContent may be stale * refactor(translate): improve copy functionality dependency handling Update copy callback dependencies to include setCopied and ensure proper memoization Fix onCopy and translateText dependencies to include copy function
This commit is contained in:
parent
96ce645064
commit
f943f05cb1
@ -134,15 +134,22 @@ const TranslatePage: FC = () => {
|
||||
)
|
||||
|
||||
// 控制复制行为
|
||||
const copy = useCallback(
|
||||
async (text: string) => {
|
||||
await navigator.clipboard.writeText(text)
|
||||
setCopied(true)
|
||||
},
|
||||
[setCopied]
|
||||
)
|
||||
|
||||
const onCopy = useCallback(async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(translatedContent)
|
||||
setCopied(true)
|
||||
await copy(translatedContent)
|
||||
} catch (error) {
|
||||
logger.error('Failed to copy text to clipboard:', error as Error)
|
||||
window.toast.error(t('common.copy_failed'))
|
||||
}
|
||||
}, [setCopied, t, translatedContent])
|
||||
}, [copy, t, translatedContent])
|
||||
|
||||
/**
|
||||
* 翻译文本并保存历史记录,包含完整的异常处理,不会抛出异常
|
||||
@ -183,7 +190,7 @@ const TranslatePage: FC = () => {
|
||||
setTimeoutTimer(
|
||||
'auto-copy',
|
||||
async () => {
|
||||
await onCopy()
|
||||
await copy(translated)
|
||||
},
|
||||
100
|
||||
)
|
||||
@ -200,7 +207,7 @@ const TranslatePage: FC = () => {
|
||||
window.toast.error(t('translate.error.unknown') + ': ' + formatErrorMessage(e))
|
||||
}
|
||||
},
|
||||
[autoCopy, dispatch, onCopy, setTimeoutTimer, setTranslatedContent, setTranslating, t, translating]
|
||||
[autoCopy, copy, dispatch, setTimeoutTimer, setTranslatedContent, setTranslating, t, translating]
|
||||
)
|
||||
|
||||
// 控制翻译按钮是否可用
|
||||
|
||||
Loading…
Reference in New Issue
Block a user